CHAPTER 30

CSS Pseudo-elements

Learn how pseudo-elements such as ::before and ::after can add and style generated content.

01. What is CSS Pseudo-elements?

Pseudo-elements allow you to style a specific part of an element.
::before and ::after can generate decorative content.
Pseudo-elements are useful for visual effects without adding extra HTML elements.

02. Syntax

selector::before { content: ""; property: value; } selector::after { content: ""; property: value; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>

<style>

.title {

    position: relative;

    display: inline-block;

    color: #2563eb;

}

.title::after {

    content: "";

    position: absolute;

    left: 0;

    bottom: -8px;

    width: 60%;

    height: 3px;

    background: #06b6d4;

}

</style>

</head>

<body>

<h1 class="title">
    AIFA Coding Lab
</h1>

</body>
</html>

04. Important Points

✓ ::before and ::after commonly require the content property.
✓ Pseudo-elements are useful for decorative UI elements.
✓ They can be positioned using position: absolute.
✓ Use them for decoration rather than important semantic content.

05. Common Mistakes

⚠ Forgetting the content property.
⚠ Putting important information only inside generated content.
⚠ Using pseudo-elements when a real HTML element is more appropriate.

06. Real World Use

Pseudo-elements are commonly used for animated underlines, badges, decorative shapes, overlays, quotation marks and visual effects.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Create a Heading Underline

Create a heading and use ::after to create a colored underline below it.