Background Repeat and Position
What you'll learn: How to control whether background images tile and where they appear inside an element.
When you add a background image to an element, CSS makes decisions about how to display it. By default, the image tiles (repeats) to fill the entire element—like wallpaper covering a wall. But you can control both whether it repeats and where it sits.
Background Repeat
The background-repeat property controls tiling behavior:
repeat(default): Tiles both horizontally and verticallyrepeat-x: Repeats only horizontally (left to right)repeat-y: Repeats only vertically (top to bottom)no-repeat: Shows the image once, no tiling
.hero {
background-image: url('pattern.png');
background-repeat: no-repeat;
}
.banner {
background-image: url('stripe.png');
background-repeat: repeat-x; /* Only tiles horizontally */
}
Background Position
The background-position property places your image within the element. You can use keywords, percentages, or absolute units:
.header {
background-image: url('logo.png');
background-repeat: no-repeat;
background-position: center; /* Centers the image */
}
.sidebar {
background-image: url('icon.png');
background-repeat: no-repeat;
background-position: top right; /* Top-right corner */
}
.footer {
background-image: url('badge.png');
background-repeat: no-repeat;
background-position: 20px 10px; /* 20px from left, 10px from top */
}
Common position keywords include top, bottom, left, right, and center. You can combine them or use precise measurements.
Key Takeaway: Use background-repeat to control image tiling (or turn it off with no-repeat) and background-position to place your image exactly where you want it within the element.