The background-repeat property controls how a background image repeats (tiles) inside an element. By default, the image repeats both horizontally and vertically to fill the entire area.
background-image.Basic syntax of the background-repeat property:
/* General syntax for background-repeat */
selector {
background-repeat: repeat;
/* repeat | no-repeat | repeat-x | repeat-y */
}
Here are four classes that use different background-repeat values.
/* Same image, different repeat behavior */
.repeat {
background-image: url("pattern.png");
background-repeat: repeat;
}
.no-repeat {
background-image: url("pattern.png");
background-repeat: no-repeat;
}
.repeat-x {
background-image: url("pattern.png");
background-repeat: repeat-x;
}
.repeat-y {
background-image: url("pattern.png");
background-repeat: repeat-y;
}
Often, you will combine background-repeat with background-position and background-size for full control.
/* Single logo positioned at the top-right corner */
.hero-header {
background-image: url("logo.png");
background-repeat: no-repeat;
background-position: right top;
background-size: 80px 80px;
}
.repeat – the pattern tiles in both directions, filling the element..no-repeat – only one image is visible; the rest shows the background color..repeat-x – the image repeats from left to right, forming a horizontal strip..repeat-y – the image repeats from top to bottom, forming a vertical strip..hero-header – a single logo is pinned to the top-right without repeating.no-repeat for single images like logos or icons.background-repeat with background-position to control exactly where the image appears.background-size: cover; or contain; when you want scaling instead of tiling.background-repeat: repeat;. Resize the box and observe how it tiles.background-repeat: no-repeat; and compare the result.repeat-x and one with repeat-y. See the difference between horizontal and vertical tiling.background-position: center; with no-repeat and observe where the image appears.background-size (e.g. 50px 50px, cover) together with different background-repeat values.