Why your CSS gradient looks muddy in the middle (and the one-line fix)
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…
Many people have experienced the issue of their CSS gradients appearing muddy in the middle. This occurs when using the `linear-gradient()` function with colors such as blue and yellow, resulting in a dull grey band. The issue is not with the colors themselves, but with the way browsers blend the gradient stops in the default sRGB colour space. Halfway between a saturated blue and a saturated yellow in sRGB, the colour becomes a desaturated grey.
The solution to this problem is to interpolate the colours in a colour space that provides perceptually even blending, such as OKLab or OKLCH. Modern CSS allows developers to choose the colour space for gradient blending. For example, the following code blends the gradient in OKLab, resulting in a perceptually even gradient with no grey band:
`background: linear-gradient(in oklab 90deg, #1e40ff, #ffd400);`
Alternatively, OKLCH can also be used, which interpolates the hue as an angle, resulting in very vivid gradients:
`background: linear-gradient(in oklch 90deg, #1e40ff, #ffd400);`
The OKLab colour space is recommended for most UI gradients as it keeps lightness changing smoothly. However, OKLCH can be used for more vivid hero backgrounds, although it might be too intense for buttons. It's important to note that the `in` colorspace syntax is supported in current versions of Chrome, Edge, Safari, and Firefox. For older browsers, the plain sRGB line should be included first to ensure a fallback.
In addition to using OKLab or OKLCH, there are other small tips to improve gradient gradients. Adding a hand-picked middle stop, such as a green between blue and yellow, can also eliminate the grey band. Conic gradients are ideal for pie-chart style rings and colour wheels, while repeating-linear-gradient() can create stripes without the need for images.
It's also worth noting that transparent stops, while useful, can darken edges due to sRGB blending rules. Using the same colour at 0 alpha (e.g. `rgb(30 64 255 / 0)`) can fade cleanly in these cases.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.