Linear gradients

A closer look at my hardest-working CSS function. Getting comfortable with color stops, direction, and turning smooth blends into crisp stripes.Getting comfy with directions, color stops, and turning smooth blends into crisp stripes.

Creative Coding

Linear Gradient 101

A beginner’s guide to linear-gradient,
the CSS function that does a lot more than fade.

· 6 min readCSS, Gradients, Basics

Intro

The hardest-working function in my CSS toolbox is linear-gradient(). If you’ve explored the knick-knacks on my landing page, you’ve already seen it at work. It draws the mesh on the radio speakers, sandwiches jam between the cake layers, and lines the pages of the books. Throughout this post, I’ll pull in little examples from my CSS art so you can see each technique in practice.

A gradient blends colors, but you get to decide where those blends happen. Bring two different color stops together, and the soft transition becomes a crisp edge. Repeat those edges, and suddenly you’re drawing stripes.

The spool from my sewing machine, drawn with gradients.

Here, we’ll cover the building blocks: directions, color stops, hard stops, and a couple of ways to repeat a gradient. Once you’ve got those down, head over to Gradient of Possibilities to see how far you can push it. Gingham, diamonds, checkerboards and more!

The anatomy

Picture a straight line running across your element. That’s the gradient line. linear-gradient() places colors along it, and the browser blends between them. Each color along the line stretches out perpendicular to it, filling the background.

One detail worth knowing early on is that a gradient isn’t a color, it’s an image. That’s why it goes on background or background-image. If you put it on background-color, nothing happens. Here’s a gradient running from blue to sky, left to right, with the result underneath.

background: linear-gradient(to right, var(--blue-bold) 20%, var(--sky-soft));
The CSS for a gradient from blue to sky, running left to right, with the gradient it draws underneath. Its parts are described in the list below.

Hover over a part below, or tab to it, to highlight it in the example.Click a part below to highlight it in the example.

Try it out

Each color stop says where its color sits at full strength, and the browser blends across the gap between them. Drag the two stops toward each other and watch the blend shrink. What happens when they meet?

linear-gradient(to right, var(--blue-bold) 30%, var(--sky-soft) 70%)
30%70%

Once the first stop meets or passes the second, poof! The fade is gone. There’s no room left to blend, so you get a sharp edge instead. This is called a hard stop.

Two colors

The simplest gradient needs just two colors. Pass them to linear-gradient and the browser blends from one to the other. It runs top to bottom by default, so there’s no need to specify a direction. We’ll try other directions next.

background: linear-gradient(var(--sky-soft), var(--blue-bold));

Direction and angles

To send the blend in another direction, add a keyword before the colors. Point it at a side with to top, to right, to bottom or to left, or at a corner with to top left, to top right, to bottom left or to bottom right.

background: linear-gradient(to right, var(--sky-soft), var(--slate-bold));

For more control, use an angle. 0deg points up, and angles turn clockwise from there. That makes 90deg the same as to right and 180deg the default top to bottom.

One small surprise: 45deg and to top right aren’t always the same. They actaully only match on a square. An angle keeps its exact tilt whatever the shape of the box. A corner keyword adjusts its angle to fit, so the blend always runs from one corner to the opposite one. So on a square they match, but on a wide box they don’t.

45deg
to top right
/* 45deg */
background: linear-gradient(45deg, var(--sky-soft), var(--slate-bold));

/* to top right */
background: linear-gradient(to top right, var(--sky-soft), var(--slate-bold));

Color stops

You’re not limited to two colors! Add as many as you like and the browser spaces them evenly unless you specify positions. Each color, together with its optional position, is a color stop. The browser blends between them. Here, blue stays solid through 20%, blends into sky at 50%, then into slate at 80%.

background: linear-gradient(
  to right,
  var(--blue-bold) 20%,
  var(--sky-soft) 50%,
  var(--slate-bold) 80%
);

Hard stops

This is where things start to get interesting. Put two different color stops at the same position and there’s no room to blend. One color ends exactly where the next begins, leaving a crisp edge. That’s a hard stop. I use these a lot in my CSS art to draw separate bands of color inside a single element. It saves me from stacking up extra divs.

In the example below, blue ends at 50% and sky starts at 50%, so they meet with nothing in between. This gives us two solid halves. A handy shortcut is to write 0 for sky’s position. A stop can’t come before the one ahead of it, so 0 really means “start right where the last color ended.”

The split can happen anywhere. On my landing page, the shelves use 0 70% for the wood, followed by a contrasting color at 0. The browser bumps that stop to 70%, leaving the last 30% for the shelf’s edge. You’ll spot this shortcut all over my CSS.

background: linear-gradient(to right, var(--blue-bold) 50%, var(--sky-bold) 50%);

Repeating gradients

Use hard stops to make two solid stripes, then let repeating-linear-gradient repeat the pair across the element. It draws your stops once, then keeps repeating them until it runs out of room.

Here, sky fills 0 to 12px and blue fills 12 to 24px. Blue’s 0 is the hard stop shortcut from above, so it starts right where sky ends. At 24px the pattern starts over with sky again. Since the first stop is at 0 and the last is at 24px, each repeat is 24px long. The 45deg at the front just tilts the stripes.

background: repeating-linear-gradient(
  45deg,
  var(--sky-soft) 0 12px,
  var(--blue-bold) 0 24px
);

Tiled gradients

There’s another way to repeat a gradient. Use background-size to give a gradient a smaller area to fill, like a tile. Backgrounds repeat by default, so the browser fills the remaining space with copies.

This one is a single hard stop, half slate and half sky. At 40px wide it repeats into stripes.

background: linear-gradient(to right, var(--slate-bold) 50%, var(--sky-soft) 0);
background-size: 40px 100%;

Keep this technique handy. In the next post, Gradient of Possibilities, nearly every pattern starts as one small tile like this. Gingham, checkerboards and diamonds are all a few gradients drawn on a tile that the browser repeats.

Keep going

That’s everything you need to start messing around. Nudge a stop, tilt an angle, squish some stripes. Tiny tweaks take the same gradient somewhere completely new, and stacking a few on top of each other is where things get delightfully plaid.

A few places to go from here:

  • Make some patterns. Head over to Gradient of Possibilities to turn these basics into cool patterns like gingham, checkerboards, diamonds and plaid.
  • Go gradient spotting. Poke around the knick-knacks on my landing page and see how many gradients you can spot. Fair warning, I’m a little heavy-handed with them.
  • Meet the rest of the family. linear-gradient has siblings! radial-gradient spreads color out from a point in circles or ovals, and conic-gradient sweeps color around a center like a pie chart. They use the same color stops and hard stops you just learned, and each has a repeating- version too.
    radial, soft
    radial, hard stops
    conic, soft
    conic, hard stops
Back to topNext · Gradient of Possibilities