In this tutorial, we will explore how to add outline effects to text using CSS. This can be a useful technique for highlighting important information, such as names or links, and making your text more visually appealing.
There are several ways to achieve an outline effect in CSS, including using the text-shadow
property, the -webkit-text-stroke
property, and SVG. We will cover each of these methods in detail.
Method 1: Using text-shadow
The text-shadow
property can be used to create a simple outline effect by applying multiple shadows to the text. The basic idea is to apply four shadows, one to the top, bottom, left, and right of the text, using the same color and offset.
.strokeme {
color: white;
background-color: white;
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
This will create a basic outline effect, but it can be improved by adjusting the offset and color of the shadows.
Method 2: Using -webkit-text-stroke
The -webkit-text-stroke
property is a more straightforward way to add an outline effect to text. It allows you to specify the width and color of the stroke.
h1 {
font: 800 40px Arial;
-webkit-text-fill-color: transparent;
-webkit-text-stroke: 1px;
}
Note that this property is only supported in WebKit-based browsers, such as Chrome and Safari.
Method 3: Using SVG
SVG can be used to create a more complex outline effect, with features like animation and gradient fills.
svg {
font: bold 70px Century Gothic, Arial;
width: 100%;
height: 120px;
}
text {
fill: none;
stroke: black;
stroke-width: .5px;
stroke-linejoin: round;
animation: 2s pulsate infinite;
}
<svg viewBox="0 0 450 50">
<text y="50">Scalable Title</text>
</svg>
This method provides more control over the outline effect, but it requires more complex code and may not be suitable for all use cases.
Tips and Variations
- To create a thicker outline, you can repeat the
text-shadow
effect multiple times, or increase thestroke-width
in SVG. - To animate the outline effect, you can use CSS animations or JavaScript libraries like GSAP.
- To improve the rendering of the outline effect, you can use
-webkit-font-smoothing: antialiased
to turn off sub-pixel rendering.
In conclusion, adding an outline effect to text with CSS can be achieved using various methods, each with its own strengths and weaknesses. By choosing the right method for your use case, you can create a visually appealing and effective way to highlight important information.