Using Gradients in UI Without the Muddy Middle
· 4 min read
Why gradients turn gray in the middle, how OKLCH interpolation fixes it, and how to keep text readable on gradients and photos without banding.
Gradients come and go in design trends, but they never quite leave. Used well, they add depth, guide the eye and make a flat layout feel finished. Used badly, they turn muddy, band into visible stripes or make text unreadable. This guide covers the technical and visual decisions behind gradients that work on real interfaces.
The muddy middle
Blend two colors that sit far apart on the color wheel and the middle of the gradient often turns gray or brown. Here is the classic example, blue to yellow:
.hero {
background: linear-gradient(90deg, #0000FF, #FFFF00);
}By default, CSS mixes colors in sRGB, channel by channel. Halfway between blue (0, 0, 255) and yellow (255, 255, 0) is (128, 128, 128): flat gray. The colors on both ends are vivid, but the path between them cuts straight through the neutral center of the color space.
Fix it with a better color space
Modern CSS lets you choose the interpolation space. In OKLCH, the gradient moves around the hue wheel while keeping chroma, so the middle stays colorful:
.hero {
background: linear-gradient(90deg in oklch, #0000FF, #FFFF00);
}Now the midpoint is a bright teal instead of gray. All current major browsers support the in oklch syntax. Other options are worth knowing:
in oklabmixes in a straight line through a perceptual space. It avoids the darkening of sRGB but can still pass near gray for opposite hues.in oklch longer huetakes the long way around the wheel, which gives rainbow-like gradients.- For colors that are already close in hue, sRGB and OKLCH look almost the same. The choice matters most for distant colors.
Another approach, which also works in older browsers, is to add a middle stop by hand. Pick a color between the two that you actually want to see, and the gradient will pass through it.
Keep hues close for UI
Large gradients spanning opposite colors are bold and suit hero sections, posters and illustrations. Inside an interface — on buttons, cards and panels — gradients usually work better when the two ends are neighbors on the color wheel, or two lightness levels of the same hue. They add depth without becoming the main thing on the screen.
.panel {
background: linear-gradient(180deg, #F7F5F0, #EFECE4);
}A background gradient this subtle is barely noticeable as a gradient. It just makes the panel feel lit from above rather than flat. The color harmony guide explains which hue relationships feel calm and which feel energetic.
Text on gradients
Contrast must hold at every point the text touches, not just on average. Check the label color against both ends of the gradient, and against the middle if the stops are far apart.
| Gradient end | White text | Result |
|---|---|---|
#4338CA (indigo) | 7.90:1 | Passes AAA |
#DB2777 (deep pink) | 4.60:1 | Passes AA |
#F472B6 (light pink) | 2.65:1 | Fails |
An indigo-to-deep-pink button is safe for white text along its whole length. Swap the end for a lighter pink and the right side of the label fails, even though the left side looks fine. Use the contrast checker on the lightest end for light text, or the darkest end for dark text.
Text over photos: the scrim
Photos are the hardest background of all because their contrast changes from pixel to pixel. A scrim — a gradient from translucent black to transparent laid over the image — creates a predictable dark zone for the text without hiding the whole picture.
.card {
background:
linear-gradient(to top, rgb(0 0 0 / 0.65), rgb(0 0 0 / 0) 60%),
url("photo.jpg") center / cover;
color: #FFFFFF;
}Fading over the bottom 60% keeps the top of the photo clean. If the text still struggles on bright photos, raise the opacity or add a soft text shadow as a backup.
Gradient text
Clipping a gradient to text is a popular way to emphasize a headline:
.headline {
background: linear-gradient(90deg, #4338CA, #DB2777);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}Keep it to large display text, and make sure every color in the gradient meets contrast against the background. Avoid it for body text or links, where uneven color makes reading harder.
Banding
On large areas, a gradient between two similar colors can show visible steps, because 8-bit color has only so many values between the two ends. Ways to reduce it:
- Increase the color difference slightly, or shorten the area the gradient spans.
- Interpolate in OKLCH or OKLab, which distributes the steps more evenly to the eye.
- Overlay a very faint noise texture to dither the steps. This is also why many hero gradients look grainy on purpose.
Linear, radial and conic
- Linear gradients suit backgrounds, buttons and overlays. Angles between 135° and 180° feel like natural light from the top.
- Radial gradients create glows and spotlights. Placing a soft radial highlight behind a product or headline draws the eye to it.
- Conic gradients rotate around a point. They are useful for pie charts, color wheels and loading rings, and less so for backgrounds.
Try it
The gradient generator lets you drag color stops, switch between linear, radial and conic, and toggle OKLCH interpolation to compare it with sRGB. It exports CSS, a Tailwind class or SVG, or a PNG. For finished examples, browse the gradient library.