πŸ” CSS Tutorial: background-repeat

πŸ“Œ 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

You can also use two values:horizontal | vertical
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

ValueDescriptionUse Case
repeatTiles both directionsPatterns, textures
repeat-xTiles horizontallyLines, dividers
repeat-yTiles verticallySidebars, vertical stripes
no-repeatNo tilingHero images, banners
roundScales tiles to fit perfectlyEven framing
spaceAdds gaps between tilesDecorative spacing patterns

πŸ–ΌοΈ Visual Demo

>>β€œRepetition in design brings rhythm β€” use background-repeat to create subtle or bold patterns.” 🎨✨

πŸŽ‰ 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. πŸš€