Gradient Text in CSS: How background-clip: text Works (and When to Avoid It)
Gradient-filled headlines are everywhere in modern product marketing: AI startups, developer tools and app landing pages love them. The effect is striking and, unlike images of text, it keeps headings as real text that search engines can index and users can select and translate. It relies on one slightly unusual CSS property and a couple of legacy prefixes.
How it works
CSS has no color: linear-gradient(...). Text color must be a single color. The trick is to paint the gradient as the element’s background and then cut that background down to the shape of the letters:
.gradient-text {
background: linear-gradient(90deg, #8a6d3b, #e0a96d, #141414);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
color: #8a6d3b; /* fallback */
}Step by step:
backgroundpaints a gradient behind the text, filling the element’s box.background-clip: texthides the background everywhere except where the glyphs are.-webkit-text-fill-color: transparentmakes the letters themselves transparent, so the clipped background shows through them.
The gradient text generator above writes this code with up to three colors, any angle, and a size and weight preview.
Why the prefixes are still there
background-clip: text started life as a WebKit-only feature. Other browsers implemented it under the -webkit- name for compatibility, and the unprefixed version became widely supported only relatively recently. Including both lines costs nothing and ensures it works everywhere, including older Safari and Chromium versions.
-webkit-text-fill-color is used instead of color: transparent for a reason: if the clipping isn’t supported, color stays as a solid fallback, and the text is still visible. If you set color: transparent and clipping fails, the text vanishes.
A safer fallback with @supports
For maximum safety, only apply the transparency when the browser supports text clipping:
.gradient-text { color: #8a6d3b; }
@supports (background-clip: text) or (-webkit-background-clip: text) {
.gradient-text {
background: linear-gradient(90deg, #8a6d3b, #e0a96d);
-webkit-background-clip: text; background-clip: text;
-webkit-text-fill-color: transparent;
}
}Gradient text compared with other approaches
| Approach | Real text | Scales | Searchable | Effort |
|---|---|---|---|---|
| CSS background-clip | Yes | Yes | Yes | Low |
| SVG text with a gradient fill | Yes (inside SVG) | Yes | Mostly | Medium |
| Image of text | No | No (fixed resolution) | Only via alt text | Low, but bad practice |
| Gradient mask over text | Yes | Yes | Yes | Medium |
CSS is the right default. Images of text should be avoided: they can’t be resized cleanly, translated or reflowed, and WCAG 1.4.5 asks you to use real text where possible.
Multi-line headings
By default, the background covers the element’s whole box, so a two-line heading shows the gradient stretched across both lines as one shape. Often that’s what you want. If you’d rather each line have its own full gradient, set the element to display: inline and add box-decoration-break: clone (with -webkit-box-decoration-break: clone for Safari), which repeats the background for each line fragment.
Shadows don’t work the normal way
Because the text fill is transparent, a text-shadow shows through the letters and muddies the gradient. To add depth, use filter: drop-shadow(0 2px 4px rgb(0 0 0 / 0.25)) on the element, which shadows the visible shape. Our text shadow guide covers other shadow techniques.
Readability and accessibility
Contrast varies along the gradient. WCAG contrast is measured between the text color and the background, but gradient text has many colors. The safe approach is to check the lightest (or least contrasting) color in the gradient against the page background with a contrast checker. If any part of the text falls below 3:1 (for large headings), some letters will be hard to read.
Keep it for large text. Gradients need area to be visible. Thin or small text shows so little gradient that it just looks like slightly inconsistent color. Use bold weights (700+) at large sizes.
Forced colors mode. In Windows High Contrast (forced colors) mode, backgrounds are replaced, which can make clipped text invisible. Adding @media (forced-colors: active) { .gradient-text { -webkit-text-fill-color: CanvasText; background: none; } } restores normal text.
Selection highlighting can look odd on gradient text in some browsers; that’s cosmetic, but test it.
Picking colors for gradient headlines
- Analogous colors (neighbours on the color wheel) give smooth, premium-looking gradients. Complementary colors can produce a muddy middle; see our gradient guide for why.
- Use one gradient headline per page. Its effect depends on rarity.
- Match your brand. A gradient from your primary color to a lighter tint keeps the effect on-brand.
- Consider dark mode. A gradient designed for a light background may need lighter colors on dark.
- Animate carefully. Animating
background-positionon an oversized gradient creates a shimmering effect; keep it slow and honour reduced-motion preferences.
Theming with custom properties
Hard-coding gradient colors makes them painful to change across a site. Store the stops in custom properties instead:
:root { --grad-a: #8a6d3b; --grad-b: #e0a96d; }
.gradient-text { background: linear-gradient(90deg, var(--grad-a), var(--grad-b)); }
@media (prefers-color-scheme: dark) { :root { --grad-a: #e0a96d; --grad-b: #f6d5b5; } }Now dark mode, seasonal campaigns or per-product accent colors only need two new values. The same variables can feed buttons, borders and illustrations so the gradient feels like part of the system rather than a one-off effect.
Where gradient text works best
| Context | Good fit? | Why |
|---|---|---|
| Hero headline on a landing page | Yes | Large, bold, seen once |
| Logo-style wordmark in a header | Yes | Short and distinctive |
| Section headings throughout an article | Rarely | Repetition dilutes the effect and slows scanning |
| Navigation links and buttons | No | Interactive text needs clear, predictable color |
| Body text and captions | No | Too small for the gradient to read, and harder to read |
A useful test: if you removed the gradient and used the darkest stop as a solid color, would the page lose anything important? If the answer is no for most headings, keep the gradient for just the one that matters most.