Taking Advantage of Pseudo-Elements
Modern CSS pseudo-elements go far beyond::before and ::after. From view transitions to scroll markers, these invisible helpers unlock interactions that once required JavaScript.
Overview
Most developers know::before and ::after. They’re workhorses, used for decorative elements, clearfixes, and the occasional icon. But CSS has quietly shipped a new generation of pseudo-elements that do far more than add visual flair.
These newer pseudo-elements give you direct styling hooks into browser-native features. Dialogs, popovers, view transitions, scroll-driven navigation, form pickers. Things that once required JavaScript wrappers or couldn’t be styled at all.
This guide covers the pseudo-elements worth knowing, when to reach for them, and how they change what’s possible with CSS alone.
The Classics
Before diving into the new, it helps to revisit what we already have. These pseudo-elements have been around for years, but they remain foundational.::before & ::after
::before & ::after
::before & ::after
These create anonymous inline elements as the first or last child of their parent. They require content to render, even if it’s empty.The common use cases are decorative layers, icons, separators, and expanding hit targets without adding DOM nodes. They keep your HTML clean while enabling visual complexity.Button Hover Effect Pattern
One pattern worth using constantly is adding a subtle background hover effect:::before element fills the button with position: absolute and inset: 0. By default it’s scaled down and invisible. On hover, it animates to full scale and opacity, creating a nice tactile feel without extra markup.Key Properties
- Required:
contentproperty (even if empty string) - Position: Creates anonymous inline element
- Use cases: Decorative layers, icons, expanded hit targets
- Browser support: Universal
::backdrop
::backdrop
::backdrop
The::backdrop pseudo-element appears behind <dialog> and fullscreen elements. It’s the layer between the element and the rest of the page.Style it to control the appearance of modal overlays:Key Properties
- Elements:
<dialog>, fullscreen elements - Layer: Positioned between element and page
- Use cases: Modal overlays, fullscreen backgrounds
- Browser support: Modern browsers (check for
backdrop-filter)
::highlight()
::highlight()
::highlight()
The::highlight() pseudo-element styles custom text highlights created via the CSS Custom Highlight API. Unlike ::selection, you can create multiple named highlights.Key Properties
- API: Requires JavaScript Custom Highlight API
- Multiple highlights: Can define different named highlights
- Use cases: Search results, syntax highlighting, collaborative editing
- Browser support: Modern browsers (check MDN)
::selection
::selection
::selection
The::selection pseudo-element styles text selected by the user:Key Properties
- Applies to: User text selections
- Allowed properties: background, color, text-decoration, text-shadow
- Use cases: Brand-consistent text selection
- Browser support: Universal
::placeholder
::placeholder
::placeholder
The::placeholder pseudo-element styles placeholder text in form inputs:Key Properties
- Elements:
<input>,<textarea>with placeholder attribute - Allowed properties: color, font, opacity, text properties
- Use cases: Styled placeholder text
- Browser support: Universal
Modern Pseudo-Elements
These newer pseudo-elements unlock powerful styling capabilities for browser-native features.View Transition Pseudo-Elements
View Transition Pseudo-Elements
View Transition Pseudo-Elements
The View Transitions API lets you animate between DOM states. Click a thumbnail, it morphs into a full-screen image. Navigate between pages, elements glide into their new positions. The API handles the snapshot diffing. You control the animation through pseudo-elements.When you calldocument.startViewTransition(), the browser captures the current state, applies your DOM changes, then generates a tree of pseudo-elements representing both states. You style these to define how elements transition.Available Pseudo-Elements
Image Lightbox Example
Here’s how to create a morphing image lightbox:Styling the Transition
How It Works
- view-transition-name: Assign the same name to elements across states
- Transition callback: Update the DOM inside
startViewTransition() - Browser magic: Interpolates between matched elements
- Style control: Use pseudo-elements to customize timing and effects
view-transition-name across a transition, the browser treats them as the same element and interpolates between their positions, sizes, and styles.Prior to view transitions, this effect required complex JavaScript libraries or manual cloning. Now, with just a few lines of CSS, you get smooth, performant animations handled natively by the browser.Key Properties
- API:
document.startViewTransition() - Pseudo-elements:
::view-transition-group(),::view-transition-image-pair(), etc. - Use cases: Page transitions, image lightboxes, animated navigation
- Browser support: Chrome, Edge (check MDN for updates)
::marker
::marker
::marker
The::marker pseudo-element styles list item markers (<li>, <summary>):Key Properties
- Elements:
<li>,<summary>,display: list-item - Allowed properties: color, font, content, some animation properties
- Use cases: Styled list bullets, custom markers
- Browser support: Modern browsers
::slotted()
::slotted()
::slotted()
The::slotted() pseudo-element styles elements distributed into Web Component slots:Key Properties
- Context: Shadow DOM only
- Scope: Direct children of
<slot> - Use cases: Web Component styling
- Browser support: Modern browsers
::details-content
::details-content
::details-content
The::details-content pseudo-element (experimental) wraps the content of a <details> element, enabling smooth animations:Key Properties
- Element:
<details> - Status: Experimental
- Use cases: Animated accordions
- Browser support: Limited (check MDN)