Linear gradients

Things are about to get delightfully plaid. Making patterns with CSS linear gradients, one layer at a time.Stripes and checks and plaid, oh my! All from CSS gradients.

Creative Coding

Gradient Patterns

One CSS function, endless possibilities.
Stack enough gradients and you’ll end up with grandma’s gingham.

· 13 min readCSS, Gradients, Patterns

Intro

Welcome to the fun part. This is where we start messing around with angles and layers to turn simple gradients into cool patterns. Every pattern in the grid above is a single div with a few gradients stacked in its background. All drawn with CSS backgrounds on one element.

Each section below has the code plus a closer look at how the pieces fit together. They go from least to most challenging, so start at the top and work your way down. A few of them are a bit tricky, and I still come back here as a reference myself.

The snippets use my site’s color variables. Swap them for your own colors, or use values like #008080 directly.

And these are only a starting point. Half the fun is nudging an angle or a color stop just to see what happens. Sometimes you end up with something totally unexpected and weird, and that’s usually the good stuff.

Quick refresher

Three ideas do most of the heavy lifting. Every pattern here uses a hard stop, which puts two colors at the same position, so instead of a blend you get a crisp edge. Every pattern also repeats, either with repeating-linear-gradient or by shrinking it with background-size so it becomes its own little tile. And most of them lean on angles, which give us diagonal edges. In a square tile, a 45deg gradient with the right hard stops can draw corner triangles. We’ll use those to build checkerboards, diamonds, and zigzags. If any of these feel fuzzy, head back to Linear Gradient 101 where I covered the building blocks.

Stack it

Layering gradients is the key to unlocking patterns. To stack gradients, separate each one with a comma. The first one listed sits on top, and the last one sits on the bottom. For patterns like gingham, part of the trick is leaving parts of a layer transparent so the layers underneath show through.

When using the background shorthand, you can add a solid color at the end of the list. It sits underneath all the gradients and shows through their transparent areas.

Each layer can also get its own settings. Give background-size or background-position a comma-separated list, and each value matches up with the layer in the same spot. That’s how you nudge one layer half a tile over without moving the others. Keep that one in your back pocket. The trickier patterns below lean on it.

Gingham

Two layers, one for the vertical stripes and one for the horizontal. Each is half pink and half transparent, and the pink itself is see-through too, thanks to color-mix at 50%.

Where the stripes cross, the pink doubles up and gets darker. Where only one passes, you get a lighter pink, and where neither does, the cream base peeks through. Three shades out of one color, and that’s the whole gingham look. background-size turns it into a 33px tile so it repeats.

background:
  linear-gradient(90deg, color-mix(in srgb, var(--pink-bold) 50%, transparent) 50%, transparent 0),
  linear-gradient(color-mix(in srgb, var(--pink-bold) 50%, transparent) 50%, transparent 0),
  var(--cream);
background-size: 33px 33px;

Plaid

Plaid is gingham’s fancier cousin. It’s four repeating gradients stacked over a base color. Two of them draw wide, see-through blue bands, one vertical and one horizontal, just like gingham. The other two draw thin lemon lines on top.

Where the translucent blue bands cross, they come out darker, which gives it that woven look. It looks complicated, but it’s really just two stripes, each written twice, once across and once down. The trick is giving every layer the same repeat size, 60px here, so everything lines up. Any size works, as long as every layer matches.

background:
  repeating-linear-gradient(90deg, transparent 0 28px, var(--lemon-soft) 0 30px, transparent 0 60px),
  repeating-linear-gradient(transparent 0 28px, var(--lemon-soft) 0 30px, transparent 0 60px),
  repeating-linear-gradient(90deg, transparent 0 20px, color-mix(in srgb, var(--blue-bold) 50%, transparent) 0 40px, transparent 0 60px),
  repeating-linear-gradient(transparent 0 20px, color-mix(in srgb, var(--blue-bold) 50%, transparent) 0 40px, transparent 0 60px),
  var(--pink-soft);

Graph paper

Graph paper is four layers of lines. Two of them draw a fine grid, one thin line across and one down. The other two draw bolder lines on top.

Each pair gets its own spacing, because background-size takes a comma-separated list, one size per layer, in the same order as the gradients. Here the thin lines repeat every 16px and the bold lines every 80px, so two grids at different scales share one element.

Change the spacing however you like. Just keep the bold spacing a multiple of the fine spacing (80 is five times 16), so every bold line lands right on top of a thin one.

background:
  linear-gradient(var(--periwinkle-bold) 3px, transparent 0),
  linear-gradient(90deg, var(--periwinkle-bold) 3px, transparent 0),
  linear-gradient(color-mix(in srgb, var(--periwinkle-bold) 55%, transparent) 1.5px, transparent 0),
  linear-gradient(90deg, color-mix(in srgb, var(--periwinkle-bold) 55%, transparent) 1.5px, transparent 0),
  var(--lemon-soft);
background-size: 80px 80px, 80px 80px, 16px 16px, 16px 16px;

