CSS Gradient Text: How to Create Colorful Text Effects
Abhay khant
Jan 1, 1970 • 11 min read
CSS gradient text paints a color gradient across your letters instead of a flat fill. The technique is short: set a gradient as the element's background-image, clip that background to the shape of the glyphs with background-clip: text, then make the text itself transparent so the gradient shows through. Per MDN's background-clip reference, the text value clips a painted background to the foreground text, which is exactly the effect you want.
The minimal recipe is four lines, and it covers every case:
.gradient-text {
background-image: linear-gradient(90deg, #6366f1, #ec4899);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
That block paints an indigo-to-pink fade, crops it to the letters, and hides the solid text color underneath. The rest of this guide explains why each line is there, why the -webkit- prefix still matters, and the one fallback that keeps your text readable when the trick fails.
Key Takeaways
- CSS gradient text is a
background-clip: texttrick, not a real text-color property (MDN).- Paint a gradient
background-image, clip it totext, then set the fill transparent.- The
-webkit-background-clip: textprefix is still required across major browsers (caniuse).- Declare a solid
colorfirst as a fallback, or unsupported browsers show invisible text.
What Is Gradient Text and How Does It Work?
Gradient text is a background effect clipped to letter shapes, not a text-color feature. CSS color accepts only one flat color value per element, so no direct gradient-color property exists. Instead you paint a gradient behind the text and crop it to the glyphs, which MDN's background-clip documentation defines as the text value clipping the background to the foreground text.
Three declarations do the work together. The background-image holds the gradient itself, any linear-gradient or radial value you like. The -webkit-background-clip: text, plus the unprefixed background-clip: text, tells the browser to paint that background only where the letters are. And color: transparent removes the normal solid fill so the clipped gradient underneath becomes visible.
Think of it as a stencil. The gradient is a sheet of colored paper, the text is a cut-out mask, and background-clip: text presses the mask over the paper so color shows only inside the letters. Nothing about the font changes, only what fills its shapes. Because the fill is a real CSS gradient, it scales, animates, and re-colors with the same freedom as any background. The mechanics are identical to a plain background gradient, covered in the full CSS gradients guide.
The reason there is no dedicated property is historical. Text has always been painted with a single color, and CSS never added a gradient equivalent for the foreground. WebKit introduced the background-clip: text value years ago as a workaround, the spec adopted it, and it became the standard way to get colored letters. Every gradient-text snippet you find online is a variation on that same clip.
Why Is the -webkit- Prefix Still Required?
The -webkit-background-clip: text prefix is required because the text keyword never left the vendor-prefixed stage in some engines. Caniuse's gradient and clip data shows the value is supported almost everywhere, yet the unprefixed background-clip: text still needs its -webkit- twin to render in Chrome, Safari, and Chromium builds.
Write both lines, prefix first. Presence matters more than order: list -webkit-background-clip: text and background-clip: text together so every engine finds the version it understands. Chromium and WebKit read the prefixed one; standards-compliant parsers read the plain one and ignore what they do not recognize.
The cost of skipping the prefix is invisible text, not a graceful downgrade. Without -webkit-background-clip: text, a WebKit or Chromium browser still applies the transparent fill but never paints the clipped gradient, so the letters vanish. The prefixed line is what makes the gradient appear at all in those engines, which is why it stays in every production snippet.
One prefix, both spellings, and you are covered across the modern field. The clip value ships in current Chrome, Edge, Safari, and Firefox once the -webkit- version is present, and caniuse's support table confirms the coverage is effectively universal for the browsers most sites target. Treat the prefixed and unprefixed pair as a single unit and never ship one without the other.
Why Do You Need a Fallback Color?
A fallback color is mandatory because color: transparent makes text invisible wherever the clip fails. Any browser that ignores background-clip: text, plus print stylesheets and some reader modes, renders transparent letters on transparent space. The CSS color value spec treats transparent as fully see-through, so nothing shows.
Transparent text is not readable text. That is the core pitfall of this effect: the moment the gradient does not paint, your headline disappears instead of falling back to a plain color. Screen readers still read the markup, so content is not lost to assistive tech, but sighted users on an unsupported path see blank space where your words should sit.
The fix is one line, placed first. Declare a solid color before you set the fill transparent, so the cascade has a value to fall back on:
.gradient-text {
color: #6366f1; /* fallback if the clip fails */
background-image: linear-gradient(90deg, #6366f1, #ec4899);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
Two details make this bulletproof. Pick a fallback color from inside the gradient so the downgrade still looks intentional. And prefer -webkit-text-fill-color: transparent over color: transparent for the hidden fill, since a supporting browser honors text-fill-color while non-supporting browsers ignore it and keep your solid color. CSS-Tricks' gradient walkthrough shows the same layered-safety pattern for background gradients.
How Do You Write Gradient Text?
Writing gradient text takes five short steps, and a visual tool skips most of the syntax. ToolSura's CSS gradient generator builds the gradient value for you, then you drop it into the clip recipe below. By hand, the sequence is quick and always the same.
- Set a fallback
colorfirst, ideally a hex pulled from the gradient, so unsupported browsers show readable text. - Add a
background-imagewith your gradient, such aslinear-gradient(90deg, #6366f1, #ec4899). - Clip the background to the letters with both
-webkit-background-clip: textandbackground-clip: text. - Make the fill transparent using
-webkit-text-fill-color: transparentso the gradient shows through. - Preview in a WebKit browser and one print or fallback path to confirm the text stays visible either way.
Each property has one job, and reading them as a set makes the recipe easy to remember:
| Property | Job |
|---|---|
color | Fallback fill for unsupported browsers |
background-image | Holds the gradient itself |
background-clip: text | Crops the gradient to the glyph shapes |
-webkit-text-fill-color | Turns the visible fill transparent |
Change the gradient in step two and everything else stays put. Swap linear-gradient for a radial or conic value, add more color stops, or animate the background-position, and the clip keeps cropping it to your letters. The recipe is fixed; the gradient inside it is yours to design, and the core gradient syntax works the same here as on any background.
A Multi-Stop Gradient Example
More stops mean more color travel across the letters. Adding intermediate colors to the background-image builds a richer fade without touching the clip lines, since MDN's linear-gradient reference accepts as many color stops as the design needs.
.rainbow-text {
color: #ef4444;
background-image: linear-gradient(
90deg,
#ef4444,
#f59e0b,
#10b981,
#3b82f6,
#8b5cf6
);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
Five stops spread across the line, so the letters run red through amber, green, and blue into violet. The clip lines never change. Only the gradient grows, which is the whole reason to keep the recipe fixed and treat the color as a separate decision.
Animating Gradient Text
Animate the position, not the gradient. Gradient values do not interpolate between keyframes, so a transition on background-image produces no motion. The working pattern oversizes the background with background-size, then animates background-position so the fade slides through the clipped letters.
.animated-text {
color: #6366f1;
background-image: linear-gradient(90deg, #6366f1, #ec4899, #6366f1);
background-size: 200% auto;
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
animation: slide 3s linear infinite;
}
@keyframes slide {
to { background-position: 200% center; }
}
Repeating the first color at the end makes the loop wrap cleanly, with no visible jump when it restarts. Keep the animation short and the surface small, since a moving gradient repaints every frame and large animated text can strain the paint step.
Gradient Text on Headings vs Inline Text
Gradient text behaves best on block elements like headings, where the gradient spans the full box the browser paints. On an <h1> or <h2>, the background-image fills the element's width and the fade reads across the whole line. That is the ideal case for the clip, since MDN's background-clip page crops the painted background to whatever glyphs sit inside that box.
Every line box clips its own copy of the background. When a heading wraps onto two lines, the browser paints the gradient fresh inside each line's box, so the fade restarts at the left edge of every line instead of flowing continuously down the block. Short lines show a compressed slice of the gradient; long lines show more of it. This per-line reset is expected behavior, not a bug, and it follows directly from how the text clip treats each fragment.
Inline elements need one adjustment. A <span> inside a paragraph occupies only the width of its own text, so the gradient clips to that narrow box, and a short word may show just a thin slice of the fade. Set display: inline-block on the span when you want the gradient to size to the word cleanly and to accept any width or padding you add. Without it, the inline box hugs the text tightly and the gradient has almost no room to travel.
Each element clips independently, so two separate headings never share one continuous gradient across the page. Keep gradient text for display type: titles, logos, and callouts, where the letterforms are wide enough to show the color move. At body sizes the fade compresses into a few pixels per glyph and reads as a muddy single color. For related fills, the gradient transparency guide covers alpha effects, and the gradient border guide handles the same clip-style workaround applied to edges.
Common Gradient Text Mistakes
The most common mistake is shipping without a fallback color, which leaves blank space in any browser that ignores the clip. MDN's background-clip reference notes the text value depends on engine support, so a missing solid color shows transparent letters wherever that support is absent. Three errors cause most broken gradient headlines.
Forgetting the fallback color is the first, and the fix is the single color line from the recipe above. Declare a solid color pulled from inside the gradient before the transparent fill, so an unsupported browser or a print stylesheet renders readable letters instead of nothing at all.
Animating the gradient directly is the second. An animation on background-image produces no movement, because gradients render as the image type MDN's gradient documentation describes, and image values do not interpolate. Oversize the background-size and animate background-position instead, which slides the fade through the clipped text at almost no cost.
Ignoring contrast is the third, and the most damaging for readability. A gradient that dips into a pale color against a light background can drop below usable contrast partway across a word. Choose stops that clear a solid contrast ratio against the page, test the lightest stop honestly, and keep the effect on large display type where thin, low-contrast strokes do the least harm.
Related Tools & Further Reading
Gradient text is one technique in a larger toolkit. Start with the fundamentals, then reach for the sibling techniques when a plain background fill is not enough for the design.
- CSS Gradients Explained: Linear, Radial, and How to Use Them
- CSS Gradient Transparency: Alpha Channels and Fade Effects
- CSS Gradient Border: Workarounds and Modern Solutions
- Reference: MDN background-clip and caniuse gradient support.
- Tool: CSS gradient generator to build and copy the gradient value.


