Sub topic 2
Pseudo-classes vs pseudo-elements: one colon or two
This chapter assumes you know CSS rule syntax, and that you can write type, class, ID, and attribute selectors with descendant and child combinators. Everything else about pseudo selectors builds on one distinction, so it is worth getting clear first.
Two kinds of pseudo selector
A pseudo-class selects an element that already exists, based on a condition the HTML does not record. The condition might be state (the pointer is over it), position (it is the first child), or a relationship (it contains an image). A pseudo-class starts with one colon.
A pseudo-element styles a part of an element, or content the browser generates, that has no tag of its own. Examples are the first line of a paragraph or a decorative arrow added after a link. A pseudo-element starts with two colons.
| Feature | Pseudo-class | Pseudo-element |
|---|---|---|
| Syntax | `:hover` | `::before` |
| Selects | An existing element in a certain condition | A part of an element, or generated content |
| Extra boxes or text | None | Often creates something that is not in the HTML |
| Example | `a:hover` | `p::first-line` |
A worked example
Both rules below target links with the class external. The first changes the link when the pointer is over it. The second adds an arrow after it.
.external:hover {
color: #c00;
}
.external::after {
content: " \2197";
}In .external:hover, the selector still matches the same a element. It only matches while the condition is true. In .external::after, the selector matches a new piece of content that the browser places inside the link, after its text.
You can read a pseudo-class as "an element that is ...". You can read a pseudo-element as "the ... of an element".
Why two colons
CSS2 wrote the four oldest pseudo-elements with one colon: :before, :after, :first-line, and :first-letter. CSS3 introduced the double colon so that readers and parsers could tell the two kinds apart at a glance.
Browsers still accept the single-colon form for those four, for backward compatibility. Newer pseudo-elements such as ::marker, ::selection, and ::placeholder work only with two colons. Use two colons for every pseudo-element in new code.
/* Legacy form, still works for these four only */
p:first-letter {
font-weight: bold;
}
/* Preferred form */
p::first-letter {
font-weight: bold;
}Placement and combining
A pseudo-element must come last in a compound selector, because it refers to a part of whatever the selector before it matched. Pseudo-classes can appear anywhere in the compound selector and can be chained.
a:hover::after {
content: " (open)";
}This reads as: the generated content after a link that is being hovered. Writing a::after:hover is a common mistake. Browsers generally treat it as invalid, and the whole rule is dropped.
A selector list is also strict. If one selector in a comma-separated list is invalid, older browsers drop the entire rule. Keep a misspelled or unsupported pseudo selector out of a list of otherwise valid ones.
Common errors
- Using one colon for a newer pseudo-element, such as
:marker. The rule is ignored. - Putting a pseudo-element before another part of the selector, as in
a::after:hover. - Expecting a pseudo-element to appear without
content. For::beforeand::after, the box is only created whencontentis set. - Using a pseudo-class when you need to style a part.
p:first-lineis not valid in modern CSS as a pseudo-class, and it only works through the legacy single-colon alias for the pseudo-element.
Exercise
Which of these selectors is written correctly, and which kind of pseudo selector does each part use? (a) li:last-child::marker (b) li::marker:last-child
Show answer
Only (a) is correct. :last-child is a pseudo-class, so it matches the final list item, and ::marker is a pseudo-element, so it styles that item's bullet or number. The pseudo-element must come last, so (b) is invalid.
Exercise
You write .note:before { content: "Note: "; } and it works. Why should you still change it, and to what?
Show answer
The single colon is the CSS2 legacy form, kept only for compatibility with :before, :after, :first-line, and :first-letter. Change it to .note::before so the code shows that it is a pseudo-element.