Sub topic 14
Combining Pseudo-classes and Pseudo-elements
This chapter assumes you know basic CSS rule syntax, type, class, ID and attribute selectors, and the difference between a pseudo-class (one colon, selects an element in a state) and a pseudo-element (two colons, styles a part of an element or generated content). Here you will chain them in one selector and learn the rules that make the chains work.
The working model
A compound selector can hold several pseudo-classes, and it can end with one pseudo-element. Read the selector left to right. The pseudo-classes filter which elements match. The pseudo-element then picks a part of each matching element.
a:hover::after {
content: " \2192";
}
li:first-child::marker {
color: #c00;
}
.card:has(img)::before {
content: "";
display: block;
height: 4px;
background: #1a73e8;
}In the first rule, a:hover matches a link under the pointer. ::after then adds an arrow after it, but only while the pointer is over the link.
Rule 1: the pseudo-element goes last
A pseudo-element must be the final part of the compound selector. Nothing can follow it, except a few user-action pseudo-classes on some pseudo-elements, which browsers support unevenly.
/* Works */
p:first-child::first-letter {
font-size: 2em;
}
/* Invalid: the whole rule is dropped */
p::first-letter:first-child {
font-size: 2em;
}An invalid selector invalidates the whole rule. The browser gives no error, and the rule simply has no effect.
Rule 2: only one pseudo-element per selector
You cannot stack two pseudo-elements, such as p::before::first-line. Write separate rules for each part.
Rule 3: a selector list is all-or-nothing
If one selector in a comma-separated list is invalid, the browser drops the entire rule. This matters when you list a vendor-specific pseudo-element next to a standard one.
/* If the browser does not know ::-moz-placeholder, both are lost */
input::placeholder,
input::-moz-placeholder {
color: #767676;
}
/* Safer: one rule per selector */
input::placeholder {
color: #767676;
}The same list problem does not apply inside :is() and :where(). These forgive unknown selectors and ignore only the invalid one. However, they do not accept pseudo-elements, so :is(::before) never matches.
Putting state and generated content together
A common pattern shows a decoration only in some state. This example marks required fields that currently fail validation.
label:has(+ input:required:invalid:not(:placeholder-shown))::after {
content: " Please check this field";
color: #c00;
}Here the logical pseudo-classes pick the label based on the input that follows it. The ::after pseudo-element then adds the message to that label. The input needs a placeholder attribute, as in the form-state chapter.
Specificity of a combined selector
Each pseudo-class adds to the class column. Each pseudo-element adds to the type column. a:hover::after scores 0-1-2: one pseudo-class and two types (a and ::after).
| Selector | Specificity (ID-class-type) |
|---|---|
| `a::after` | 0-0-2 |
| `a:hover::after` | 0-1-2 |
| `.nav a:not(.active)::after` | 0-2-2 |
| `:where(.nav) a::after` | 0-0-2 |
Common mistakes
- Putting a pseudo-class after a pseudo-element, which drops the rule.
- Expecting
::beforeon aninputorimgto work. These replaced or void elements have no content box for generated content in most browsers. - Forgetting
contenton::beforeand::after. Without it, nothing is generated. - Testing in one browser only when mixing
:has()with pseudo-elements.
Exercise
Why does button::after:hover { content: "!"; } never apply, and how do you fix it? What is the fixed selector's specificity?
Show answer
A pseudo-class cannot follow a pseudo-element, so the selector is invalid and the browser drops the rule. Write button:hover::after. Its specificity is 0-1-2.
Exercise
A rule lists a::after, a::unknown-part { content: "*"; }. Neither selector produces output in a browser. Why, and what should you change?
Show answer
::unknown-part is invalid, and an invalid selector in a plain selector list invalidates the whole rule. Put each selector in its own rule.