CSS Text Decoration
Welcome to the world of CSS Text Decoration! This tutorial will guide you through the basics of using text decorations – a powerful feature that allows you to add visual flair to your web pages. Text decorations are a fantastic way to highlight important elements, create visual interest, and enhance the overall aesthetic of your designs. They’re a fundamental building block for more complex styling techniques.
💻 Understanding the Basics
Text decorations are created using the ::before and ::after pseudo-elements. These elements are placed before or after the regular text content of an element, effectively adding a decorative element to the display. They’re incredibly versatile and can be used to create everything from subtle highlights to bold, dramatic effects.
🎨 The ::before Pseudo-Element
The ::before pseudo-element is the most commonly used text decoration. It creates a new element that’s inserted before the existing text. It’s essentially a shadow, but with a distinct visual appearance.
.my-element::before {
content: "This is a highlighted text!";
color: red;
font-weight: bold;
position: absolute; /* Important for positioning */
top: 20px;
left: 20px;
width: 20px;
height: 20px;
background-color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */
border-radius: 50%; /* Makes it a circle */
}
In this example, we've added a red, bold text element before the text "This is a highlighted text!" using the ::before pseudo-element. The position: absolute; is crucial; it allows us to precisely control the element's placement relative to the parent element. The width and height define the size of the decoration, and background-color sets the color. border-radius: 50%; makes it a circle.
🚀 The ::after Pseudo-Element
The ::after pseudo-element is similar to ::before, but it inserts a new element after the existing text. This is useful when you want to add an element that's not directly part of the original text.
.my-element::after {
content: "A small icon";
color: blue;
font-size: 12px;
position: absolute;
left: -10px; /* Position it to the left */
width: 10px;
height: 10px;
background-color: blue;
border-radius: 50%;
}
Here, we've added a blue icon before the text "This is a highlighted text!" using the ::after pseudo-element. The left: -10px; moves the icon to the left, creating a subtle visual effect.
💡 Tip: Using ::before and ::after
- Placement:
::beforeis generally preferred for adding decorative elements that should be positioned before the text.::afteris better for elements that are added after the text. - Positioning:
position: absolute;is essential for controlling the exact placement of the element. You can also useposition: relative;to position the element relative to its nearest positioned ancestor. - Combining with other properties: You can combine
::beforeand::afterwith other CSS properties likecontent,color,font-weight,font-size,margin,padding, andborderto create complex and visually appealing effects.
💻 Practice Exercises
- Simple Highlight: Add a red, bold text decoration to the text "Hello World!" using the
::beforepseudo-element. - Icon Placement: Add a blue icon (using a placeholder image) to the text "My Website" using the
::afterpseudo-element. - Circular Element: Create a circular element (using the
::beforepseudo-element) with a green background and add it to the text "Important Information" usingposition: absolute;andleft: -10px;. - Text with a Border: Add a border around the text "This is a test" using the
::afterpseudo-element and set the border color to orange.
🖥️ Try It Yourself
- Highlight: Modify the text "Welcome to the site" to include a red, bold text decoration using the
::beforepseudo-element. - Icon: Add a blue icon to the text "New Product" using the
::afterpseudo-element. - Circular Element: Create a circular element with a green background and a blue border using the
::afterpseudo-element. - Border: Add a border around the text "Contact Us" using the
::afterpseudo-element and set the border color to yellow.

