Skip to main content
April 1, 2026Dan Rodney/5 min read

Pseudo-Elements & the Content Property

Master CSS Pseudo-Elements for Dynamic Content Creation

CSS Pseudo-Elements Fundamentals

Virtual Elements

Pseudo-elements create fake elements that render content without adding HTML markup. They're perfect for decorative content and styling enhancements.

Content Property

The content property defines what appears in pseudo-elements. It can include text, special characters, and even empty strings for styling hooks.

Before and After

The most common pseudo-elements that insert content before or after an element's existing content, ideal for icons and decorative elements.

Topics Covered in This HTML & CSS Tutorial:

Mastering Pseudo-elements, Leveraging the Content Property, and Debugging with Chrome's DevTools

Exercise Preview

preview pseudo elements

Exercise Overview

While HTML is the standard vehicle for adding content to web pages, CSS pseudo-elements offer powerful alternatives for specific use cases. In this exercise, you'll discover how to strategically use CSS to inject content before or after elements—a technique that separates presentation from structure and creates more maintainable, scalable code. This approach is particularly valuable for adding decorative elements, icons, or consistent visual cues across your site without cluttering your HTML markup.

Why Use CSS for Content?

While HTML is the standard way to add content, CSS pseudo-elements are powerful for decorative elements, icons, and styling enhancements that shouldn't clutter your markup.

