CSS Text Effects: Gradients, Shadows, Strokes, and Underlines
Typography on the web has a paint layer most developers never touch. color is only the start: text can carry multiple shadows with a single declaration, be filled with a gradient or an image, stroked into an outline, rotated into a vertical writing mode, or underlined with a line that draws itself on hover. What makes these effects worth knowing is that they are all properties of ordinary text â no images of words, no SVG replacements for headings (which would be a screen-reader and localization disaster), no JavaScript. The pitfalls are equally real: gradient text that vanishes in high-contrast mode, a stroke with no fallback color, shadows so layered they turn to mud at small sizes. This lesson walks the recipes and the guards that keep them safe.
text-shadow: one declaration, many layers
text-shadow takes x offset, y offset, blur radius, and color â and accepts a comma-separated stack of layers painted back to front, the same convention as box-shadow minus the spread radius. A soft depth shadow is one layer; a glow is two blurs at different radii; a retro extruded block is a series of one-pixel offsets marching diagonally, each without blur:
.soft-depth {
text-shadow: 0 2px 6px #0f172a33;
}
.neon-glow {
color: #22d3ee;
text-shadow:
0 0 8px #22d3eeaa,
0 0 24px #22d3ee66;
}
.extruded {
color: #f8fafc;
text-shadow:
1px 1px 0 #94a3b8,
2px 2px 0 #64748b,
3px 3px 0 #475569,
4px 4px 0 #334155,
0 6px 14px #0f172a55;
}
.misaligned {
/* missing offsets create a halo, not a shadow */
text-shadow: 0 0 4px #0f172a;
}
Shadow color defaults to the current color, so an uncolored shadow follows the text â useful for themeable glows, surprising when you wanted neutral gray. Zero offsets with a blur radius produce a halo rather than a shadow, which is exactly right for emphasis on dark hero sections. The performance note: text shadows repaint on every frame of an animated move or color transition, so keep layers few on text that animates â two or three is a sensible cap.
Gradient and image text with background-clip: text
The signature effect of the last decade â headings filled with a gradient or a photograph â works by clipping the background to the glyphs instead of painting them:
.gradient-heading {
font-size: clamp(2rem, 1rem + 4vw, 3.5rem);
font-weight: 800;
letter-spacing: -0.02em;
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 45%, #ec4899 100%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
color: transparent;
padding-block-end: 0.1em;
}
.photo-heading {
background: #0f172a url("/hero-texture.jpg") center / cover no-repeat;
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
color: transparent;
}
@supports not (background-clip: text) {
.gradient-heading,
.photo-heading {
background: none;
color: #6366f1;
-webkit-text-fill-color: currentColor;
}
}
The mechanics deserve a clear look: the background paints across the element's box, background-clip: text restricts painting to the glyph shapes, and the text fill goes transparent so the clipped background shows through instead of the letters' own color. The -webkit- prefixed properties are the compatibility baseline â still required in practice â and color: transparent covers engines where text-fill-color is unsupported. Two guards: high-contrast and forced-colors modes may override transparent fills with system colors, which is correct behavior (keep essential text readable even when the effect dies), and the fallback @supports block restores a solid brand color so the heading never disappears entirely.
Outline and stroke text
-webkit-text-stroke draws a line along glyph outlines with a width and color, and combined with a transparent fill it produces hollow outline type. It composes with background-clip: text, too â a stroked gradient edge around a transparent interior is a two-line effect that would otherwise need SVG:
.outlined {
font-size: clamp(2.5rem, 2rem + 5vw, 5rem);
font-weight: 900;
color: transparent;
-webkit-text-stroke: 2px #0f172a;
paint-order: stroke fill;
}
.stroke-gradient {
color: transparent;
-webkit-text-stroke: 1.5px transparent;
background: linear-gradient(90deg, #f43f5e, #f59e0b);
background-clip: text;
-webkit-background-clip: text;
}
.subtitle-outline {
color: #e2e8f0;
-webkit-text-stroke: 1px #94a3b8;
}
Because paint-order on text (unlike SVG) has limited support, the stroke paints over the fill by default in most engines â which is why the outlined example empties the fill. The stroke width scales with nothing: it stays fixed as the font grows, so pair it with clamp()-sized headings rather than tiny text where a 2px stroke would swallow the counters.
Decorative and animated underlines
The old text-decoration shorthand is limited: color, thickness, style â but no animation. The trick from the pseudo-elements lesson returns here as the general solution: build the underline from a background-image gradient or a pseudo-element, then animate its size or width:
.link-draw {
color: #1d4ed8;
text-decoration: none;
background-image: linear-gradient(currentColor, currentColor);
background-repeat: no-repeat;
background-size: 0% 0.12em;
background-position: 0 100%;
transition: background-size 0.3s ease;
}
.link-draw:hover,
.link-draw:focus-visible {
background-size: 100% 0.12em;
}
.link-strike {
position: relative;
text-decoration: none;
}
.link-strike::after {
content: "";
position: absolute;
inset-inline: 0;
bottom: -2px;
height: 2px;
background: currentColor;
transform: scaleX(0);
transform-origin: inline-end;
transition: transform 0.25s ease;
}
.link-strike:hover::after,
.link-strike:focus-visible::after {
transform: scaleX(1);
}
The background approach needs no extra element; the pseudo-element approach lets you choose direction (transform-origin: inline-end makes the line draw from right to left, and swapping to center makes it expand from the middle). Modern text-decoration-line also offers underline and overline combinations, and text-underline-offset plus text-decoration-thickness tune native underlines when animation is not required.
Vertical writing modes and rotated text
Two mechanisms handle vertical text, and they are not interchangeable. writing-mode: vertical-rl (or vertical-lr) is real vertical typesetting: glyphs are laid out in vertical columns, CJK characters stand upright, and Latin text rotates 90 degrees by default â adjustable with text-orientation: mixed | upright | sideways. This is what side tabs, book spines, and rotated table headers want, because the browser treats the text as text â wrapping, selection, and assistive output all follow the writing mode:
.sidebar-tab {
writing-mode: vertical-rl;
text-orientation: mixed;
padding: 1rem 0.5rem;
letter-spacing: 0.08em;
}
.book-spine {
writing-mode: vertical-rl;
text-orientation: upright;
letter-spacing: 0.15em;
}
.rotated-label {
display: inline-block;
transform: rotate(-90deg);
transform-origin: center;
}
transform: rotate on horizontal text is the quick alternative â fine for a single decorative badge, wrong for anything that must reflow or be read comfortably, since the layout box stays horizontal while the pixels tilt. Use writing mode when the text is genuinely vertical content; use a rotate when you are tilting an existing horizontal thing.
Fluid display type and wrapping polish
Display text is where effects and sizing meet: a gradient heading looks broken when it wraps awkwardly, so pair effects with text-wrap: balance for headings and text-wrap: pretty for body copy, both cheap one-liners. letter-spacing tightens large sizes (large type wants negative tracking; small caps want positive) and font-variant-numeric: tabular-nums aligns digits in data display. The clamp-based sizing from the units lesson keeps effects proportional â a stroke or extrusion tuned at 64px looks wrong at 24px, so let the size move while the effect remains a fixed visual weight, or scale the shadow offsets proportionally in the same clamp range.
Selection colors and text highlights
One small surface with outsized polish: the color that appears when a user selects your text. ::selection styles any highlighted range, and because browsers historically restricted which properties it honors, the reliable pair is background and color only â a brand-tinted highlight with readable foreground. Set it globally for a themed feel, or locally for a section that wants a different accent:
::selection {
background: #2563eb;
color: #ffffff;
}
.hero ::selection {
background: #f59e0b;
color: #0f172a;
}
.dimmed-notice::selection {
background: #e2e8f0;
color: #334155;
}
The ::highlight() pseudo-element family extends the same idea to programmatic ranges created by the CSS Custom Highlight API â search matches, diff annotations, and autocomplete suggestions can be styled without wrapping the matched text in spans. It is the newer, script-driven cousin of ::selection: where ::selection reacts to whatever the user happens to select, ::highlight(name) targets exactly the ranges your code defined, styled with ::highlight(name) { background: ...; color: ... }. For ordinary pages, a considered ::selection rule alone is enough â it costs two declarations and quietly signals that someone cared about the details. Just verify contrast in both directions: a selection color that reads well over white can disappear over your dark hero, which is exactly why the second rule above flips its foreground.
Common Mistakes
Gradient heading renders invisible or as solid black
Fix: Set all three: background-clip: text (plus the -webkit- version), -webkit-text-fill-color: transparent, and a color fallback with an @supports block for engines without clip support
Text stroke covers the letterforms on small sizes
Fix: Keep stroked effects on display type with clamp() sizing, use hairline widths, and remember stroke width does not scale with font size
Rotating text with transform breaks layout and reading order
Fix: Use writing-mode: vertical-rl for genuine vertical text â rotation is only for decorative single labels that must not reflow
Underline animation never shows because text-decoration cannot animate
Fix: Rebuild the underline as a background gradient or pseudo-element line, then animate background-size or transform
Best Practices
- Cap text-shadow layers on animating text and prefer two-layer glows over heavy stacks
- Provide solid color fallbacks for background-clip and text-stroke effects with @supports
- Let currentColor flow into decorative lines so dark mode themes automatically
- Pair display effects with text-wrap: balance and letter-spacing tuned to the size
- Choose writing-mode over transform rotation whenever the text must wrap or be selected as content
Interview Questions
Q1: How does background-clip: text produce gradient headings?
The element paints a background across its box; background-clip: text restricts painting to the glyph shapes; the fill is made transparent with -webkit-text-fill-color: transparent (and color as backup) so only the background shows through the letters. Prefix and fallback declarations are still needed for broad support.
Q2: What is the difference between writing-mode: vertical-rl and transform: rotate(-90deg)?
writing-mode performs real vertical typesetting â layout, wrapping, and selection happen in the vertical direction, with text-orientation controlling whether glyphs stand upright or lie sideways. rotate transforms the painted pixels while the layout box stays horizontal, so wrapping and surrounding layout do not adapt.
Q3: Why can't text-decoration lines be animated?
text-decoration paints a line derived from the text's decoration properties and is not exposed as an animatable box or value. The workaround is to recreate the underline with a background gradient (animate background-size) or a pseudo-element line (animate width or scaleX), both of which are ordinary animatable properties.
Key Takeaways
- text-shadow stacks comma-separated layers â one declaration gives depth, glow, or extrusion
- Gradient and photo text = background-clip: text plus transparent fill, with @supports fallbacks
- -webkit-text-stroke draws hollow or gradient-edged display type and pairs with clamp() sizing
- Animated underlines come from background-size or pseudo-element transforms, not text-decoration
- writing-mode is true vertical typesetting; transform rotation is decorative only
- Balance and pretty wrapping keep effect-heavy display type polished