The background-repeat
property defines how a background image is repeated if it is smaller than its container. By default, background images repeat both horizontally and vertically. This property gives you precise control over that tiling behavior.
Example 1: No Repetition
/* style.css */
.repeat-one {
background-image: url('https://via.placeholder.com/100');
/* Prevents the image from repeating in any direction */
background-repeat: no-repeat;
height: 250px;
border: 1px solid grey;
}
Explanation
The no-repeat
value ensures that the background image is displayed only once. It will appear at the top-left corner of the element by default.
Example 2: Repeat Horizontally
/* style.css */
.repeat-two {
background-image: url('https://via.placeholder.com/100');
/* Repeats the image horizontally across the element */
background-repeat: repeat-x;
height: 100px;
border: 1px solid grey;
}
Explanation
Using repeat-x
causes the background image to tile only along the horizontal axis (the x-axis). The vertical repetition is disabled.
Example 3: Repeat Vertically
/* style.css */
.repeat-three {
background-image: url('https://via.placeholder.com/100');
/* Repeats the image vertically down the element */
background-repeat: repeat-y;
height: 250px;
width: 100px;
border: 1px solid grey;
}
Explanation
The repeat-y
value makes the background image tile only along the vertical axis (the y-axis), creating a column of the image.
Example 4: The Default repeat
Value
/* style.css */
.repeat-four {
background-image: url('https://via.placeholder.com/100');
/* Explicitly sets the default behavior to repeat in both directions */
background-repeat: repeat;
height: 250px;
border: 1px solid grey;
}
Explanation
The repeat
value is the default behavior. It causes the image to tile both horizontally and vertically until it fills the entire background of the element.
Example 5: space
Value for Even Spacing
/* style.css */
.repeat-five {
background-image: url('https://via.placeholder.com/100');
/* Repeats the image with even spacing between them, without clipping */
background-repeat: space;
height: 250px;
border: 1px solid grey;
}
Explanation
The space
value repeats the image as many times as it can without clipping it. The extra space is distributed evenly between the images.
Example 6: round
Value for Stretching
/* style.css */
.repeat-six {
background-image: url('https://via.placeholder.com/100');
/* Stretches the image slightly to avoid gaps or clipping */
background-repeat: round;
height: 250px;
border: 1px solid grey;
}
Explanation
The round
value is similar to repeat
, but it stretches or squashes the image instances slightly so that they fit perfectly within the container without any gaps.
Example 7: Two-Value Syntax for Different Axes
/* style.css */
.repeat-seven {
background-image: url('https://via.placeholder.com/100');
/* First value for horizontal, second for vertical (repeat-x, no-repeat-y) */
background-repeat: repeat no-repeat;
height: 250px;
border: 1px solid grey;
}
Explanation
You can provide two values to background-repeat
: the first for horizontal repetition and the second for vertical. This example repeats the image horizontally but not vertically.