Overview

In CSS, pseudo-elements are keywords that allow you to style specific parts of an element. They enable the ability to apply styles to certain parts of a document that are not directly accessible through the DOM (Document Object Model). Pseudo-elements are used to manipulate the content of an element without altering the document's structure or adding extra HTML elements.

The two most commonly used pseudo-elements are ::before and ::after, but there are several others as well. Understanding and using pseudo-elements effectively can make your styling more powerful and help reduce unnecessary HTML markup.

Syntax

The basic syntax of a pseudo-element is as follows:

selector::pseudo-element {
    property: value;
}

It's important to note that two colons (::) should be used to indicate a pseudo-element. The single colon (:) is typically used for pseudo-classes (like :hover), but for backward compatibility with older versions of CSS, a single colon can still be used with certain pseudo-elements like ::before and ::after.

Common Pseudo-elements

Here are the most common pseudo-elements in CSS:

::before

The ::before pseudo-element inserts content before the content of the selected element.

Example:

h1::before {
    content: "📌";
    margin-right: 8px;
}

In this example, a pin emoji will appear before the text of every h1 element.

::after

The ::after pseudo-element inserts content after the content of the selected element.

Example:

h1::after {
    content: "⭐";
    margin-left: 8px;
}

Here, a star emoji will appear after the text of every h1 element.

::first-letter

The ::first-letter pseudo-element is used to apply styles to the first letter of an element's content.

Example:

p::first-letter {
    font-size: 2em;
    font-weight: bold;
}

In this case, the first letter of every p element will be displayed in a larger and bolded font.

::first-line

The ::first-line pseudo-element applies styles to the first line of text inside a block-level element.

Example:

p::first-line {
    color: blue;
    font-weight: bold;
}

This will style the first line of every p element with blue color and bold font.

::selection

The ::selection pseudo-element is used to style the portion of a document that is selected by the user.

Example:

::selection {
    background-color: #3399ff;
    color: white;
}

In this example, the selected text will have a blue background and white text.

Use Cases

Pseudo-elements are useful for a variety of scenarios, including:

Browser Compatibility

As of 2025, most modern browsers support pseudo-elements. However, some older versions of browsers might have partial or no support for certain pseudo-elements, such as ::first-letter, ::first-line, and ::selection. Here's a quick compatibility summary:

Browser ::before ::after ::first-letter ::first-line ::selection
Chrome Yes Yes Yes Yes Yes
Firefox Yes Yes Yes Yes Yes
Safari Yes Yes Yes Yes Yes
Edge Yes Yes Yes Yes Yes
Internet Explorer No No No No No

You can use feature queries or CSS fallbacks to ensure graceful degradation for unsupported browsers.

Best Practices

When using pseudo-elements in your CSS, it's important to follow certain best practices to maintain clean, accessible, and efficient code. While they offer powerful capabilities for styling, improper use can lead to maintainability issues or unexpected behavior. Keep the following tips in mind:

Conclusion

Pseudo-elements in CSS provide an efficient and elegant way to enhance the visual design of your website. Whether you're adding decorative icons, styling specific parts of text, or customizing the appearance of selected text, pseudo-elements help you achieve more with less HTML. By understanding and leveraging the full potential of pseudo-elements, you can create cleaner, more maintainable, and visually appealing styles.