Explain it simply
Explain it simply
A pseudo-class selects a whole element in a certain state; a pseudo-element selects a part that has no tag of its own in the HTML. The first line of a paragraph, the number in front of a list item and the text the user has highlighted are not real elements, yet ::first-line, ::marker and ::selection can style them.
::before and ::after go further: they create a brand-new box inside the element, straight from CSS. Accents under headings, large quote marks and similar decorations can be added without touching the HTML.
Problem → Why → Solution
The problem: a fully styled accent that never appears
To put a 64px colored accent under a heading, you give ::after a display, width, height and background. After a refresh, nothing shows, and the DevTools Elements panel has no ::after node to inspect. The heading stays 30px tall; the extra 11px (8px margin plus 3px height) never arrives.
.section-title::after {
/* content is missing, so this box is never generated */
display: block;
width: 64px;
height: 3px;
margin-top: 8px;
background: crimson;
}Why is no box created?
On ::before and ::after, content defaults to none, and with content: none the browser does not generate the box at all, so every other property has nothing to attach to. Add a single content: "" (an empty string) and the box appears; the heading grows to 41px.
Core concept: generated boxes and element parts
Pseudo-elements are written with two colons (::) to tell them apart from pseudo-classes. Browsers still accept a single colon for the four legacy ones, :before, :after, :first-line and :first-letter, but new code should use ::. A selector can hold only one pseudo-element, and it must come last.
::before becomes the first child and ::after the last child inside the element, not outside it. That is why they inherit the element's color and font, and why setting position: relative on the element lets you place them with position: absolute. Their default display is inline, where width and height have no effect, so switch to block or inline-block. Do not rely on ::before or ::after for replaced elements such as <img> and <input>, whose content the element itself does not control.
Where ::before and ::after live
-------------------------------
<h2 class="title"> the element's box
+-------------------------------------------+
| [::before] Section text [::after] |
+-------------------------------------------+
first child ^ ^ last child
both are INSIDE the h2 and inherit its stylescontent accepts a string ("→ "), an image (url(icon.svg)), an attribute value (attr(data-label)) or counters. Users cannot select or copy generated text, and it is missing from JavaScript's textContent. Screen readers usually do read it, though: in Chromium, a Save button with ::before { content: "★ " } gets the accessible name "★ Save". For pure decoration, content: "★" / "" supplies empty alternative text after the slash. Current major browsers support that syntax, but a browser that does not will drop the whole declaration, so write a plain content line before it as a fallback.
For specificity, a pseudo-element counts the same as an element selector. A pseudo-class may come before it: a:hover::after changes the ::after box while the link is hovered. The reverse order, a::after:hover, makes the whole selector invalid, so the rule is ignored. Do not treat ::before or ::after as separate elements you can hover or click; pointer events land on the parent element.
- Select a part: ::marker, ::first-line
- Or create a box: ::before / ::after
- No content means no box
- The box lives inside the element
Think of ::before and ::after as two invisible spans at the start and end inside the element.
General pattern
.element { position: relative; } /* anchor for absolute boxes */
.element::before {
content: "text" / "alt text"; /* required: "" for an empty box */
position: absolute; /* or display: block */
}
li::marker { color: value; } /* style an existing part */
p::first-line { font-weight: value; }For box-creating ::before and ::after, always write content and give the box a display or position. Pseudo-elements that style an existing part need no content.
Example: a heading accent and a note icon
Here ::after draws an empty box as a gradient line and ::before inserts emoji text. Try it with an <h2 class="section-title"> and a <p class="note">, then expand the ::before and ::after nodes in the DevTools Elements panel.
.section-title::after {
content: "";
display: block;
width: 64px;
height: 3px;
margin-top: 8px;
background: linear-gradient(90deg, cyan, magenta);
}
.note::before {
content: "💡 ";
}Code breakdown
- content: "";
content: "";An empty string, but enough to create the box. Delete this line and the six properties below it do nothing.
- display: block;
display: block;Switches from inline to block, so width and height apply and the line drops below the heading text.
- width/height
height: 3px;Makes a 64px by 3px bar; with margin-top: 8px the heading grows 11px taller.
- gradient
background: linear-gradient(90deg, cyan, magenta);With no text inside, the background is all you see: cyan fading to magenta from left to right.
- .note::before
content: "💡 ";Text content stays inline in front of the note. A screen reader may announce the emoji's name, something like "light bulb", so if it is purely decorative, "💡 " / "" with empty alt text is kinder.
A 64px by 3px cyan-to-magenta bar appears 8px below the heading, and a 💡 emoji appears before the note text; the emoji cannot be selected with the mouse.Pseudo-elements that style existing parts
| Pseudo-element | What it styles |
|---|---|
| ::marker | A list item's bullet or number (only a few properties such as color, font and content apply) |
| ::first-line | The first rendered line of a block; it changes with the width |
| ::first-letter | The first letter, for drop caps |
| ::placeholder | An input's placeholder text |
| ::selection | Text the user highlights (a few properties such as color and background) |
When styling ::placeholder, watch the contrast: too faint and it becomes unreadable. Never use a placeholder instead of a label either, since it vanishes as soon as the user types.
In real projects
Where this shows up in real projects
Production CSS uses ::before and ::after mostly for decoration: the asterisk beside a required field's label, a large quote mark on a testimonial card, a gradient overlay on an image, a CSS-only arrow on a dropdown button, the little triangle on a tooltip. The HTML stays clean and the component's styling lives in one CSS file.
Content-heavy sites tint ordered-list numbers with ::marker and match the highlight color to the theme with ::selection. The rule many teams follow is simple: only put something in a pseudo-element if losing it with CSS turned off would not matter. Information like "Sale" or "Out of stock" belongs in the HTML, where translation tools, search engines and users who copy and paste can reach it.
Common mistakes
Common mistakes
1. Sizing an inline box
Even with content: "", an ::after left at its default inline display ignores width: 64px; height: 3px;, so the line stays invisible. Give it display: block or inline-block, or use position: absolute, which makes the box block-level.
2. Forgetting position: relative on the parent
Placing a quote mark with ::before { position: absolute; top: 0; left: 0; } without position: relative on the card sends it to the corner of the nearest positioned ancestor, often the page, instead of the card.
3. Putting essential text in content
With .price::after { content: " (incl. tax)"; }, the note is missing when users copy the price, and translation tools usually skip it too. Put meaningful text in the HTML and keep pseudo-elements for decoration.
Best practices
Best practices
- Start every ::before or ::after rule with its content line so it is never forgotten.
- View the page with CSS disabled; if meaning disappears, move that content into the HTML.
- Give decorative icons and emoji empty alt text (/ "") and listen to the result once with a screen reader.
- Pair absolutely positioned boxes with position: relative on the parent in the same change.
Mini challenge
Mini challenge: decorate a form and a quote
Without changing the HTML, decorate a required label, a quote and a numbered list using only pseudo-elements, and keep screen readers from announcing the decoration.
- Add a red " *" after the .required label with empty alt text (plus a fallback line)
- Place a 48px “ in the top-left corner of .quote with absolute positioning
- Make the numbers of the .steps list blue and bold
- Give selected text on the whole page a light yellow background
<style>
/* TODO: .required::after - red " *", hidden from screen readers */
/* TODO: .quote - make it the positioning anchor, leave 40px on the left */
/* TODO: .quote::before - a 48px light-blue opening quote mark, top-left */
/* TODO: blue, bold numbers for .steps list items */
/* TODO: light yellow background for selected text */
</style>
<label class="required" for="email">Email</label>
<input id="email" type="email" required>
<blockquote class="quote">Small steps every day.</blockquote>
<ol class="steps"><li>Plan</li><li>Build</li><li>Ship</li></ol>Expected: A crimson " *" follows "Email", yet the input's accessible name stays "Email". A 48px light-blue “ sits left of the quote text, the list numbers 1. 2. 3. are blue and bold, and selected text gets a light yellow background.
Show hint
Write content: " *"; then content: " *" / ""; so a browser that does not know the new syntax keeps the first line. For the quote mark, set position: relative on .quote and place ::before at left: 0; top: -12px.
Show solution
<style>
.required::after {
content: " *";
content: " *" / "";
color: crimson;
}
.quote { position: relative; margin: 24px 0; padding-left: 40px; }
.quote::before {
content: "“";
content: "“" / "";
position: absolute;
left: 0;
top: -12px;
font-size: 48px;
color: #a5b4fc;
}
.steps li::marker { color: #1a73e8; font-weight: bold; }
::selection { background: #fde68a; }
</style>
<label class="required" for="email">Email</label>
<input id="email" type="email" required>
<blockquote class="quote">Small steps every day.</blockquote>
<ol class="steps"><li>Plan</li><li>Build</li><li>Ship</li></ol>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Pseudo-elements use :: and either style a part of an element (::marker, ::first-line, ::selection) or create a new box (::before, ::after).
- ::before and ::after have no box without content, and they start inline, so change display before sizing them.
- Generated boxes are the first/last child inside the element; position them absolutely against a position: relative parent.
- Screen readers read generated text: hide decoration with empty alt text such as content: "★" / "" and keep essential text in the HTML.
CSS reference — MDN Web Docs