Menu

Why your CSS gradient looks muddy in the middle (and the one-line fix)
📰

Why your CSS gradient looks muddy in the middle (and the one-line fix)

DEV Community: css·GoToolsOnline·1 day ago
#7dzzjICM
#devtocss#devto#dev#gradient#linear
Reading 0:00
15s threshold

If you've ever written linear-gradient(blue, yellow) and got a dull grey-ish band in the middle, the colours aren't wrong; the interpolation is. By default browsers blend gradient stops in sRGB, which isn't perceptually even. Halfway between a saturated blue and a saturated yellow in sRGB is a desaturated grey.

The fix: interpolate in OKLab or OKLCH

Modern CSS lets you choose the colour space the gradient is blended in:

/* default: blended in sRGB, muddy middle */ background : linear-gradient ( 90deg , #1e40ff , #ffd400 ) ;

/* blended in OKLab: perceptually even, no grey band */ background : linear-gradient ( in oklab 90deg , #1e40ff , #ffd400 ) ;

/* blended in OKLCH: goes "around" the hue wheel, very vivid */ background : linear-gradient ( in oklch 90deg , #1e40ff , #ffd400 ) ;

Enter fullscreen mode

Exit fullscreen mode

OKLab keeps lightness changing smoothly, so it's the safe choice for most UI gradients.

OKLCH interpolates hue as an angle, so blue→yellow passes through greens or pinks (you can…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.