The Wayback Machine - https://web.archive.org/web/20240926152114/https://www.geeksforgeeks.org/css-background-repeat-property/
Open In App

CSS background-repeat Property

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The background-repeat property in CSS is used to repeat the background image both horizontally and vertically. It also decides whether the background-image will be repeated or not.

Syntax:

background-repeat: repeat|repeat-x|round|space|repeat-y|no-repeat|initial|
inherit;

Default Value: Its default value is initial. 

Property Values:

  • repeat: This property is used to repeat the background image both horizontally and vertically. The last image will be clipped if it is not fit in the browser window. 
  • repeat-x: This property is used to repeat the background image horizontally. 
  • repeat-y: This property is used to set the background image repeated only vertically. 
  • no-repeat: This property does not repeat the background image. It displays the background image only once. 

Example: In this example, we are using the background-repeat: repeat; property.

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>background-repeat property</title>
    <style>
        body {
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-repeat: repeat;
            background-size: 200px 150px;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>background-repeat: repeat;</h2>
</body>
  
</html>

Output: 

Example:  In this example, we are using the background-repeat: repeat-x; property.

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>background-repeat property</title>
    <style>
        body {
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-repeat: repeat-x;
            background-size: 200px 150px;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>background-repeat: repeat-x;</h2>
</body>
  
</html>

Output: 

 

Example:  In this example, we are using the background-repeat: repeat-y; property.

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>background-repeat property</title>
    <style>
        body {
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-repeat: repeat-y;
            background-size: 200px 150px;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>background-repeat: repeat-y;</h2>
</body>
  
</html>

Output: 

Example:  In this example, we are using the background-image: no-repeat; property.

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>background-repeat property</title>
    <style>
        body {
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
            background-repeat: no-repeat;
            background-size: 400px 250px;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>background-repeat: no-repeat;</h2>
</body>
  
</html>

Output: 

Supported Browsers: The browsers supported by background-repeat property are listed below:  

  • Google Chrome 1.0 and above
  • Edge 12.0 and above
  • Firefox 1.0 and above
  • Opera 3.5 and above
  • Apple Safari 1.0 and above

CSS background-repeat Property – FAQs

What is the background-repeat property in CSS?

The background-repeat property in CSS controls how a background image repeats (tiles) within an element. You can specify whether the image should repeat horizontally, vertically, both, or not at all.

What is the purpose of background-repeat: repeat-x and repeat-y?

The values repeat-x and repeat-y allow you to control the repetition of a background image along the x-axis (horizontal) or y-axis (vertical) respectively. For example, repeat-x repeats the image only horizontally.

How does background-repeat work with multiple background images?

You can control the repeat behavior of multiple background images by specifying each repeat setting in the order of the images. For example: background-repeat: no-repeat, repeat;.

Why does my background image look distorted when repeating?

Distortion occurs when the image is scaled or stretched due to conflicting background-size settings. Ensure the image dimensions are appropriate for the repeat pattern and consider adjusting background-size to prevent distortion.

How can I stop a background image from repeating?

To prevent a background image from repeating, use background-repeat: no-repeat;. This will display the image once without tiling.



Previous Article
Next Article

Similar Reads

How to access index of the parent ng-repeat from child ng-repeat in AngularJS ?
In this article, we will see How to access the index of the parent ng-repeat from child ng-repeat in AngularJS. In AngularJS applications, when we implement the nested ng-repeat directives and if we need to access the index of the parent ng-repeat from the child ng-repeat, then we can do this by using 2 different approaches like by using the $paren
6 min read
Tailwind CSS Background Repeat
This class accepts more than one value in tailwind CSS. All the properties are covered in class form. It is the alternative to the CSS background-position property. This class is used to repeat the background image both horizontally and vertically. It also decides whether the background-image will be repeated or not. Background Repeat Classes: bg-r
3 min read
How to repeat background image vertically and horizontally using CSS ?
In this article, we are going to discuss the background image repeat property of CSS. Also, we are going to discuss how to repeat the background image in horizontal and vertical directions. The background-repeat property in CSS is used to repeat the background image both horizontally and vertically. It also decides whether the background-image will
2 min read
CSS mask-repeat property
CSS mask-repeat sets how the mask images are placed after they have been sized and positioned. The mask image can be repeated along the vertical or horizontal or both axes or not repeated. Syntax: mask-repeat: One-values /* Or */ mask-repeat: Two-values /* Or */ mask-repeat: Multiple values /* Or */ mask-repeat: Global values Property values: This
2 min read
CSS border-image-repeat Property
The border-image-repeat property in CSS is used to scale and tiling the border images. It can be used to match the middle part of the border image to the size of the border. It can have either one or two values. One is for the horizontal and one for the vertical axis. Only one value is given then it applies to all sides, but two values are given it
4 min read
How to repeat border image using CSS ?
You can repeat border images using the border-image-repeat property in CSS. It is generally used for scaling and tiling the border-image. It is used to match the middle part of the border-image to the size of the border. Syntax:border-image-repeat: stretch|repeat|round|initial|inheritNote: The border-image-slice property is used to divide or slice
2 min read
Difference between background and background-color
Background Property The background property in CSS is very commonly used and contains many variants. The background property consists of all the following properties:background-colorbackground-imagebackground-positionbackground-sizebackground-repeatbackground-originbackground-clipbackground-attachmentExample: C/C++ Code &lt;style&gt; h1 { b
1 min read
Which property is used to set the background image of an element using CSS ?
In this article, we will see the property used for setting the background image of an element in CSS. The background image can be set using the background-image property that is used to set one or more background images for an element. By default, it places the image in the top left corner. To specify two or more images, we need to specify the sepa
1 min read
CSS background-position-y Property
The CSS background-position-y property is used to set the initial vertical position for the background image i.e. it is used to set an image at a certain position in the vertical direction. The position is relative to the positioning later, which can be set by using the CSS background-origin property. Syntax: background-position-y: value; Property
2 min read
CSS background-position-x Property
The CSS background-position-x property is used to set the initial horizontal position for the background image i.e. it is used to set an image at a certain position in a horizontal direction. The position is relative to the positioning layer, which can be set by using the CSS background-origin property. Syntax: background-position-x: value; Propert
2 min read
How to use the background Property in CSS ?
The background property in CSS is a comprehensive shorthand property that allows you to set various background-related properties for an element. It's used to control the background color, image, position, size, and other related aspects. Syntax:/* Example of using the background property */.element { background: [background-color] [background-imag
1 min read
CSS background-attachment Property
The background-attachment property sets whether a background image scrolls with the rest of the page or is fixed. It has three possible values: scroll (default, the image moves with content), fixed (image stays in place), and `local` (image scrolls within the element). Syntaxbackground-attachment: scroll|fixed|local|initial|inherit;Property ValuePr
8 min read
CSS background-blend-mode Property
The background-blend-mode Property defines how the element's background image should blend with each other and with the element's background-color.  Syntax:  background-blend-mode: normal|multiply|screen|darken|lighten|color-dodge|saturation|difference|luminosity|overlay;Default Value:  normalProperty:Normal: This is the default value. It sets the
5 min read
CSS background-clip Property
The background-clip property in CSS controls how a background (color or image) extends within an element, allowing you to dictate the reach of your background. Syntax: background-clip: border-box | padding-box | content-box | text | initial | inherit;Property value:  border-box: The border-box property is used to set the background color spread ove
3 min read
CSS background-origin property
The background-origin property in CSS is your key to managing the background image of a webpage. It sets the origin point of the image in the background, with the default setting placing the background image origin at the upper-left corner of the screen/webpage. Syntax:  background-origin: padding-box|border-box|content-box|initial| inherit;Propert
5 min read
CSS background-color Property
The background-color CSS property sets the background color of an element, allowing you to create a solid color backdrop. You can define the color using named colors, hexadecimal values, RGB, RGBA, HSL, or HSLA. This property applies to both the content and padding areas of the element. By using the background-color property, you can easily enhance
4 min read
CSS background-image Property
The background-image property allows you to set one or more background images for an element. It allows you to specify the image URL and combine it with other background properties to control its position, size, repeat behavior, and more, enhancing the visual design of web pages. Syntaxelement_selector { background-image: url('image_url')| none | i
4 min read
CSS background-position Property
The CSS background-position property sets the starting position of a background image within an element. It allows you to use keywords, percentages, or length values to control where the image is placed. This property helps customize the visual layout by precisely aligning background images, enhancing the overall design and appearance of your webpa
7 min read
CSS background-size Property
The background-size property specifies the size of background images. It accepts values in pixels, percentages, or keywords such as cover (to ensure the image covers the entire element) and contain (to fit the image within the element), enabling flexible and responsive designs. Syntaxbackground-size: auto | length | cover | contain | initial | inhe
5 min read
Create a CSS Fireflies background using HTML/CSS
In this article, we will see how to create the CSS Fireflies background using HTML & CSS, along with understanding its implementation through the illustration. The CSS fireflies effect can be created by utilizing the CSS animation and we will implement 3 animations to achieve the fireflies effect. To create randomness, we used different animati
7 min read
Angular-JS ng-repeat Directive
Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code for a number of times or once per item in a collection of items. ng-repeat is mostly used on arrays and objects. ng-repeat is similar to a loop that we have in C, C++ or other languages but technically it instantiates a template(normally a set of HTML structures) for each e
2 min read
How to hide or show one record from an ng-repeat within a table based on ng-click?
The way to hide or show a particular record in a table is similar to what is used to hide or show any elements of the DOM. The basic and the first thing that comes to mind is the ng-show and ng-hide directives which show or hides respectively based on binary results of the expressions bound to them. Another way can be the use of ng-if which works l
3 min read
How to filter ng-repeat values according to ng-model using AngularJS ?
In this article, we will see how to filter the value of the ng-repeat directive according to the ng-model directive using AngularJS. The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat v
2 min read
How to iterate over the keys and values with ng-repeat in AngularJS ?
The task is to iterate over a JS object (its keys and values) using the ng-repeat directive. This can be done using parenthesis in the ng-repeat directive to explicitly ask for a key-value pair parameter from angularJS. Here the variable key contains the key of the object and value contains the value of the object. Syntax: <element ng-repeat="(k
3 min read
How to display length of filtered ng-repeat data in AngularJS ?
The task is to Display the length of filtered ng-repeat data. Here we are going to use alias expression to solve this problem. Approach: To display the length of filtered ng-repeat data we use an alias expression. We create an alias for the variable used to filter the ng-repeat data and display the length of the alias at each moment. Syntax: <el
2 min read
How to iterate over filtered (ng-repeat filter) collection of objects in AngularJS ?
The task is to iterate over a collection of objects already filtered by ng-repeat filters and after clicking a button, change some property of only those objects which got filtered. Suppose you have a collection of objects that were filtered by a text search within a title property on each object. Now you want to iterate over this subset of the lis
3 min read
How to fetch the details using ng-repeat in AngularJS ?
In this article, we will see how to fetch the details with the help of the ng-repeat directive in Angular, along with understanding its implementation through the illustrations. AngularJS contains various types of pre-defined Directives, where most of the directives start with ng which denotes Angular. The ng-repeat directive is used to iterate ove
2 min read
How to repeat HTML element multiple times using ngFor based on a number?
In Angular ngFor is directive which accomplishes the motive of displaying repeated or list of contents with least lines of code, this serves the same purpose as for loops in conventional programming languages. We can print repeated lines of content based on a number using the javascript/typescript function Array() which will generate a list of numb
2 min read
JavaScript RangeError - Repeat count must be less than infinity
This JavaScript exception repeat count must be less than infinity occurs if the passed argument of String.prototype.repeat() method is infinity. Output message: RangeError: argument out of range (Edge) RangeError: repeat count must be less than infinity and not overflow maximum string size (Firefox) RangeError: Invalid count value (Chrome) Error Ty
2 min read
JavaScript RangeError - Repeat count must be non-negative
This JavaScript exception repeat count must be non-negative occurs if the argument passed to String.prototype.repeat() method is a negative number. Message: RangeError: argument out of range RangeError: repeat count must be non-negative (Firefox) RangeError: Invalid count value (Chrome) Error Type: RangeError Cause of Error: The repeat() method has
1 min read