Getting Started

  1. We'll be switching to a new folder of prepared files for this exercise. In your code editor, close all open files to avoid confusion and maintain a clean workspace.
  2. For this exercise we'll be working with the Tahoe Pseudo-Elements folder located in Desktop > Class Files > Advanced HTML CSS Class. Open that folder in your code editor if it allows you to (like Visual Studio Code does).
  3. Open index.html from the Tahoe Pseudo-Elements folder.
  4. Preview index.html in Chrome (we'll be using its DevTools for debugging).

    This page builds upon the previous exercise, but notice the links at the bottom of each column are positioned on their own line. This intentional spacing creates visual hierarchy and provides the perfect canvas for adding sophisticated styling touches. We want to add a special character to all three of the Read More links using pure CSS—demonstrating how pseudo-elements can create consistent visual elements across multiple instances without repeating code.

  5. Leave the page open in Chrome for real-time preview as we make changes.

Exercise Setup Checklist

0/4

Pseudo-Elements & the Content Property

Now we'll dive into the practical implementation of pseudo-elements, exploring how they create virtual DOM elements that enhance your design without affecting your HTML structure.

  1. First let's identify our target elements. Go to index.html in your code editor.
  2. Find the Read More link at the bottom of the first section.
  3. Notice it has a read-more class we can use to style it. This class-based approach ensures our pseudo-element styling can be applied consistently across all similar links.
  4. Open main.css from the css folder (in the Tahoe Pseudo-Elements folder).
  5. Above the .book-now-btn rule add the following new rule

    .read-more::before {
    
    }

    The ::before selector creates a pseudo-element—a virtual element that renders content inside the anchor tag before all existing content. This technique is invaluable for adding decorative elements, icons, or visual cues that enhance user experience while keeping your HTML semantic and clean. Pseudo-elements are rendered as inline elements by default and must include a content property to appear.

  6. Specify the content to add by including the code shown below in bold. Pay close attention to spacing—the space after the character is crucial for proper visual separation. To type » use Opt–Shift–\ (Mac) or hold down ALT while typing code 0187 on the numeric keypad (Windows).

    .read-more::before {
       content: "» ";
    }
  7. Save the file and reload the page in Chrome.
  8. Examine the Read More links to see they now feature the special » character at the beginning. This visual enhancement was achieved without touching the HTML, demonstrating the separation of concerns that makes CSS so powerful.
  9. Return to your code editor to explore the alternative placement option.
  10. Change before to after to position the character at the end of the link:

    .read-more::after {
       content: "» ";
    }
  11. Move the space from before the » character to after it for proper spacing:

    .read-more::after {
       content: " »";
    }
  12. Save the file and reload the page in Chrome. Now the » character should appear at the end of each link, creating a more traditional "read more" indicator that users intuitively understand as a call-to-action.

Creating Your First Pseudo-Element

1

Target the Element

Use the .read-more class to select the links that need enhancement.

2

Add Pseudo-Element Selector

Use ::before to create a virtual element that renders before the existing content.

3

Define Content Property

Add the content property with your desired character or text, including proper spacing.

4

Test and Adjust

Switch between ::before and ::after, adjust spacing and characters as needed.

Special Character Input

To type the » character: Mac users press Opt-Shift-\, Windows users hold ALT and type 0187 on the numeric keypad.

Seeing Pseudo-Elements in Chrome's DevTools

Understanding how to inspect and debug pseudo-elements is crucial for professional development. Chrome's DevTools provide excellent visibility into these virtual elements.

  1. CTRL–click (Mac) or Right–click (Windows) on a Read More link's » character and choose Inspect.
  2. In the DevTools Elements panel you'll see the HTML code. Expand the <a href="#" class="read-more"> element to reveal the ::after pseudo-element nested inside. Notice how Chrome renders pseudo-elements as actual DOM nodes for inspection purposes, even though they don't exist in your HTML source.
  3. Click on the ::after pseudo-element and you'll see the corresponding CSS rule displayed in the Styles panel. This debugging capability is essential when working with complex pseudo-element implementations or troubleshooting inheritance issues.

DevTools Pseudo-Element Inspection

Element Visibility

Pseudo-elements appear as child elements in the DOM tree, even though they're not in your HTML source code.

Style Inspection

Click on the pseudo-element in DevTools to see its styles in the Styles panel, making debugging easier.

Single Vs. Double Colons (Pseudo-Element Vs. Pseudo-Class)

The distinction between single and double colon syntax represents an important evolution in CSS standards. Some pseudo-elements (such as :before and :after) can be written with one or two colons (:after or ::after). This dual compatibility exists because the CSS specification evolved to create clearer distinctions between different types of selectors.

Initially, pseudo-elements used single colon syntax, but the W3C recognized the need to distinguish pseudo-elements from pseudo-classes. The double colon syntax was introduced to create this semantic separation, making code more readable and intentions clearer for developers.

While single colon syntax remains acceptable for backward compatibility with legacy browsers (though this is rarely a concern in 2026), double colon syntax is required for newer pseudo-elements and represents current best practices.

Understanding the fundamental difference between these selector types is crucial for advanced CSS development:

Pseudo-Class:

  • Targets elements in specific states or positions within the document tree.
  • Uses single colon syntax.
  • Examples include :hover, :first-of-type, :last-child, :not, :checked, :focus-within

Pseudo-Element:

  • Creates virtual elements that target specific parts of existing elements.
  • Uses double colon syntax for modern implementations.
  • Examples include ::after, ::before, ::first-letter, ::first-line, ::placeholder, ::selection

These selectors represent just a subset of available pseudo-classes and pseudo-elements. For comprehensive documentation and browser support details, visit the MDN Web Docs CSS selectors reference at tinyurl.com/pseudo-ce

Pseudo-Class vs Pseudo-Element

FeaturePseudo-ClassPseudo-Element
SyntaxSingle colon (:)Double colon (::)
PurposeStyles element statesStyles specific parts
Examples:hover, :first-of-type::before, ::after
Content PropertyNot applicableRequired for visibility
Recommended: Use double colons for pseudo-elements to follow modern CSS standards and distinguish from pseudo-classes.
Backward Compatibility Note

While single colon syntax works for older pseudo-elements like :before and :after, new pseudo-elements require double colons. Always use double colons for consistency.

Common Pseudo-Elements and Pseudo-Classes

Pseudo-Classes

Target element states like :hover for mouse over, :first-of-type for first elements, and :not for exclusions.

Pseudo-Elements

Style element parts like ::first-letter for drop caps, ::first-line for paragraph styling, and ::before/::after for content injection.

Key Takeaways

1Pseudo-elements create virtual elements that can add decorative content without cluttering HTML markup
2The content property is required for pseudo-elements to be visible and defines what content appears
3Use ::before to add content at the beginning of an element and ::after for content at the end
4Pseudo-elements are visible in Chrome DevTools as child elements, making them easy to inspect and debug
5Double colons (::) distinguish pseudo-elements from pseudo-classes (:) in modern CSS syntax
6Pseudo-classes style element states while pseudo-elements style specific parts of elements
7Special characters can be added using keyboard shortcuts or ALT codes for enhanced visual design
8Pseudo-elements are ideal for adding icons, decorative characters, and styling hooks without additional HTML

RELATED ARTICLES