🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Background-repeat

AI & DATA SCIENCE // background-repeat

The background-repeat property controls whether and how a background image tiles to fill an element's background area, defaulting to repeating in both directions.

Syntax

background-repeat: repeat | no-repeat | repeat-x | repeat-y | space | round;

Deep Dive Course

The default value, repeat, tiles the image both horizontally and vertically until it fills the element's entire background area, which is ideal for small, seamless pattern textures but usually undesired for a single larger photograph. no-repeat displays the image exactly once with no tiling at all, repeat-x and repeat-y restrict tiling to only the horizontal or vertical direction respectively, and the less common space and round values adjust tile spacing or scaling specifically to avoid a partially-cut-off tile at the edge, evenly distributing whole tiles instead.

1Understanding Background-repeat

The default value, repeat, tiles the image both horizontally and vertically until it fills the element's entire background area, which is ideal for small, seamless pattern textures but usually undesired for a single larger photograph. no-repeat displays the image exactly once with no tiling at all, repeat-x and repeat-y restrict tiling to only the horizontal or vertical direction respectively, and the less common space and round values adjust tile spacing or scaling specifically to avoid a partially-cut-off tile at the edge, evenly distributing whole tiles instead.

💡

Set background-repeat: no-repeat whenever using a single, larger image as a background rather than a small seamless pattern — forgetting this is one of the most common reasons a background photo unexpectedly appears tiled multiple times across an element.

editor.html
.hero {
  background-image: url("photo.jpg");
  background-repeat: no-repeat;
}
localhost:3000

2Practical Example

Here is a real-world application of Background-repeat showing how it is used in production CSS code.

editor.html
.pattern-bg {
  background-image: url("dots.png");
  background-repeat: repeat;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Background-repeat:

1. Set background-repeat: no-repeat explicitly whenever using a single larger image, since the default repeat value will otherwise tile it unexpectedly

2. Leave background-repeat at its default repeat value specifically for small, seamlessly-tiling pattern or texture images designed for that purpose

3. Use repeat-x or repeat-y specifically when a pattern should tile in only one direction, like a horizontal repeating border texture

⚠️

Tip: Set background-repeat: no-repeat whenever using a single, larger image as a background rather than a small seamless pattern — forgetting this is one of the most common reasons a background photo unexpectedly appears tiled multiple times across an element.

editor.html
.hero {
  background-image: url("photo.jpg");
  background-repeat: no-repeat;
}
localhost:3000

Examples

Example 01Basic Usage
.hero {
  background-image: url("photo.jpg");
  background-repeat: no-repeat;
}
Example 02Advanced Example
.pattern-bg {
  background-image: url("dots.png");
  background-repeat: repeat;
}

Best Practices

  • Set background-repeat: no-repeat explicitly whenever using a single larger image, since the default repeat value will otherwise tile it unexpectedly
  • Leave background-repeat at its default repeat value specifically for small, seamlessly-tiling pattern or texture images designed for that purpose
  • Use repeat-x or repeat-y specifically when a pattern should tile in only one direction, like a horizontal repeating border texture

Interview Question

Why does a small pattern image like a seamless texture typically look fine when tiled with the default background-repeat, while a larger photograph almost always looks wrong tiled the same way?

Hint: Think about what 'seamless' actually means for a pattern image, and whether a typical photograph shares that same property.

A seamless pattern image is specifically designed so that its edges align perfectly when placed directly adjacent to another copy of itself, meaning the visual content at its right edge continues naturally into the left edge of the next tile, and similarly for top and bottom, producing a smooth, continuous-looking texture across the tiled grid with no visible seams or repetition artifacts, which is exactly the visual effect background-repeat's default tiling is meant to produce for that kind of purpose-built image. A typical photograph has no such edge-matching property at all, its right edge and left edge depict entirely unrelated visual content with no intentional continuity between them, so tiling it produces an obviously repeating grid of the same distinct image with visually jarring, clearly visible seams at every tile boundary, immediately looking wrong and unintentional rather than like a deliberate, cohesive background pattern. This fundamental difference, purpose-built for seamless tiling versus not, is exactly why the same default repeating behavior looks appropriate for one kind of image and clearly broken for the other.

Exercises

MediumPractice using Background-repeat in a real scenario.
View Solution
.hero {
  background-image: url("photo.jpg");
  background-repeat: no-repeat;
}

Frequently Asked Questions

Why does a small pattern image like a seamless texture typically look fine when tiled with the default background-repeat, while a larger photograph almost always looks wrong tiled the same way?

A seamless pattern image is specifically designed so that its edges align perfectly when placed directly adjacent to another copy of itself, meaning the visual content at its right edge continues naturally into the left edge of the next tile, and similarly for top and bottom, producing a smooth, continuous-looking texture across the tiled grid with no visible seams or repetition artifacts, which is exactly the visual effect background-repeat's default tiling is meant to produce for that kind of purpose-built image. A typical photograph has no such edge-matching property at all, its right edge and left edge depict entirely unrelated visual content with no intentional continuity between them, so tiling it produces an obviously repeating grid of the same distinct image with visually jarring, clearly visible seams at every tile boundary, immediately looking wrong and unintentional rather than like a deliberate, cohesive background pattern. This fundamental difference, purpose-built for seamless tiling versus not, is exactly why the same default repeating behavior looks appropriate for one kind of image and clearly broken for the other.

Related Functions

Background-imageBackground-sizeBackground-position