Checkerboard

Okay, this is where things start to get a little tricky. The secret to a checkerboard is starting with triangles. It’s a bit fiddly, so the steps below show how they come together. I’ve colored the two layers pink and blue so you can tell them apart. In the finished pattern both are teal, so the seams disappear and each pair of triangles becomes one little square.

First, we make a 45deg gradient. Pink fills the first 25%, the middle stays transparent, and pink comes back at 75%. Inside each tile, that gives us two pink triangles in opposite corners.

Next, we add the same gradient in blue and shift it 20px 20px, so it moves 20px right and 20px down. That’s half of our 40px tile in each direction. The triangles from the two layers meet to form squares, and the transparent areas let the lime base show through.

The shift is always half of the background-size. If you bump the tile up to 60px, the shift becomes 30px 30px. If the numbers don’t match, the triangles miss each other and the squares never close.

Layer one
Layer two, shifted half a tile
Both together

Two layers of triangles, one checkerboard.

background:
  linear-gradient(45deg, var(--teal-bold) 25%, transparent 0 75%, var(--teal-bold) 0),
  linear-gradient(45deg, var(--teal-bold) 25%, transparent 0 75%, var(--teal-bold) 0)
    20px 20px,
  var(--lime-soft);
background-size: 40px 40px;

Diamonds

More triangles, arranged a little differently. For the checkerboard, we shifted two matching layers so their triangles met to make squares. Here, we use four gradients to put a triangle in every corner of the tile.

Each gradient is iris up to a hard stop at 25%, with the rest left transparent. The angles 45deg, 135deg, 225deg and 315deg start each one from a different corner, so our 50px tile gets one triangle in every corner.

One gradient
All four in one tile
Four tiles together

The pattern makes more sense once it repeats. Where four tiles meet, their corner triangles join into a diamond. The mint left in the middle of each tile makes another diamond. Two colors taking turns, with the tile edges quietly disappearing.

background:
  linear-gradient(135deg, var(--iris-bold) 25%, transparent 0),
  linear-gradient(225deg, var(--iris-bold) 25%, transparent 0),
  linear-gradient(315deg, var(--iris-bold) 25%, transparent 0),
  linear-gradient(45deg, var(--iris-bold) 25%, transparent 0),
  var(--mint-soft);
background-size: 50px 50px;

Argyle

Argyle builds on the diamonds we just made, adding thin blue lines that crisscross the pattern. The same four gradients form the diamonds, with two extra layers on top for the lines.

Each line is a narrow band of blue between transparent areas, made with hard stops around 50%. One gradient runs at 45deg, the other at -45deg, so the lines cross through the center of each diamond.

background:
  linear-gradient(45deg, transparent calc(50% - 1px), var(--blue-bold) 0 calc(50% + 1px), transparent 0),
  linear-gradient(-45deg, transparent calc(50% - 1px), var(--blue-bold) 0 calc(50% + 1px), transparent 0),
  linear-gradient(135deg, var(--red-soft) 25%, transparent 0),
  linear-gradient(225deg, var(--red-soft) 25%, transparent 0),
  linear-gradient(315deg, var(--red-soft) 25%, transparent 0),
  linear-gradient(45deg, var(--red-soft) 25%, transparent 0),
  var(--marigold-soft);
background-size: 70px 70px;

Zigzag

Zigzag uses the same four corner triangles as the diamonds, just rearranged. When tiles sit side by side, the bottom corner triangles of two neighboring tiles meet at the edge and form one pointy tooth, facing up. That’s why each tooth is two colors, one half from each tile. The top corners do the same thing, making a row of teeth pointing down.

Without a shift, the teeth meet tip to tip, and you’re right back to diamonds. Shift the top pair half a tile to the left with background-position: -30px 0, half of our 60px tile. Now each tooth pointing down drops into the gap between two pointing up, and their slanted edges line up into one continuous zigzag.

Bottom pair
Both pairs, no shift
Top pair shifted 30px left

Same triangles, a little rearranging.

background:
  linear-gradient(135deg, var(--cherry-bold) 25%, transparent 0) -30px 0,
  linear-gradient(225deg, var(--coral-bold) 25%, transparent 0) -30px 0,
  linear-gradient(315deg, var(--cherry-bold) 25%, transparent 0),
  linear-gradient(45deg, var(--coral-bold) 25%, transparent 0),
  var(--sky-soft);
background-size: 60px 60px;

Bricks

Each brick starts as two slanted wedges. Two gradient layers draw them near the edges of a 90px tile, positioned so they meet along a diagonal, and the ivory background shows through the gaps as mortar. I’ve colored the wedges two greens in the steps below so you can see where they meet. In the finished pattern they’re the same green, so each brick looks solid.

Fair warning, this one doesn’t have a neat formula. I picked a tile size for my bricks, then fine-tuned the first row by hand. The first layer sits at 0 3px and the second at 6px 54px. The small numbers leave gaps for the mortar, and the 54px lines the wedges up along the diagonal. I nudged them until they met.

