π Introduction
The background-repeat property in CSS controls how a background image is repeated (tiled) across an element. By default, background images repeat horizontally and vertically, but you can change this behavior to create precise visual layouts. This property is useful for patterns, textures, hero sections, and UI decorations. π¨β¨
π― What Does background-repeat Do?
- Controls whether a background image repeats
- Determines repeat direction (X/Y)
- Can prevent repetition for single-image backgrounds
- Supports special values like round & space
β¨ Syntax
Syntax
selector {
background-repeat: repeat | repeat-x | repeat-y | no-repeat | round | space;
}Note
Example:
background-repeat: repeat no-repeat;π§© Available Values
1. repeat (default)
Image repeats both horizontally and vertically.
repeat
background-repeat: repeat;2. no-repeat
Shows the image only once.
no-repeat
background-repeat: no-repeat;3. repeat-x
Repeats the image horizontally only.
repeat-x
background-repeat: repeat-x;4. repeat-y
Repeats the image vertically only.
repeat-y
background-repeat: repeat-y;5. round
Tiles the image but scales each tile so they fit perfectly with no cut-off or leftover space.
round
background-repeat: round;6. space
Repeats the image but adds equal spacing between tiles; the image never gets cut or stretched.
space
background-repeat: space;π§© Using Two Values
Use two values when you want different behavior horizontally and vertically.
Two-value example
/* horizontal | vertical */
background-repeat: repeat no-repeat;π₯ Practical Examples
1. Pattern Background
Pattern Tile
.pattern-box {
background-image: url("pattern.png");
background-repeat: repeat;
}2. Single Background Image (no repeat)
Single Image
.hero {
background-image: url("hero.jpg");
background-repeat: no-repeat;
background-size: cover;
}3. Horizontal Repeating Texture
repeat-x example
.divider {
background-image: url("line.png");
background-repeat: repeat-x;
}4. Vertical Repeating Line
repeat-y example
.sidebar {
background-image: url("stripe.png");
background-repeat: repeat-y;
}5. Perfect Fit Using round
round example
.gallery {
background-image: url("tile.png");
background-repeat: round;
}6. Spaced Background Tiles
space example
.spaced {
background-image: url("dot.png");
background-repeat: space;
}π Summary Table
| Value | Description | Use Case |
|---|---|---|
| repeat | Tiles both directions | Patterns, textures |
| repeat-x | Tiles horizontally | Lines, dividers |
| repeat-y | Tiles vertically | Sidebars, vertical stripes |
| no-repeat | No tiling | Hero images, banners |
| round | Scales tiles to fit perfectly | Even framing |
| space | Adds gaps between tiles | Decorative spacing patterns |
πΌοΈ Visual Demo
Further details can be found at: https://dummyimage.com/900x260/eeeeee/000000&text=background-repeat+demo
π Conclusion
The background-repeat property gives you precise control over how background images tile across an element. Whether you want full patterns, one-time hero images, evenly spaced tiles, or scaled patterns, this property is essential for crafting beautiful UI backgrounds. π