The ::before and ::after Pseudo-element in CSS

The CSS ::before and ::after pseudo-elements are used to insert content before and after an element's actual content, respectively. These pseudo-elements create virtual elements that can be styled and positioned like regular HTML elements.

Syntax

element::before {
    content: "text or value";
    /* other styles */
}

element::after {
    content: "text or value";
    /* other styles */
}

Key Points

  • The content property is required for pseudo-elements to appear
  • Content can be text, images, or empty strings
  • Pseudo-elements are inline by default but can be styled as block elements
  • They inherit from their parent element

Example 1: Using ::after Pseudo-element

The following example adds content after paragraph elements −

<!DOCTYPE html>
<html>
<head>
<style>
    p {
        background-color: #2196F3;
        color: white;
        padding: 10px;
        margin: 5px 0;
    }
    p::after {
        content: " ?";
        background-color: #FF5722;
        padding: 5px;
        font-weight: bold;
        margin-left: 10px;
    }
</style>
</head>
<body>
    <h2>Favorite Sports</h2>
    <p>Football</p>
    <p>Basketball</p>
</body>
</html>
Two blue paragraphs appear with "Football" and "Basketball" text, each followed by a red star symbol with orange background.

Example 2: Using ::before Pseudo-element

The following example adds content before paragraph elements −

<!DOCTYPE html>
<html>
<head>
<style>
    .quote {
        background-color: #f0f0f0;
        padding: 15px;
        margin: 10px 0;
        border-left: 4px solid #4CAF50;
    }
    .quote::before {
        content: "? ";
        font-size: 1.2em;
        color: #4CAF50;
    }
</style>
</head>
<body>
    <div class="quote">Success is not final, failure is not fatal.</div>
    <div class="quote">It is the courage to continue that counts.</div>
</body>
</html>
Two gray quote boxes appear with green left borders, each prefixed with a green speech bubble emoji.

Example 3: Creating Decorative Elements

This example demonstrates creating a simple tooltip using both pseudo-elements −

<!DOCTYPE html>
<html>
<head>
<style>
    .tooltip {
        position: relative;
        display: inline-block;
        background-color: #007BFF;
        color: white;
        padding: 10px;
        cursor: pointer;
        border-radius: 5px;
    }
    .tooltip::before {
        content: "Click me for info";
        position: absolute;
        bottom: 125%;
        left: 50%;
        transform: translateX(-50%);
        background-color: #333;
        color: white;
        padding: 8px;
        border-radius: 4px;
        font-size: 14px;
        opacity: 0;
        transition: opacity 0.3s;
    }
    .tooltip::after {
        content: "";
        position: absolute;
        bottom: 115%;
        left: 50%;
        transform: translateX(-50%);
        border: 5px solid transparent;
        border-top-color: #333;
        opacity: 0;
        transition: opacity 0.3s;
    }
    .tooltip:hover::before,
    .tooltip:hover::after {
        opacity: 1;
    }
</style>
</head>
<body>
    <p>Hover over the button below:</p>
    <div class="tooltip">Hover Me</div>
</body>
</html>
A blue button appears. When hovered, a dark tooltip with white text and a small arrow appears above it.

Conclusion

The ::before and ::after pseudo-elements are powerful tools for adding decorative content and UI elements without cluttering your HTML. They require the content property and can be styled just like regular elements.

Updated on: 2026-03-15T14:01:06+05:30

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements