The text stroke effect is a visual effect of adding an outline or border to the text. The text-stroke
effect in CSS is represented by a -webkit-text-stroke
property. It is a non-standard property used to add an outline to text. It is primarily supported in WebKit-based browsers (such as Chrome, Safari, and Opera) and is not part of any official CSS specification.
-webkit-text-stroke
The -webkit-text-stroke
CSS property specifies the width
and color
of strokes for text characters. It is a shorthand property for the longhand properties -webkit-text-stroke-width
and -webkit-text-stroke-color
.
Example
.text-stroke {
-webkit-text-stroke: 1px black;
/* text-stroke: 1px black; It's an unknown property for now */
color: white;
}
If you are looking for a simple, cross-browser alternative to text-stroke, you can achieve a similar effect using text-shadow
or SVG text
element.
text-shadow
The text-shadow
property can be used to create an outline effect by layering multiple shadows. The text-shadow
property adds four shadows around the text to create an outline effect.
Example
.text-shadow {
font-size: 48px;
color: white;
text-shadow: -1px -1px 0 black,
1px -1px 0 black,
-1px 1px 0 black,
1px 1px 0 black;
}
SVG text element
SVG (Scalable Vector Graphics) can also create stroked text. stroke
sets the outline color, and stroke-width
determines the thickness of the outline.
Example
<svg>
<text
x="50%"
y="50%"
dominant-baseline="middle"
text-anchor="middle"
font-size="48"
fill="white"
stroke="black"
stroke-width="1"
>
Text Stroke Example
</text>
</svg>
text-shadow
is generally simpler for straightforward text effects, while SVG provides more flexibility and precise control for complex designs.
Full example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Text Stroke Example</title>
<style>
.text-stroke {
color: white;
/*text-stroke: 1px black; It's an unknown property for now */
-webkit-text-stroke: 1px black;
font-size: 48px;
}
svg {
display: block;
width: 410px;
height: 50px;
}
.text-shadow {
font-size: 48px;
color: white; /* Text color */
text-shadow: -1px -1px 0 black, 1px -1px 0 black,
-1px 1px 0 black, 1px 1px 0 black; /* Stroke color */
}
</style>
</head>
<body>
<h1>SVG stroke</h1>
<svg>
<text
x="50%"
y="50%"
dominant-baseline="middle"
text-anchor="middle"
font-size="48"
fill="white"
stroke="black"
stroke-width="1"
>
Text Stroke Example
</text>
</svg>
<h1>-webkit-text-stroke</h1>
<div class="text-stroke">Text Stroke Example</div>
<h1>text-shadow</h1>
<div class="text-shadow">Text Stroke Example</div>
</body>
</html>