How To Add Text Outline using CSS (r)

Jul 8, 2023
CSS text outline

Share the news on

In this post, we look at ways to alter the font's outline with CSS and then look into the ways it could be utilized together with other choices available.

Understanding Web Texts

Web Text can be described as text displayed on web pages. Fonts are an essential element when it comes to displaying text online. They are basically images that are vector based, so the pixels are not limited in their capacity to display in various sizes and still maintain the clarity and sharpness of their images.

The most fascinating thing about the vector graphics used in fonts is the capability to create lines, or strokes over characters. Like how you draw outlines around the shape of characters in vector software like Adobe Illustrator, CSS provides the ability to produce the same results with web-based fonts.

Two Methods to Add Text Outline With CSS

In the case of adding the look of an outline to your text with CSS, there are two methods to use it. Let's explore and see the negatives of these techniques and ways to apply these strategies.

1. Making use of the Text-Strike Property

Text-stroke text-stroke is one CSS property that is used to make an outline of the text. It is possible to specify the outline's dimensions as well as the shade. This feature is supported only by browsers that use WebKit, and for you to use it, you have to include the webkit- prefix.

For an example, we can make a stroke of an h1 head text--"Welcome to ":

Hello and welcome to

This is how the text-stroke property will be used. "text-stroke" property can be used with the webkit prefix:

h1 -webkit-text-stroke-width: 3px; -webkit-text-stroke-color: black; 

-webkit-text-stroke-width and -webkit-text-stroke-color specify the stroke's width and color, respectively. The code sets the stroke's width in the range of 3px and the hue from black from black to black.

Adding outline with the text-stroke property
The incorporation of an outline is done using the text-stroke property.

The two properties can be used together with the property of shorthand webkit-text stroke that simultaneously defines stroke the color and width as well as the stroke's width.

h1 -webkit-text-stroke: 3px black; 

The identical output.

Support for Text-Strike Property

Based on Caniuse the website, there's no support to support this "text-stroke property on Internet Explorer. Internet Explorer browser.

Support for the text-stroke property
Text-stroke support for property.

If you're using your text-stroke property to set the text's outline and your user is making use of a browser that isn't compatible the text might not appear in the event that the color is not compatible with the color of the background.

To fix this, you can use the -webkit-text-fill-color property to set a color for the text and set a fallback color with the color property:

h1 color: black; -webkit-text-fill-color: white; /* Will override color (regardless of order) */ -webkit-text-stroke: 3px black; 
Adding support for unsupported browsers
Adding support for unsupported browsers.

If a browser doesn't allow text-strokes, and a browser is not able to support this stroke-text property will use the color that is specified by the property's the color property.

Fallback when the browser is unsupported
If the browser you are using doesn't work.

Another alternative is to confirm whether your browser can support the -webkit-text-stroke property before you add the style.

@supports (-webkit-text-stroke: 3px black) h1 -webkit-text-fill-color: white; -webkit-text-stroke: 3px black; 

2. Utilizing the property text-shadow

If you're not trying to deal with support variations, you can use this Text-shadow property which comes with the ability to work with every recent versions of all used browsers.

Support for the text-shadow property
Support for the property text-shadow.

To apply the text-shadow feature to apply the text-shadow property, we'll apply four different shadows each to the top left middle, the left top, the lower left, and the bottom left.

h1 color: #fff; text-shadow: 3px 3px 0 #000, -3px 3px 0 #000, -3px -3px 0 #000, 3px -3px 0 #000; 

The shadows serve to make a text outline effect. Each shadow is 3 pixels offset from the text. Additionally, the color of each shadow is set to deep black ( #000). The results are the same that was created using the method previously used.

Adding outline with the text-shadow property
The property text shadow could be utilized to draw an outline. property.

When we apply shadows to all 4 corners it creates a clean outline. It is your choice to change the offsets of pixels and shadow color or text colors according to your personal style preferences.

This technique offers an additional benefit because you are able to adjust the horizontal and vertical shadows based on the kind of text. Also, you can include the blur radius

h1 color: #fff; text-shadow: 3px 3px 2px #000, -3px 3px 2px #000, -3px -3px 0 #000, 3px -3px 0 #000; 
Add Blur to outline with the text-shadow property
Include Blur as an outline by through the text-shadow property.

One limitation of using text shadows is there is a possibility of the effect of a continuous stroke when the shadow length exceeds 1 pixels (you'll observe this if you look at it in relation to text-stroke method). text-stroke method).

Text-stroke Properties that include text-shadow and text-stroke properties

You can combine both properties to create a amazing effect that blends an ideal outline of the text with soft blurs along with other effects that are available through the Text-Shadow property. The result is a adaptable and customized method of improving the appearance of text.

h1 -webkit-text-stroke: 1px black; color: white; text-shadow: 3px 3px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000; 
Combining text-stroke and text-shadow to create an outline
Combining text-stroke with shadow to create an outline.

Also, it is possible to eliminate the need to apply separate shadows to corners and then create a general shadow using one line:

#heading-1 color: white; -webkit-text-stroke: 1px #F8F8F8; text-shadow: 0px 1px 4px #23430C; #heading-2 color: white; -webkit-text-stroke: 1px #F8F8F8; text-shadow: 0px 2px 4px red; #heading-3 color: #333; -webkit-text-stroke: 1px #282828; text-shadow: 0px 4px 4px #282828; 
More outline examples implementation with text-stroke and text-shadow
More outline examples implementation with Text-Shadow and Text-stroke.

Summary

The text-stroke as well as the shadow text properties are useful for creating outlines for text. Which one to use is contingent on your browser's compatibility, the desired effects as well as the degree of control you require for your layout.

     Please share your story! Are you using different strategies that aren't mentioned in the article? Let us know about it in the comments.

This post was posted on here