Pseudo-Elements: ::before, ::after, ::first-line, ::marker, ::selection and More
Styling parts of a document that aren't elements: ::before and ::after, ::first-letter and ::first-line and their property limits, ::marker, form control parts, highlights such as ::selection and ::target-text, ::backdrop and ::cue.
kvi5hnuCode Sprout team member11 min readOn this page
A pseudo-element selects something that isn't an element in the HTML: the first line of a paragraph, the bullet of a list item, the text the user has selected, or a box that CSS itself generates before or after an element's content. Pseudo-elements are written with two colons, as in ::before, to distinguish them from pseudo-classes, which select whole elements in a certain state.
Writing pseudo-elements in selectors
p::first-line { } /* the first line of every paragraph */
.book-card:hover::after { } /* the ::after box of a hovered card */
li::marker { } /* the bullet or number of a list item */
- The pseudo-element comes last in its compound selector.
p::first-line.introis invalid. Pseudo-classes describing the element go before it:a:hover::after. - A few pseudo-classes may follow some pseudo-elements, such as
::part(label):hoverand::view-transition-group(*):only-child, but not after::beforeor::after. - A selector can't hold pseudo-elements inside
:is(),:where(),:not()or:has(). - The single-colon forms
:before,:after,:first-lineand:first-letterstill work for compatibility with CSS 2. Newer pseudo-elements require two colons.

None of these parts exist in the HTML. Note in the first panel that the generated boxes sit inside the element's border: ::before and ::after are its first and last children.
::before and ::after: generated boxes
::before and ::after create a box as the first or last child of an element, filled with whatever the content property says. They're used for icons, decorative shapes, labels, quotation marks and counters. Without a content value, they don't exist:
.new::before {
content: "NEW";
font: 700 0.7rem system-ui, sans-serif;
background: #2f7d4f;
color: white;
padding: 0.1rem 0.35rem;
border-radius: 3px;
margin-right: 0.4rem;
}
.price::after { content: " incl. VAT"; color: #666; font-size: 0.85em; }
blockquote::before { content: "“"; font-size: 3rem; color: #f4c95d; line-height: 0; vertical-align: -0.4em; }
Key facts:
contentis required. Usecontent: ""for a purely decorative box, such as a shape or an overlay.- They're inline by default. Set
display: block,inline-block, or position them absolutely to give them sizes. - They're inside the element, so they inherit from it, take part in its layout (as flex or grid items if the element is a flex or grid container), and are clipped by its
overflow. - They don't work on replaced elements such as
<img>and<video>, or on most form controls such as<input>, which have no content box to put children in. Wrap the element, or use the parent. - They're not in the DOM. Scripts can't select them, and users can't select their text. Their computed styles are available with
getComputedStyle(el, "::before").
Generated content and accessibility
Screen readers generally do read generated text, so content: "NEW" is announced. That's good for meaningful labels and bad for decoration. You can give generated content alternative text, after a slash:
.external::after { content: "↗" / "opens in a new tab"; }
.icon-star::before { content: "★" / ""; } /* decorative: announced as nothing */
The alternative text syntax is supported in all current browsers. Still, keep important information in the HTML: generated content isn't copied with the text, isn't translated by most page translators, and is harder to find.
Article 95 covers content in depth: strings, attr(), counters, quotes and images.
::first-letter and ::first-line
These select typographic parts of a block container, such as a paragraph, heading or list item. They don't work on inline elements.
::first-letter selects the first letter, together with any punctuation right before or after it, such as an opening quotation mark. If other content, such as an image or an inline-block, comes first, nothing is selected. It's the classic tool for drop caps:
.chapter > p:first-of-type::first-letter {
font-size: 3.2em;
float: left;
line-height: 0.8;
margin: 0.05em 0.1em 0 0;
color: #8a3b12;
}
The initial-letter property does this more precisely, sizing the letter to an exact number of lines (article 93).
::first-line selects the first formatted line, which changes with the width of the container and the font size. Resize the preview to see the styled text grow and shrink with the line:
p { font-family: Georgia, serif; max-width: 34rem; line-height: 1.5; }
p::first-letter { font-size: 2.6em; float: left; line-height: 1; margin-right: 0.08em; color: #8a3b12; }
p::first-line { font-variant-caps: small-caps; letter-spacing: 0.04em; }

The rule is the same in both panels. Only the width changed, and with it the words that land on the first line.
Only some properties apply to these pseudo-elements:
::first-letter | ::first-line | |
|---|---|---|
| font, color, background, text decoration | yes | yes |
opacity | yes | no (allowed by the specification, but browsers don't apply it) |
letter-spacing, word-spacing, text-transform, text-shadow | yes | yes |
margins, padding, borders, float, box-shadow | yes | no |
vertical-align, line-height | yes | yes |
Other properties, such as width or position, are ignored.
::marker
::marker selects the bullet or number of a list item, or of any element with display: list-item. It accepts a small set of properties: color, font properties, content, white-space, animations and transitions, and a few text properties:
li::marker { color: #8a3b12; font-weight: 700; }
.steps li::marker { content: counter(list-item) ". "; }
summary::marker { content: "▸ "; } /* the triangle of <details> */
Article 94 covers lists in depth. Until version 27.2, Safari applied only color and font-size from ::marker rules. Full support, including content, arrives with Safari 27.3.
Pseudo-elements of form controls
-
::placeholderstyles the placeholder text of<input>and<textarea>. It accepts the same properties as::first-line. Placeholders often have too little contrast by default, so check it (article 35). Note the difference from:placeholder-shown, which selects the input itself while its placeholder is visible. -
::file-selector-buttonstyles the button inside<input type="file">:cssinput[type="file"]::file-selector-button { font: inherit; border: 1px solid #8a3b12; border-radius: 6px; background: white; padding: 0.3rem 0.8rem; margin-right: 0.75rem; } -
::details-contentselects the part of a<details>element that opens and closes, which can be animated (article 108). -
Customizable select adds
::picker(select),::picker-iconand::checkmarkfor styling the parts of a<select>(article 102). -
::-webkit-pseudo-elements such as::-webkit-scrollbarand::-webkit-slider-thumbstyle browser-specific parts of controls. They aren't standard, and most have standard replacements now, such asscrollbar-color(article 43).
Highlight pseudo-elements
Highlights style ranges of text that the browser marks for some reason:
| Pseudo-element | Styles |
|---|---|
::selection | text the user has selected |
::target-text | text highlighted by a text fragment link, such as page#:~:text=sail%20loft |
::search-text | text matched by the browser's find-in-page (Chrome only so far) |
::spelling-error, ::grammar-error | text the browser marks as misspelled or ungrammatical (not in Firefox yet) |
::highlight(name) | ranges a script registers with the Custom Highlight API |
Highlights accept only properties that don't change layout: color, background-color, text-shadow, and SVG fill and stroke. The specification also allows text decoration, but in ::selection only Chromium applies it. That keeps them fast, and prevents a page from learning what you've selected or misspelled through layout changes.
::selection { background: #f4c95d; color: #1a1a1a; }
::target-text { background: #fde7d6; }
Users expect selections to look the way their system shows them, so restyle ::selection only to fit the page's palette, and keep the contrast high.
Since Chrome 134, highlight styles are inherited along the element tree, as the specification defines: a ::selection rule on a container now applies inside its child elements too. Firefox and Safari still apply each element's own ::selection rules (or the defaults) to its text, so write the rule as a bare ::selection (which means *::selection) or :root ::selection, with a space, if you want the same look everywhere. :root::selection without the space matches only the root element's own text.
The Custom Highlight API lets scripts highlight text ranges without wrapping them in elements, which suits search results and syntax highlighting:
const range = new Range();
range.setStart(textNode, 0); range.setEnd(textNode, 5);
CSS.highlights.set("search-hit", new Highlight(range));
::highlight(search-hit) { background: #f4c95d; }
Firefox shipped the script API in June 2025 and the ::highlight() pseudo-element in March 2026, which made the feature Baseline.
::backdrop
::backdrop is a box the size of the viewport, drawn behind an element in the top layer: a modal <dialog>, a popover, or an element in fullscreen. It's how you dim the page behind a dialog:
dialog::backdrop { background: rgb(0 0 0 / 0.5); backdrop-filter: blur(2px); }
::backdrop inherits from its element, the dialog or popover, so custom properties set on the dialog are available in its backdrop. That changed in 2024: earlier versions of some browsers didn't let it inherit anything. Article 60 covers top-layer elements.
::cue
::cue styles the captions and subtitles of a <video>, from WebVTT text tracks. ::cue(selector), which isn't supported in Firefox, styles parts of cues, such as ::cue(i) for italic text or ::cue(v[voice="Ana"]) for a speaker. Only a limited set of properties apply: color, background, font, text decoration, text-shadow, opacity, visibility, white-space and outline properties. The user's own caption settings may override yours, which is intended.
Pseudo-elements covered elsewhere
::part()and::slotted()style the insides of web components (article 14).::view-transitionand its children animate view transitions (article 114).::scroll-marker,::scroll-button()and::columnbuild carousels (article 115).
What's current
::before,::after,::first-letter,::first-line,::selection,::placeholder,::file-selector-button,::backdropand::cuework in all current browsers.::markeris in all browsers, but Safari before 27.3 applies onlycolorandfont-sizeto it.::target-textis Baseline since December 2024, and::highlight()since March 2026.::details-contentis Baseline since September 2025.::spelling-errorand::grammar-errorare in Chromium and Safari, not Firefox.::search-textis Chrome only.- Alternative text for generated content (
content: "★" / "") works in all current browsers.
Common questions
Why doesn't my ::before show up?
Most often, content is missing: without it, the box isn't generated. Use content: "" for an empty decorative box. Other causes: the element is replaced, such as an <img> or <input>, which can't have generated children, or the box is inline and empty, so it has no size until you give it display: inline-block or block and dimensions.
What's the difference between :before and ::before?
None in behavior. The double colon is the modern syntax that distinguishes pseudo-elements from pseudo-classes. The single colon is kept only for the four pseudo-elements from CSS 2.
Can I add ::before to an <img>?
No. Images are replaced elements, with no content box for generated children. Wrap the image in a <figure> or <span> and put the pseudo-element on the wrapper.
Do screen readers read generated content?
Usually yes. Give decorative content an empty alternative, as in content: "★" / "", and keep essential information in the HTML.
Why does my ::selection style not apply inside a link in some browsers?
Only Chrome applies highlight styles through inheritance. In other browsers, a ::selection rule on a parent doesn't reach the selected text of its child elements. Write the rule for ::selection on all elements, as in ::selection { ... } at the top level, or :root ::selection.
Summary
- Pseudo-elements select parts of the document that aren't elements, and use two colons. They come last in a compound selector.
::beforeand::aftergenerate inline boxes as the first and last children of an element, requirecontent, and don't work on replaced elements.- Generated text is read by screen readers. Give decorative content an empty alternative text with
/ "". ::first-letterand::first-linestyle the first letter and first formatted line of block containers, with a limited set of properties.::markerstyles list bullets and numbers,::placeholderplaceholder text, and::file-selector-buttonthe button of file inputs.- Highlight pseudo-elements, such as
::selection,::target-textand::highlight(), accept only non-layout properties. ::backdropsits behind top-layer elements such as modal dialogs, and::cuestyles video captions.
Practice questions
Write a rule that adds an arrow after links to other sites (with href starting with http), announced to screen readers as "external".
Answer
a[href^="http"]::after { content: " ↗" / " external"; }
Why does this rule have no effect?
.cover-image::after { content: "Signed"; }
<img class="cover-image" src="salt.jpg" alt="Salt and Paper cover">
Answer
<img> is a replaced element, which can't contain generated children, so ::after isn't created. Wrap the image, for example in <figure class="cover">, and use .cover::after.
A designer asks for the first line of each article's first paragraph to be in bold small caps, and for the effect to cover exactly the first line at any screen width. Which pseudo-element do you use, and why not a <span> around the first few words?
Answer
article > p:first-of-type::first-line { font-weight: 700; font-variant-caps: small-caps; }. ::first-line follows the actual formatted line, which changes with the screen width and font size, while a span around a fixed number of words would be too short on wide screens and wrap onto a second line on narrow ones.
Which of these declarations will apply in a ::selection rule: background-color, font-weight, color, text-decoration, padding?
Answer
background-color and color, plus text-decoration in Chromium browsers only. Highlight pseudo-elements accept only properties that don't affect layout, so font-weight and padding are ignored.
Previous: Logical Pseudo-Classes: (), (), () and the () Parent Selector
Next: Styling Web Components: , ::slotted(), ::part() and ()

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.