What pseudo-elements are and how they differ from pseudo-classes
How to create content with ::before and ::after
Styling the first letter and first line of text
Customizing text selection appearance
Creating CSS counters for automatic numbering
Advanced techniques like generated content and the attr() function
Introduction to Pseudo-elements
Pseudo-elements allow you to style specific parts of an element or insert content without
adding extra HTML markup. While pseudo-classes (single colon :) select elements
based on state, pseudo-elements (double colon ::) create virtual elements that
style specific portions of content or generate new content entirely.
Pseudo-elements are incredibly powerful for adding decorative elements, creating complex
layouts, and implementing patterns like clearfix—all without cluttering your HTML. They're
virtual elements that don't appear in the DOM but are rendered by the browser as if they were real.
::before and ::after - Generated Content
The ::before and ::after pseudo-elements are the most commonly
used pseudo-elements. They insert content before or after an element's actual content,
perfect for adding decorative elements, icons, or additional text without modifying HTML.
<blockquote class="quote">
This text gets decorative quotation marks
</blockquote>
<a href="https://example.com" class="external-link">
External Link
</a>
Rendered Result:
::first-letter - Drop Caps
The ::first-letter pseudo-element targets the first letter of a block-level element,
allowing you to create classic typographic effects like drop caps without any extra markup.
<p class="drop-cap">
This paragraph has a drop cap on the first letter.
The effect is created entirely with CSS using the
::first-letter pseudo-element.
</p>
Rendered Result:
Allowed Properties
Only certain CSS properties work with ::first-letter:
Font properties (font-family, font-size, font-weight, etc.)
Color properties (color, background-color)
Margin, padding, border
Text decoration
Vertical alignment (only if float is none)
Float and clear
::first-line - First Line Styling
The ::first-line pseudo-element styles the first line of a block-level element.
It dynamically updates as the viewport changes, making it perfect for responsive designs.
<p class="article">
The first line is styled differently. This styling
automatically updates when the window is resized and
text reflows to different line breaks.
</p>
Rendered Result:
Allowed Properties
::first-line supports a limited set of properties:
Font properties
Color and background properties
word-spacing, letter-spacing, text-decoration
text-transform, line-height
clear, vertical-align (if float is none)
::selection - Highlight Color
The ::selection pseudo-element customizes the appearance of text when users
select it with their mouse or keyboard. This is a simple way to add brand personality
and improve the user experience.
Pseudo-elements can generate content dynamically using the content property
combined with functions like attr(), which pulls values from HTML attributes.
CSS counters allow you to create automatic numbering without JavaScript. They're perfect
for section numbering, ordered lists with custom formatting, and complex nested numbering schemes.