🔄 CSS background-repeat
: Controlling Background Image Repetition
When you add a background image to a webpage, sometimes you want it to repeat (tile) across an element’s background, and other times you want it to appear only once. The CSS property that controls this behavior is background-repeat
.
🧾 What is background-repeat
?
The background-repeat
property specifies if and how a background image should repeat (or tile) inside an element.
By default, background images repeat both horizontally and vertically to fill the background area. background-repeat
gives you control to change this behavior.
🧬 Syntax
selector {
background-repeat: repeat | repeat-x | repeat-y | no-repeat | space | round;
}
🔄 Common Values Explained
Value | Description | Example Behavior |
---|---|---|
repeat | The default. Tiles the image both horizontally and vertically to fill the element. | Tiles in both directions |
repeat-x | Repeats the image only horizontally (along the x-axis). | Tiles left to right, no vertical repeat |
repeat-y | Repeats the image only vertically (along the y-axis). | Tiles top to bottom, no horizontal repeat |
no-repeat | Displays the image only once, no repetition. | Single image, no tiling |
space | Tiles the image as many times as possible without clipping and leaves space between tiles | Tiles spaced out evenly |
round | Tiles the image but stretches/shrinks each tile to fill the area without clipping | Tiles resized to fit exactly |
🎯 Visual Examples
1. repeat
(default)
div {
background-image: url("pattern.png");
background-repeat: repeat;
}
The pattern will tile infinitely across the whole element horizontally and vertically.
2. repeat-x
div {
background-image: url("stripe.png");
background-repeat: repeat-x;
}
The stripe pattern repeats only left-to-right.
3. no-repeat
div {
background-image: url("logo.png");
background-repeat: no-repeat;
}
The logo appears once, usually positioned by background-position
.
4. space
div {
background-image: url("dots.png");
background-repeat: space;
}
Dots are tiled with space in between; no clipping occurs.
5. round
div {
background-image: url("grid.png");
background-repeat: round;
}
The grid tiles are stretched or shrunk so they fit perfectly without clipping or spacing.
🛠️ When to Use background-repeat
- Use
no-repeat
for hero images, logos, or icons you want to show once. - Use
repeat
for textures, patterns, or tiled backgrounds. - Use
repeat-x
orrepeat-y
when you want to tile in only one direction (e.g., horizontal stripes). - Use
space
andround
for advanced control over tiling when you want consistent spacing or scaling.
✅ Tips & Best Practices
- Combine with
background-size
to control tile size. - Use
background-position
to control where the image starts repeating. - Be mindful of image resolution to avoid pixelation when tiling.
- Test how your tiled backgrounds behave on different screen sizes.
🔚 Conclusion
The background-repeat
property is a simple yet crucial tool for controlling how background images fill an element. Whether creating subtle textures or bold patterns, mastering this property allows you to tailor the visual rhythm of your design.