Web development and Tech news Blog site. WEBISFREE.com

HOME > css

Learn how to implement text outline in CSS

Last Modified : 02 Mar, 2023 / Created : 03 Mar, 2023
509
View Count

I am trying to add an outline(border) style to text using CSS. There are several ways to do it, such as adding an outline or shadow effect, so I will explore these methods and think about which one suits the effect I want.



# Methods for implementing CSS outline or shadow effect


Adding an outline or shadow effect to text is used to emphasize it more than other texts. Typically, if there are core keywords, we use this method to highlight them. For now, I have classified them into three ways:

  • Applying a shadow effect: text-shadow
  • Applying an outline effect: -webkit-text-stroke
  • Implementing an outline using a shadow effect (for browser compatibility purposes)

These three methods are the most commonly used methods. We will compare the advantages and disadvantages of using CSS and explore how to implement each method first.


!Adding shadow effect, text-shadow.


The greatest advantage of this method is its simplicity and speed of implementation. However, a disadvantage is that while it allows for adjusting the shadow's position in all directions, it also produces a blurry effect, which limits its customizability. Nonetheless, it is recommended for quick and simple application.

Below is a simple implementation. Compared to other methods, the code is also short and concise.

<hr />
<span id="test1">ABCDEF</span>
<hr />
<style>
#test1 {
font-size: 60px;
text-shadow: 2px 2px 4px red;
}
</style>


If you look at the implemented code above, you can see that the red shadow effect has been implemented. The actual code is as follows.
<span id="test1">ABCDEF</span>

<style>
#test1 {
  font-size: 60px;
  text-shadow: 2px 2px 4px red;
}
</style>

Then, let's explore the next method.

This time, the method using the -webkit-text-stroke style property in CSS to add outlines. It was not widely used before due to browser compatibility issues as it is only compatible with IE and Webkit engines. However, it is still frequently used. The method is simple. Just set the length and color, so it's easy. However, it may lack some variety in implementing various effects. Below is an example of implementation.

<hr />
<span id="test2">ABCDEF</span>
<hr />
<style>
#test2 {
font-size: 60px;
-webkit-text-stroke: 2px red;
}
</style>


The implemented code is as follows.
<span id="test2">ABCDEF</span>

<style>
#test2 {
  font-size: 60px;
  -webkit-text-stroke: 2px red;
}
</style>

Now, this is the method using text-shadow multiple times, which is the last method.


! Implementing outlines using text-shadow multiple times


This method has been frequently used as a CSS trick for a long time. Since using text-shadow only once has many limitations and does not produce diverse effects, the purpose is to implement the desired effect by using multiple repetitions of text-shadow. Of course, the disadvantage is that the code becomes more complex and takes more time. However, above all, the text-shadow property has high browser compatibility and is still widely used. Let's take a look at the code.
<hr />
<span id="test3">ABCDEF</span>
<hr />
<style>
#test3 {
font-size: 60px;
text-shadow: -1px -1px 0 red, 1px -1px 0 red, -1px 1px 0 red, 1px 1px 0 red;
}
</style>

Below is the code that implements the above example.
<span id="test3">ABCDEF</span>

<style>
#test3 {
  font-size: 60px;
  text-shadow: -1px -1px 0 red, 1px -1px 0 red, -1px 1px 0 red, 1px 1px 0 red;
}
</style>

Looking at the applied code above, the main difference is in the use of commas to implement each style in multiple directions, unlike the first method. This is the key difference. Furthermore, it is possible to use this technique multiple times to achieve different lengths of outlines or to implement outlines or inlines in desired ways.


@ Tips and tricks 
In addition, there is another way to apply the same effect. It involves using two separate text elements with identical phrases, with one of them being outlined or shadowed. This method may not be necessary in most cases but is often used when the implementation of shadow or outline effects is difficult or when animation is required.

So far, we have briefly explored the methods of applying outline or shadow effects. As it is a simple and well-known method, it is good to remember it briefly.
Perhaps you're looking for the following text as well?

    Previous

    Preprocessor Sass/SCSS to CSS compilation transformation

    Previous

    How to apply line break hyphens to alphabet text in CSS, hyphens