How to Make Outlined Text in CSS with Stroke and Shadow

Author

Posted Oct 22, 2024

Reads 382

Close-Up Shot of a Text on White Paper
Credit: pexels.com, Close-Up Shot of a Text on White Paper

To create outlined text in CSS, you can use the `text-shadow` property to add a shadow effect around the text. This property allows you to specify the color, distance, and blur radius of the shadow, all of which can be adjusted to achieve the desired outlined effect.

The `text-shadow` property is a simple and effective way to create outlined text, and it's supported by most modern browsers. By experimenting with different values, you can achieve a range of outlined text effects, from subtle to dramatic.

To add a stroke effect to your text, you can use the `box-shadow` property in combination with the `text-shadow` property. This allows you to specify a separate shadow effect for the text itself and the surrounding box, creating a more nuanced and detailed outlined effect.

By combining these two properties, you can achieve a wide range of outlined text effects, from simple and elegant to complex and intricate.

For another approach, see: Create Css Selector from Webpage

Adding Stroke to CSS

Credit: youtube.com, Adding Stroke to Text - Css Quick Tips & Tricks - Add outline or stroke to fonts using CSS

Adding stroke to your text using CSS can be done in several ways, and we're going to explore two popular methods.

You can use the text-stroke property, which is supported by WebKit-based browsers, to add an outline to your texts. This property allows you to specify the outline's width and color, and it can be used with the -webkit- prefix.

The text-stroke property can be used with the -webkit-text-stroke-width and -webkit-text-stroke-color properties to specify the stroke's width and color, respectively. For example, you can use the following code to add a 3px black stroke to an h1 heading text: -webkit-text-stroke-width: 3px; -webkit-text-stroke-color: black.

Combining the text-stroke property with the text-shadow property can also create a visually stunning effect that combines a perfect text outline with a subtle blur.

Here are the two methods to add text outline with CSS:

  • Using the text-stroke property
  • Combining text-stroke and text-shadow properties

You can also add a stroke to your text using SVG filters, which can be defined in the SVG element and applied to the text using the filter property. This method can be used to create a hollow text or outlined text.

Credit: youtube.com, How to Add Border in Text in HTML and CSS | Text Stroke HTML CSS

Here's a summary of the methods to add stroke to text:

Remember, you can use these methods to add a stroke to your text in a variety of browsers, including Chrome and Firefox.

Using SVG Filters

Using SVG filters is a great way to add a stroke to your text. You can define an SVG filter that uses the feMorphology and feComposite elements to create the effect.

The SVG filter definition has two steps: dilating the text with a radius of 2 pixels and performing a XOR operation to subtract the original text from the enlarged text, resulting in a hollow text or outlined text.

To change the thickness of the text stroke, you can modify the radius attribute in the SVG filter, and to change the color, simply change the color CSS property in the stroke-text class. However, keep in mind that this method can look a bit coarse for bigger values of the radius.

Here's an interesting read: Text Color Css Code

Credit: youtube.com, Introduction to SVG Filters

Here are the steps to create a stroke-text-svg-filter SVG filter:

  • feMorphology with dilate takes the original text and “dilates” or enlarges the text in all directions by a radius of 2 pixels
  • feComposite with the operator="xor" performs a XOR operation which actually subtracts from the enlarged text the original text, such that we are left with a hollow text or outlined text

Add Stroke to SVG Filters

To add stroke to text using SVG filters, you can define SVG filters that you can then use in CSS with the filter property.

The stroke-text-svg-filter SVG filter has two steps: feMorphology with dilate enlarges the text in all directions by a radius of 2 pixels, and feComposite with the operator="xor" performs a XOR operation which subtracts from the enlarged text the original text, leaving a hollow text or outlined text.

To change the thickness of the text stroke, modify the radius attribute in the SVG filter. To change the color of the text stroke, simply change the color CSS property in the stroke-text class.

This method of adding stroke to text has a downside: the end result looks a bit coarse for bigger values of the radius, because the dilation algorithm tries to expand the letters in all directions, which might not render a true outline equally sized in all directions.

Credit: youtube.com, Using SVG Filters to Recreate a Neumorphic Logo Design

You can use a heart or arrow symbol in the text, or any emoji or text symbol you like.

Here's a quick summary of the steps to add stroke to text using SVG filters:

  • Define the SVG filter with two steps: feMorphology with dilate and feComposite with the operator="xor"
  • Modify the radius attribute to change the thickness of the text stroke
  • Change the color CSS property in the stroke-text class to change the color of the text stroke

Combining Stroke and Shadow Properties

Combining stroke and shadow properties can create a visually stunning effect. You can combine the -webkit-text-stroke property with the text-shadow property to achieve a perfect text outline with a subtle blur and additional effects.

The text-stroke property is only supported by WebKit-based browsers, and you must add the -webkit- prefix to use it. However, you can use the text-shadow property, which has support for all the latest versions of popular browsers.

By combining both properties, you can create a versatile and customizable approach to enhancing your text's appearance. You can also do away with having to add individual shadows to each corner and apply a general shadow with one line.

This combination allows for a smooth and well-defined outline, similar to that generated by the text-stroke method. However, using text shadows may encounter a discontinuous stroke effect when the shadow length exceeds 1 pixel.

You can use both a stroke and a shadow to create a great effect, like tossing on a WebKit stroke, the all-around text-shadow stroke, as well as a deeper text-shadow stroke.

Expand your knowledge: Css Typing Effect for Html Text

Ann Predovic

Lead Writer

Ann Predovic is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for research, she has established herself as a go-to expert in various fields, including technology and software. Her writing career has taken her down a path of exploring complex topics, making them accessible to a broad audience.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.