Sub topic 14
Combining pseudo-classes and pseudo-elements
Real stylesheets rarely use a pseudo selector alone. You usually stack several: a link that gets an icon only when it is external, a tooltip that appears on hover or keyboard focus, a label that gets an asterisk when its input is required. This chapter covers the grammar for combining them, the specificity of the result, and the mistakes that make a rule silently do nothing.
This chapter assumes you know type, class, ID, and attribute selectors and the descendant and child combinators. It also assumes you have met the individual pseudo-classes (:hover, :focus-visible, :nth-child(), :not(), :is(), :has()) and the basics of ::before and ::after.
The grammar: pseudo-classes first, one pseudo-element last
A compound selector is a run of simple selectors with no spaces between them, such as a.nav:hover::after. Two rules govern how pseudo selectors fit into it:
- You can chain any number of pseudo-classes, in any order, anywhere in the compound.
- You can use at most one pseudo-element, and it must come last.
The pseudo-element comes last because it describes a part of the element that the earlier simple selectors already picked. Everything before it filters which elements qualify. The pseudo-element then picks a piece of each match.
Here is a selector read in that order:
li:nth-child(odd):not(.done)::marker {
color: #c00;
}It means: list items in odd positions that do not have the class done, and style their markers. The same pseudo-classes written after the pseudo-element would make the rule invalid.
Hover and focus effects on generated content
You cannot attach :hover to a pseudo-element in a portable way. You put the state on the real element and the pseudo-element at the end:
.card::before {
content: "";
display: block;
height: 4px;
background: #1a73e8;
transition: height 0.2s;
}
.card:hover::before,
.card:focus-within::before {
height: 8px;
}Both selectors in the list are valid, so the bar grows when the pointer is over the card or when any control inside it has focus. Adding :focus-within next to :hover is a habit worth keeping, because keyboard users never trigger :hover.
Use :is() to avoid repeating the list:
.card:is(:hover, :focus-within)::before {
height: 8px;
}A worked example: a keyboard-friendly tooltip
This page combines an attribute selector, two pseudo-classes inside :is(), and ::after that reads its text from an attribute. Save it as an HTML file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tooltip</title>
<style>
[data-tip] {
position: relative;
}
[data-tip]:is(:hover, :focus-visible)::after {
content: attr(data-tip);
position: absolute;
left: 0;
top: 100%;
margin-top: 0.4em;
padding: 0.25em 0.5em;
background: #222;
color: #fff;
font-size: 0.875rem;
white-space: nowrap;
}
button:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}
</style>
</head>
<body>
<button type="button" data-tip="Saves your draft">Save</button>
</body>
</html>Without :hover or :focus-visible, the ::after box does not exist, because its rule does not match. Tabbing to the button shows the tip as well as the pointer does.
Form patterns with `:has()` and `::after`
A label cannot see the state of its input through a pseudo-class on the label itself. :has() solves this by letting a parent check its contents:
.form-group:has(input:required) label::after {
content: " *";
color: #c00;
}
.form-group:has(input:required:invalid:not(:placeholder-shown)) label {
color: #c00;
}The first rule adds an asterisk to labels in any group that contains a required input. The second rule colors the label red only after the user has typed something invalid, because :not(:placeholder-shown) stays false while the field is empty. This needs a placeholder attribute on the input, even a single space: placeholder=" ".
Color alone is a weak error signal, so pair it with text or an icon. The asterisk above already does that for the required state.
Specificity of combined selectors
Specificity is written as three counts: IDs, then classes, attributes, and pseudo-classes, then types and pseudo-elements. Each pseudo-class adds to the middle count. Each pseudo-element adds to the last. The logical pseudo-classes follow their own rules.
| Selector | Specificity | Why |
|---|---|---|
| `a:hover` | 0,1,1 | One type, one pseudo-class |
| `a:hover::after` | 0,1,2 | The pseudo-element adds a type-level point |
| `li:nth-child(odd)::marker` | 0,1,2 | `:nth-child(odd)` counts as one pseudo-class |
| `button:not(:disabled):hover` | 0,2,1 | `:not()` counts as its argument, `:disabled`, plus `:hover` |
| `.card:has(img)::before` | 0,1,2 | `:has()` counts as its argument, `img`, which is a type selector |
| `:is(#nav, .menu) a:hover` | 1,1,1 | `:is()` takes its most specific argument, `#nav` |
| `:where(#nav, .menu) a:hover` | 0,1,1 | `:where()` always contributes nothing |
Two consequences follow from the table. A single #id inside :is() or :not() raises the whole selector to ID level, even when the element matched through a different branch of the list. When you want a convenient group without that cost, use :where(). This is why reset and base styles often start with :where(): any later rule can override them without a specificity fight.
Common mistakes
These problems make a rule do nothing or the wrong thing.
- Pseudo-class after a pseudo-element.
.card::before:hoveris invalid and the browser drops the rule. Write.card:hover::before. - Two pseudo-elements in one compound.
p::first-line::first-letteris invalid. - Pseudo-elements inside
:is(),:where(), or:not().:is(::before, ::after)never matches. Use a selector list outside:.a::before, .a::after. - One unknown selector in a comma list. If a browser does not recognize any part of a list, it discards the entire rule. Vendor-prefixed pseudo-elements, such as
::-webkit-...forms, belong in separate rules for this reason. Put each in its own rule rather than grouping it with::placeholder. - Missing
content.::beforeand::aftergenerate no box untilcontentis set.content: ""is enough for a purely visual shape. - Expecting generated content on replaced elements.
::beforeand::afterdo not work onimg,input, orbrin most browsers, because those elements have no content area to place the pseudo-element in. Put the pseudo-element on a wrapper instead. - Overriding with weaker selectors. Putting
a:hoverbeforea:linkhides the hover style, as the LVHA order from earlier showed. Combined selectors have the same ordering problem when specificity ties.
Exercise
A stylesheet has a:hover::after { content: " \2197"; } and later a::after:hover { color: red; }. Which rule works, and how would you fix the other so the arrow turns red on hover?
Show answer
The first rule is valid. The second is invalid because a pseudo-class follows a pseudo-element, so the browser ignores it. Fix it by putting the state first: a:hover::after { content: " \2197"; color: red; }, or by adding color: red; to the first rule.
Exercise
Calculate the specificity of :where(.sidebar) a:not(.active):hover::before.
Show answer
0,2,2. :where() contributes nothing, a adds one type point, :not(.active) counts as its argument .active (one class-level point), :hover adds another class-level point, and ::before adds one type-level point. That gives 0 IDs, 2 class-level points, and 2 type-level points.
Exercise
Write one rule that gives a list item a bold marker only when the item is among the first three and does not have the class muted. Which part of the selector must come last?
Show answer
li:nth-child(-n + 3):not(.muted)::marker { font-weight: bold; }. The pseudo-element ::marker must come last. The pseudo-classes :nth-child(-n + 3) and :not(.muted) can appear in either order before it.