How it works
Painting letters with a background
CSS can't fill text with a gradient directly: color takes a color, not an image. But a background can be clipped to the shape of the text, and the text's own fill made transparent so the background shows through. The words stay real text.
1. Gradient fill: background-clip: text
background-clip decides how far a background reaches: the border box, the
padding box, the content box, or, with text, only the glyphs. Clipping alone
changes nothing visible, because the text is painted on top of its background in its own
color. Making that fill transparent with -webkit-text-fill-color: transparent lets the clipped gradient show through.
-webkit-text-fill-color rather than color: transparent, because color keeps working: it is the fallback below, the color currentColor refers to, and what forced-colors mode overrides.
@property --angle { syntax: '<angle>'; inherits: false; initial-value: 0deg; }
.headline {
color: #ffb84d; /* fallback */
}
@supports (background-clip: text) or (-webkit-background-clip: text) {
.headline {
--angle: 100deg;
background-image: linear-gradient(var(--angle), #ff4ecd, #ffb84d, #5ee7ff);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
animation: turn 8s linear infinite;
}
.headline::selection {
-webkit-text-fill-color: HighlightText;
background-color: Highlight;
}
}
@keyframes turn { to { --angle: 460deg; } }
@media (prefers-reduced-motion: reduce) {
.headline { animation: none; }
}
@media (forced-colors: active) {
.headline { background: none; -webkit-text-fill-color: currentColor; -webkit-text-stroke: 0; }
} 2. Prefixes and the fallback
The unprefixed background-clip: text is recent in Chromium (version 120) and
Safari (14); before that only -webkit-background-clip: text worked. Firefox has
supported both spellings since version 49, with known gaps: it doesn't work with background-attachment: fixed or in multi-layer backgrounds that mix clip values.
That partial flag is why web-features doesn't list the feature as Baseline. Both spellings are
set here, on a single background layer.
The order of the fallback matters. The transparent fill sits inside @supports (background-clip: text) or (-webkit-background-clip: text), together
with the clip. A browser that can't clip to text therefore never makes the text transparent;
it paints it in the solid color. Without the guard, that browser would show a gradient
rectangle with invisible text inside.
3. Outlines: -webkit-text-stroke and paint-order
-webkit-text-stroke draws a line along the glyph outlines. Despite the prefix it
is standardized in the Compatibility spec and Baseline widely available since 2017. The line is
centered on the outline, so half of it eats into the letter: thin letters get thinner and counters
close up.
paint-order: stroke fill paints the stroke first and the fill on top, so only
the outer half stays visible and the letter keeps its shape. That's why the component sets
twice the Stroke width when Stroke outside is on. For HTML text this has
worked everywhere since March 2024 (Chromium 123); before, it only affected SVG text. With Fill: none the fill is transparent and only the outline is left, so the component never
lets that stroke go to zero.
4. Turning the gradient: @property
The gradient's angle is a custom property, --gt-angle, registered as an <angle>. The animation turns it by 360 degrees, and because the browser
knows the type it interpolates smoothly and recomputes the gradient on every frame. Without
the registration (older browsers, or the stylesheet of a shadow root, where @property is ignored) the value can't be interpolated and the gradient jumps at
the half-way point instead. The aurora page shows that difference
side by side.
Accessibility
- The panel shows the lowest contrast anywhere in the fill against the page. A gradient between two colors can be darker than both in the middle (red to green passes through a dull olive), so the check samples the whole ramp, not just the stops. 3:1 is the minimum for large text; body text needs 4.5:1, which is why gradients belong on headings.
- Selected text would be transparent too. A
::selectionrule paints it in the system highlight colors instead. - In forced-colors mode (Windows high contrast) the gradient and transparent fill are removed, so the text takes the user's colors.
- The rotation stops under
prefers-reduced-motion: reduceand pauses with the button. It is a slow color change, not a flash.
Browser support and performance
Support facts are from web-features 3.44 and MDN's browser-compat-data 8.1.7. Checked in
Chromium 156 and Firefox 157 (both spellings report as supported via CSS.supports() and render); Safari is untested.
- Static, the gradient is painted once like any background. Turning it repaints the text's box on every frame, which is cheap for a heading and adds up for long text.
- A gradient on an inline element that wraps is laid out as if the lines were one long line,
so each line shows a slice of it. Add
box-decoration-break: cloneto give each line the full gradient.