coding help
Quick Code


Copy to clipboard
<div style="background: repeating-linear-gradient(to right, LightGray, white 20%);">Text Goes Here</div>
Repeating linear gradients, including smooth and striped examples

Six examples of repeating gradients

Let's say you'd like your gradient to use a repetitive pattern, but you don't want to code something incredibly repetitive. Luckily, programmers love lazy solutions to problems, so CSS has an answer.

Here's an example:

background: repeating-linear-gradient(to right, Teal, SeaGreen, Teal 30%);

Prerequisites

To make repeating gradients, you'll need:

Even if your knowledge isn't perfect, tinkering is a great way to learn. Your sandbox can be handy for experimenting with code.

Instructions

To change a regular gradient into a repeating gradient, you need to do 2 things:

  1. Put the prefix "repeating-" in front of your gradient (either linear-gradient, radial-gradient, or conic-gradient)
  2. Add gradient stops, but don't go all the way to 100%. Just stop when you've put down the first part of your pattern. It'll repeat from there forward.

For example, maybe you want the same pattern to repeat 4 times. Then you only fill out the first 1/4 (25%) of the area. It'll autofill the remaining 75% by repeating the 1/4 3 more times.

Let's show a non-repeating gradient and a repeating version:

Not repeating
background: linear-gradient(to right, lavender, thistle);
Repeating
background: repeating-linear-gradient(to right, lavender, thistle 50%);


That 50% is important: it tells the code where to start repeating. When it reaches the 50% mark, it goes back to our first color (lavender). We didn't put a gradient stop on the lavender because that is optional.

Repeating linear gradients

A repeating linear gradient looks something like this:

<div style="background: repeating-linear-gradient(to left, lavender, pink 20%);">Text goes here.</div>

Here are some examples:

background: repeating-linear-gradient(to right, LightSeaGreen, DarkCyan 33.3%);
background: repeating-linear-gradient(to right, SteelBlue, CornflowerBlue, SlateBlue 50%);


If you want soft transitions, start and end with the same color. For example, "blue, purple, blue."

background: repeating-linear-gradient(to right, LightCyan, PaleTurquoise, LightCyan 50%);
background: repeating-linear-gradient(to right, Sienna, SaddleBrown, Sienna 33.3%);


To get solid stripes, specify multiple color stops per color. For example, "blue 0% 25%, purple 25% 50%."

background: repeating-linear-gradient(to right, BlanchedAlmond 0% 25%, Wheat 25% 50%);
background: repeating-linear-gradient(to right, Seashell 0% 10%, MistyRose 10% 20%);

Repeating radial gradients

A radial gradient starts at the center and radiates outward. A repeating radial gradient can look like ripples.

<div style="background-image: repeating-radial-gradient(color, color, color 50%);">Text goes here</div>


Examples:

background: repeating-radial-gradient(LightCyan, PaleTurquoise 50%);
background: repeating-radial-gradient(Seashell 0% 20%, MistyRose 20% 40%);

The same principles about soft transitions and stripes apply.

Conic Gradient (with div)

A conic gradient starts at the center and twirls outward, like a color wheel.

<div style="background: repeating-conic-gradient(color, color, color 50%);">Text goes here</div>

Examples:

background: repeating-conic-gradient(azure 0% 25%, PowderBlue 25% 50%);
background: repeating-conic-gradient(white, bisque, white 33.3%);

Repeating even more with background-size

You can add the CSS background-size property to make tiling backgrounds. You specify a background size using px, %, or something else. Then the background repeats/tiles to fill the space.

When you specify background size, use two numbers or percentages. The first one is for width and the second is for height. For example:

<div style="background-size: 25% 50%">The background tiles are 25% wide and 50% tall.</div>

Here are some examples of how it looks with gradients:

background: repeating-conic-gradient(white 0% 25%, cornsilk 25% 50%); background-size: 25% 50%;
background: repeating-radial-gradient(LightCyan, PaleTurquoise, LightCyan 50%); background-size: 50px 50px;

This can get overwhelming quickly, so make sure you're following best practices.

Best practices

See also: Accessibility

Always think about readability and accessibility. You want your designs to be easy on the eyes. This means:

Bad:
VISUAL CHAOS!!!
Better:
Simple & subtle

Specify your fallbacks. Sometimes, browsers display things differently. Plan ahead:

See also