Sub topic 2
Pseudo-classes vs pseudo-elements: one colon or two
This chapter covers the difference between pseudo-classes and pseudo-elements. It assumes you know basic HTML elements and attributes, CSS rule syntax (selectors, properties, values), type, class, ID and attribute selectors, and the descendant and child combinators. Later chapters cover each pseudo selector in detail. Here you build the model that tells you which kind you are looking at.
Where pseudo selectors fit
A selector you already know matches elements by what is written in the HTML: a tag name, a class, an ID, or an attribute. Pseudo selectors match things the markup does not state directly. There are two kinds.
- A pseudo-class matches an element that is already in the document, based on its state, its position, or its relationship to other elements. The element is real. The condition is what changes.
- A pseudo-element matches a part of an element, or a piece of content the browser generates, that has no tag of its own. It gives you a handle on something that is not a separate node in the HTML.
Both attach to a selector with a colon. The number of colons tells you which kind you wrote.
The syntax rule
A pseudo-class uses one colon. A pseudo-element uses two colons.
/* pseudo-class: one colon */
a:hover {
text-decoration: underline;
}
/* pseudo-element: two colons */
p::first-line {
font-variant: small-caps;
}The double colon was introduced in CSS3 to make the distinction visible. In CSS2, pseudo-elements also used a single colon. For backward compatibility, browsers still accept the single-colon form for four legacy pseudo-elements: :before, :after, :first-line and :first-letter. Every other pseudo-element requires two colons. Write ::marker, ::selection and ::placeholder with two colons, because the single-colon form does not work for them.
Use the double colon for all pseudo-elements in new code. It is consistent, and a reader can tell the two kinds apart at a glance.
What each one selects
The question to ask is whether the thing you want to style is a whole element in a particular condition, or a part of an element.
| Question | Pseudo-class | Pseudo-element |
|---|---|---|
| Colons | One | Two |
| What it selects | An existing element in a given state or position | A part of an element, or generated content |
| Does it add anything to the page? | No | Sometimes, as with `::before` and `::after` |
| Examples | `:hover`, `:focus-visible`, `:nth-child()`, `:checked`, `:not()` | `::before`, `::after`, `::first-letter`, `::marker`, `::selection` |
| Count per element | Any number can apply at once | At most one pseudo-element per selector, placed last |
Compare two selectors that look alike:
li:first-child {
font-weight: bold;
}
li::first-letter {
text-transform: uppercase;
}The first selects the whole li that is the first child of its parent. The entire item becomes bold. The second selects only the first letter of the text inside every li. No element in the HTML wraps that letter, so only a pseudo-element can reach it.
Where each one goes in a selector
A pseudo-class can appear anywhere in a compound selector, and you can chain several on one element. Each one is a further condition that the element must meet.
input:required:invalid:not(:placeholder-shown) {
border-color: #c00;
}This matches an input that is required, currently invalid, and not showing its placeholder. All three conditions must hold. The order of pseudo-classes does not matter here, because they are independent tests. (Order does matter for link states, which a later chapter covers.)
A pseudo-element must come at the end of the selector, after any pseudo-classes. It names the final target.
/* valid: the pseudo-class comes first, the pseudo-element last */
a:hover::after {
content: " \2192";
}
/* invalid: nothing can follow a pseudo-element */
a::after:hover {
content: " \2192";
}The second rule is dropped by the browser. A few pseudo-elements allow certain user-action pseudo-classes after them, but you should not rely on that. Put the pseudo-element last.
You also cannot chain two pseudo-elements, as in p::first-line::first-letter. The browser treats that selector as invalid and ignores the whole rule.
Invalid selectors drop the whole rule
An unknown or misspelled pseudo selector makes the entire selector invalid, and the browser discards the rule. This matters when you use selector lists.
a:hover,
a:not-a-real-state {
color: red;
}The second selector is not recognized, so the whole rule, including the valid a:hover, is ignored. The same happens when you use a newer pseudo-class that an older browser does not support, such as :has() in an old browser. If you want a rule to survive, write the selectors as separate rules, or use :is() and :where(), which are forgiving about invalid entries in their argument list. Later chapters explain both.
Pseudo-elements and the DOM
A pseudo-element does not exist in the DOM. You cannot find ::before with document.querySelector, you cannot attach an event listener to it, and a user cannot select its text. The browser builds it at render time from your CSS.
This has a practical consequence. Pseudo-elements such as ::before and ::after are generated, so they only appear when you give them a content property. Without content, nothing is rendered. Other pseudo-elements, such as ::first-letter, style text that already exists and need no content.
.badge::after {
content: "new";
margin-left: 0.5em;
font-size: 0.75em;
}A pseudo-class, by contrast, always describes an element that is in the DOM. You can test the same condition from JavaScript with element.matches(":hover"). You can also pass a pseudo-class selector to querySelectorAll, for example document.querySelectorAll("input:checked"). Passing a pseudo-element selector to these methods does not match anything.
const checkedBoxes = document.querySelectorAll("input:checked");
console.log(checkedBoxes.length);
const generated = document.querySelectorAll(".badge::after");
console.log(generated.length); // always 0To read the style of a generated box from JavaScript, pass the pseudo-element name as the second argument to getComputedStyle: getComputedStyle(el, "::after").content.
Common errors
- Using one colon for a modern pseudo-element.
li:markerandinput:placeholderdo nothing. Useli::markerandinput::placeholder. - Putting a pseudo-element before a pseudo-class.
a::after:hoveris invalid. Writea:hover::after. - Forgetting
content. A::beforeor::afterrule with nocontentproperty produces no box. For a purely visual shape, usecontent: "". - Expecting a pseudo-class to style part of an element.
p:first-letterworks only because of the legacy single-colon allowance. Treat it as the pseudo-element it is, and writep::first-letter. - Grouping a pseudo-element with an unsupported selector in one list. One unsupported selector invalidates the whole rule, so keep browser-specific selectors in their own rules.
Exercises
Exercise
For each selector, say whether it uses a pseudo-class or a pseudo-element and what it selects: (1) input:checked, (2) p::first-letter, (3) li:nth-child(2), (4) .card::before.
Show answer
(1) Pseudo-class. It selects an existing input that is currently checked. (2) Pseudo-element. It selects the first letter of the text in each paragraph, which has no tag of its own. (3) Pseudo-class. It selects an existing li that is the second child of its parent. (4) Pseudo-element. It selects a generated box placed before the content of each .card, and it renders only if content is set.
Exercise
This rule has no effect. Explain why and rewrite it so it works: a::after:hover { content: " \2192"; }
Show answer
A pseudo-element must be the last part of a selector, so a::after:hover is invalid and the browser drops the rule. Put the pseudo-class first: a:hover::after { content: " \2192"; }.
Exercise
A stylesheet contains h2:hover, h2:fake-state { color: teal; }. Does the h2 turn teal on hover? Why or why not?
Show answer
No. The unknown pseudo-class :fake-state makes the whole selector list invalid, so the browser ignores the entire rule, including the valid h2:hover. Splitting it into two rules, or removing the invalid selector, fixes it.