CSS
π― What is background-repeat?
The background-repeat property controls:
- horizontal repetition
- vertical repetition
- both directions
- or no repetition at all
for a background image.
π§© Simple Definition
background-repeat = controls how a background image repeats
π‘ Basic Syntax
background-repeat: repeat;
π§ Default Behavior
By default, background images repeat both horizontally and vertically.
background-repeat: repeat;
π This is the default value.
βοΈ Accepted Values
| Value | Description |
|---|---|
| repeat | Repeats image horizontally and vertically |
| repeat-x | Repeats image only horizontally |
| repeat-y | Repeats image only vertically |
| no-repeat | Displays image only once |
| space | Repeats image with equal spacing |
| round | Resizes image to fit perfectly |
| initial | Resets to default value |
| inherit | Inherits value from parent element |
πΉ repeat
background-repeat: repeat;
π The image repeats in both directions.
πΉ repeat-x
background-repeat: repeat-x;
π The image repeats only along the horizontal axis.
πΉ repeat-y
background-repeat: repeat-y;
π The image repeats only along the vertical axis.
πΉ no-repeat
background-repeat: no-repeat;
π The image appears only once.
This is commonly used in hero sections and banners.
πΉ space
background-repeat: space;
π The images repeat with equal spacing between them.
Extra space is distributed evenly.
πΉ round
background-repeat: round;
π The image is automatically resized so it fits perfectly before repeating.
π§© Practical Examples
1. Repeating Background
body {
background-image: url("pattern.png");
background-repeat: repeat;
}
2. Horizontal Repeat
div {
background-image: url("line.png");
background-repeat: repeat-x;
}
3. Single Background Image
.hero {
background-image: url("banner.jpg");
background-repeat: no-repeat;
}
π¨ Common Usage with Other Properties
background-repeat is often combined with:
| Property | Purpose |
|---|---|
| background-size | Controls image size |
| background-position | Controls image placement |
| background-image | Defines the image |
π§© Example
.hero {
background-image: url("hero.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
π§ Explanation
no-repeat
Shows the image only once.
cover
Makes the image fill the entire area.
center
Centers the image inside the element.
β οΈ Important Note
Small images may create unexpected repeating patterns if background-repeat is not controlled properly.
For large banners or hero sections, developers usually use:
background-repeat: no-repeat;
