CSS
π― What is repeating-linear-gradient()?
repeating-linear-gradient() creates a linear gradient that automatically repeats itself at specific intervals.
When one gradient section ends, the same pattern starts again.
π§© Simple Definition
repeating-linear-gradient() = creates a repeating linear gradient pattern
π§© Basic Syntax
background: repeating-linear-gradient(
direction,
color1,
color2,
...
);
π§ Parameters
1. Direction
Defines the direction of the gradient.
Common Values
| Value | Direction |
|---|---|
| to right | Left β Right |
| to left | Right β Left |
| to bottom | Top β Bottom |
| to top | Bottom β Top |
| 45deg | Diagonal gradient |
| 90deg | Vertical angle |
2. Colors and Stops
Each color can have a length value that determines:
- where the color starts
- where it ends
- how often the pattern repeats
Example:
red 0px,
yellow 20px
π This means the color transition lasts for 20 pixels.
π§© Basic Examples
1. Simple Horizontal Stripes
background: repeating-linear-gradient(
to right,
red,
yellow 20px,
red 40px
);
π§ Result
- red and yellow stripes repeat continuously
- the pattern repeats every 40 pixels
πΉ 2. Diagonal Repeating Gradient
background: repeating-linear-gradient(
45deg,
blue,
lightblue 15px,
blue 30px
);
π§ Result
Creates diagonal blue stripes repeating every 30 pixels.
πΉ 3. Thin Line Pattern
background: repeating-linear-gradient(
to bottom,
#ddd,
#ddd 1px,
white 1px,
white 20px
);
π§ Result
Creates notebook-style horizontal lines.
π¨ Practical Example
Stylish Striped Background
body {
background: repeating-linear-gradient(
45deg,
#222,
#222 10px,
#333 10px,
#333 20px
);
}
π Creates a modern diagonal stripe effect.
βοΈ How Repetition Works
The gradient repeats based on the final stop position.
Example:
red 20px,
blue 40px
π The full pattern size becomes 40px.
After 40px, the same gradient repeats again.
β οΈ Difference Between linear-gradient() and repeating-linear-gradient()
| Property | Behavior |
|---|---|
| linear-gradient() | Gradient appears only once |
| repeating-linear-gradient() | Gradient pattern repeats infinitely |
π§© Combining with Other Background Properties
repeating-linear-gradient() can be combined with:
| Property | Purpose |
|---|---|
| background-size | Controls pattern size |
| background-position | Controls placement |
| background-blend-mode | Adds visual effects |
π§ Common Use Cases
This function is commonly used for:
- striped backgrounds
- grid designs
- line textures
- decorative UI elements
- warning patterns
- modern abstract designs
π¨ Example: Warning Stripe Pattern
background: repeating-linear-gradient(
45deg,
yellow,
yellow 20px,
black 20px,
black 40px
);
π Creates a caution-style striped pattern.
β οΈ Important Note
Because gradients are generated directly by CSS:
- β no image file is required
- β gradients scale automatically
- β they are lightweight and responsive
This improves website performance.
