CSS Text Stroke: How to Outline Text in CSS
Contents
To add a stroke (an outline) to text in CSS, use -webkit-text-stroke with a width and a color: -webkit-text-stroke: 2px black;. Despite the prefix, it works in every modern browser: Chrome, Edge, Firefox and Safari. The unprefixed text-stroke property doesn’t exist in any browser yet, so always write the -webkit- version.
Demo:
Stroke
Outline
Thick
Basic text stroke
The shorthand takes the stroke width and color. You can also set them separately with -webkit-text-stroke-width and -webkit-text-stroke-color.
h1 {
color: white;
-webkit-text-stroke: 2px #3b82f6;
}
/* Same thing, longhand */
h1 {
-webkit-text-stroke-width: 2px;
-webkit-text-stroke-color: #3b82f6;
}
Outlined (hollow) text
Make the fill transparent and keep only the stroke. -webkit-text-fill-color overrides color for the fill, which is handy when the element’s color is used elsewhere, like for currentColor borders.
.outline {
color: transparent;
-webkit-text-stroke: 2px #111827;
}
/* or keep `color` and override only the fill */
.outline {
-webkit-text-fill-color: transparent;
-webkit-text-stroke: 2px currentColor;
}
Thick strokes without eating the letters
The stroke is centered on the edge of each letter, so half of it covers the inside of the glyph. Thick strokes make letters look thin and blobby. paint-order: stroke fill paints the stroke first and the fill on top, so only the outer half shows. Double the width to get the visible thickness you want.
.thick {
color: #facc15;
-webkit-text-stroke: 10px #3b82f6; /* shows as 5px outside */
paint-order: stroke fill;
}
Fill the text on hover
Outlined text that fills in on hover is a popular heading effect. Transition the fill color.
.hover-fill {
color: transparent;
-webkit-text-stroke: 2px #3b82f6;
transition: color 0.3s ease;
}
.hover-fill:hover {
color: #3b82f6;
}
Text stroke vs text-shadow
Before -webkit-text-stroke worked everywhere, the usual trick was four sharp text shadows, one in each direction. It still has a use: shadows sit outside the letters by default and can be blurred, so you can build a soft outline or a glow. For a crisp outline, -webkit-text-stroke is cleaner and follows curves better.
Outline with text-shadow
Want to tweak shadow outlines, glows and 3D text visually? Use the CSS text shadow generator and copy the CSS or Tailwind class.
.shadow-outline {
color: white;
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
}
FAQ
Is -webkit-text-stroke safe to use?
Yes. Despite the prefix, it’s supported in all current browsers, including Firefox, and it’s part of the Compatibility Standard, so browsers have committed to keeping it.
Why does my stroke make the letters look thinner?
The stroke is drawn on the center of the letter edges, so half of it goes inward. Add paint-order: stroke fill to keep only the outer half.
Can I use a gradient as the stroke?
Not directly: -webkit-text-stroke only takes a solid color. For a gradient outline, use SVG text with a gradient stroke, or layer a gradient-clipped copy of the text behind it.
Is outlined text accessible?
Hollow text has very little contrast, because the reader only gets a thin line. Use it for large display headings, not for body text, and check that the stroke color contrasts with the background.