Dev.to WebDev 🛠 Dev 👁 0 📖 1 min read

Why your CSS gradients look muddy in the middle (and the one-word fix)

Blend blue into yellow with linear-gradient(90deg, blue, yellow) and the middle turns a dull grey. That isn't your colours' fault: by default browsers mix gradient colours in sRGB, where the straight line between two col

Blend blue into yellow with linear-gradient(90deg, blue, yellow) and the middle turns a dull grey. That isn't your colours' fault: by default browsers mix gradient colours in sRGB, where the straight line between two colours often passes through a desaturated zone.

The fix: interpolate in OKLab or OKLCH

.muddy  { background: linear-gradient(90deg, blue, yellow); }
.clean  { background: linear-gradient(in oklab 90deg, blue, yellow); }
.vivid  { background: linear-gradient(in oklch 90deg, blue, yellow); }
  • oklab is perceptually even, so the midpoint looks like a real halfway colour.
  • oklch travels around the hue wheel, giving brighter, rainbow-like transitions (use shorter hue / longer hue to choose the direction).

Keep a fallback

Colour-space interpolation is supported in current Chrome, Edge, Firefox and Safari, but declare the plain version first so older browsers still get a gradient:

.card {
  background: linear-gradient(90deg, blue, yellow);
  background: linear-gradient(in oklab 90deg, blue, yellow);
}

Two more tips

  • Add a middle stop instead of fighting the blend: linear-gradient(90deg, blue, #6aa 50%, yellow).
  • conic-gradient() works great for pie charts and colour wheels: conic-gradient(red 0 25%, gold 0 60%, teal 0).

Try it

Disclosure: I run GoToolsOnline. Its free CSS gradient generator does linear, radial and conic gradients with draggable stops and an OKLab/OKLCH option, and writes the fallback line for you: https://gotoolsonline.com/tools/css-gradient

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.