The second row is where the math is exact. Copy both layers and move them half a tile, 45px right and 45px down. That turns 0 3px into 45px 48px, and 6px 54px into 51px 99px.

Because the tile repeats every 90px, 99px lands in the same place as 9px, so we can write that last position as 51px 9px. The joins now fall halfway along the bricks above, giving us that familiar brickwork pattern.

One wedge
Two wedges, one brick
All four layers

Tiny nudges, surprisingly convincing brickwork.

background:
  linear-gradient(335deg, var(--green-bold) 36px, transparent 0) 0 3px,
  linear-gradient(155deg, var(--green-bold) 36px, transparent 0) 6px 54px,
  linear-gradient(335deg, var(--green-bold) 36px, transparent 0) 45px 48px,
  linear-gradient(155deg, var(--green-bold) 36px, transparent 0) 51px 9px,
  var(--ivory);
background-size: 90px 90px;

Pyramids

Each tile holds four triangles in different shades of orange. Together, they look like the faces of a little pyramid.

We build the shading from the bottom up. First, a 45deg gradient with a hard stop at 50% splits the tile diagonally into two dark shades. A 135deg gradient sits above it, covering half the tile in a brighter orange.

The top face takes a little more arranging. Two gradients draw corner triangles at 45deg and 315deg. Shifting both 15px left with background-position: -15px 0 brings those triangles together into one lighter face.

--shade: color-mix(in srgb, var(--orange-bold), black 15%);
--deep: color-mix(in srgb, var(--orange-bold), black 30%);
background:
  linear-gradient(315deg, transparent 75%, var(--orange-soft) 0) -15px 0,
  linear-gradient(45deg, transparent 75%, var(--orange-soft) 0) -15px 0,
  linear-gradient(135deg, var(--orange-bold) 50%, transparent 0),
  linear-gradient(45deg, var(--shade) 50%, var(--deep) 0);
background-size: 30px 30px;

3D cubes

Cubes are the most challenging pattern in this collection because nothing about them is square... They bring in new angles, a tall tile, and six layers that only look like cubes once they overlap. The idea is still familiar, though. Gradients draw flat shapes, and a little shading makes them look 3D.

Two 60deg gradients use hard stops at 25% and 75% to draw light blocks. Four more gradients at 30deg and 150deg draw dark diamonds on top, and those become the tops of the cubes. Because the diamonds sit on top, they cover parts of the light blocks, and what’s left of the light shows up as the right side of each cube. The teal background shows through everywhere else to make the left side.

Light blocks
Dark tops
Both together

To line everything up, half the layers shift 20px right and 35px down. That’s half our 40px by 70px tile in each direction, placing each cube between the two above it.

So where do the odd numbers come from? The angles come from the way cubes are drawn in flat illustrations. Their edges sit 30 degrees off horizontal, which is where 30deg and 150deg come in. That slanted grid also needs a tile about 1.73 times as tall as it is wide. A perfect fit for 40px would be 69.3px, so 70px is close enough.

The stops at 12% and 87.5% are a different story. Like the first row of bricks, they come from a classic recipe that was tuned by eye, not calculated. The tiny half-percent gaps between stops, like 12% to 12.5%, soften the slanted edges a touch.

So yeah, this one’s tricky. My advice? Copy the code below and fine-tune from there :)

--top: color-mix(in srgb, var(--teal-bold), black 20%);
background:
  linear-gradient(30deg, var(--top) 12%, transparent 12.5% 87%, var(--top) 87.5%),
  linear-gradient(150deg, var(--top) 12%, transparent 12.5% 87%, var(--top) 87.5%),
  linear-gradient(30deg, var(--top) 12%, transparent 12.5% 87%, var(--top) 87.5%) 20px 35px,
  linear-gradient(150deg, var(--top) 12%, transparent 12.5% 87%, var(--top) 87.5%) 20px 35px,
  linear-gradient(60deg, var(--teal-soft) 25%, transparent 25.5% 75%, var(--teal-soft) 75%),
  linear-gradient(60deg, var(--teal-soft) 25%, transparent 25.5% 75%, var(--teal-soft) 75%) 20px 35px,
  var(--teal-bold);
background-size: 40px 70px;

Keep going

I love inventing weird, random patterns by taking one of these classics and changing one thing at a time. Move a stop, shift a layer, or stretch the tile and see what happens. It’s also a really good way to figure out what’s doing what and how the puzzle pieces fit together.

Every pattern here is just layers, so you can stack them too. Try the graph paper lines over the diamonds. Just drop the base color from the top pattern so the one underneath shows through.

For another little rabbit hole, try radial-gradient() for polka dots or conic-gradient() for pinwheels. Plot twist! A conic gradient can draw the whole checkerboard in one line.

And if any of this felt fuzzy, head back to Linear Gradient 101 for a refresher on the basics.

Back to topNext · Rejected Plates