Sub topic 8
Selector lists and grouping
This chapter assumes you can write type, class, and ID selectors and know the three-part specificity triple (IDs, classes, types). It covers the comma syntax that applies one block of declarations to several selectors. It also covers what happens to specificity inside a list, and how one bad selector can disable a whole rule.
What a selector list is
A selector list is two or more selectors separated by commas. The browser applies the declaration block to every element matched by any selector in the list.
h1,
h2,
h3 {
margin-top: 0;
font-family: Georgia, serif;
}This rule is shorthand for three separate rules with identical declarations. Writing one per line is a convention, not a requirement. It makes diffs easier to read and makes a missing comma easier to spot.
The comma in a selector list is a different construct from the comma in a value such as font-family: Georgia, serif. In a value it separates alternatives for one property. In a selector it separates complete selectors.
Comma versus combinator
The comma and the descendant combinator (a space) are easy to mix up.
| Written | Meaning |
|---|---|
| `h1, h2` | Any `h1`, and also any `h2` |
| `h1 h2` | Any `h2` that is a descendant of an `h1` |
| `h1 > h2` | Any `h2` that is a direct child of an `h1` |
A stray space where you meant a comma produces a rule that rarely matches anything. It also raises no error.
Each selector is evaluated on its own
The browser treats each selector in a list as an independent rule that happens to share declarations. This has a direct consequence for specificity. A rule's specificity is not the sum or the maximum of the whole list. For a given element, the specificity is that of the most specific selector in the list that actually matches that element.
h2,
#intro {
color: gray;
}
.title {
color: green;
}Take this element:
<h2 class="title">Welcome</h2>The h2 selector matches, at (0,0,1). The #intro selector is in the same list, but it does not match this element, so its (1,0,0) is ignored. The .title rule has (0,1,0), which is higher, so the heading is green.
Now give the element both a matching ID and a class:
<h2 id="intro" class="title">Welcome</h2>Both h2 (0,0,1) and #intro (1,0,0) match, so the grouped rule counts as (1,0,0). It beats .title, and the heading is gray.
Source order and grouped rules
A grouped rule sits at one position in the stylesheet. When two selectors from the same list have equal specificity on the same element, the declarations are the same, so order between them does not matter. Order does matter relative to other rules.
.card h2,
.panel h2 {
color: navy;
}
.card h2 {
color: crimson;
}For an h2 inside .card, both rules have specificity (0,1,1). The second rule is later in the source, so it wins and the heading is crimson. For an h2 inside .panel, only the first rule matches, so it is navy.
If you split a list into separate rules, keep each one in the position that gives the cascade you want. Moving a selector between a group and its own rule can change which rule is "later".
Invalid selectors break the whole rule
This is the main gotcha. In a normal selector list, if any one selector is invalid, the browser drops the entire rule, including the valid selectors.
h1,
h2:not-a-real-pseudo-class {
color: red;
}Here h2:not-a-real-pseudo-class is not valid CSS, so the browser discards the whole rule. The h1 selector is perfectly good, but it receives no styling either.
Common ways to trigger this:
- A trailing comma, as in
h1, h2, { ... }. The empty slot after the last comma is invalid. - A pseudo-class or pseudo-element the browser does not support yet.
- A vendor-prefixed selector that only some browsers recognize.
- A typo such as
a:hovr.
The standard workaround is to write the risky selector as its own rule:
h1 {
color: red;
}
h2:not-a-real-pseudo-class {
color: red;
}Each rule is now judged separately, so the browser can discard the second without affecting the first. This is why stylesheets that must handle prefixed selectors, such as older placeholder-text selectors, repeat the declarations instead of grouping.
Forgiving lists inside `:is()` and `:where()`
The selector lists inside :is() and :where() are forgiving. An invalid selector inside them is ignored and the remaining selectors still work.
:is(h1, h2:not-a-real-pseudo-class) {
color: red;
}In a browser that supports :is(), this rule still colors every h1 red. The invalid part simply never matches.
Forgiveness is not free. A typo inside :is() is silently ignored, so you get no sign that anything went wrong. Use forgiving lists when you want resilience against unsupported syntax, and not as a way to hide mistakes.
Shortening repeated prefixes with `:is()`
Selector lists often repeat a common ancestor:
.article h2,
.article h3,
.article h4 {
line-height: 1.2;
}:is() removes the repetition:
.article :is(h2, h3, h4) {
line-height: 1.2;
}The two forms match the same elements, but the specificity can differ. :is() takes the specificity of its most specific argument. In this example the arguments are all type selectors, so both versions are (0,1,1). It changes when one argument is stronger:
.article :is(h2, #lead) {
line-height: 1.2;
}Because #lead is inside the list, this is (1,1,0) for every element that the rule matches, including a plain h2. This differs from a comma list, where each selector keeps its own specificity. Use :where() instead if you want the list to add zero.
| Form | Specificity for an `h2` |
|---|---|
| `.article h2, #lead` | (0,1,1) for `.article h2`, since `#lead` does not match |
| `.article :is(h2, #lead)` | (1,1,0) |
| `.article :where(h2, #lead)` | (0,1,0) |
Practical guidelines
- Group selectors that share the same intent and a similar specificity.
- Put risky or newly supported selectors in their own rule.
- Never leave a trailing comma.
- Format one selector per line so a missing comma is visible.
- Use
:is()for readability only when you have checked that the specificity change is acceptable.
Exercise
Given the CSS below and the element <p class="note" id="tip">Hi</p>, what color is the text, and why?
p, #other { color: blue; }
.note { color: green; }
p#tip { color: orange; }
Show answer
The text is orange. The p selector in the first rule matches (0,0,1), while #other does not match this element, so it does not count. The .note rule is (0,1,0). The p#tip rule is (1,0,1), which is the highest of the matching selectors, so orange wins regardless of source order.
Exercise
This rule styles nothing, not even the <h1> elements. Name two things wrong with it and fix it.
h1,
h2:hovr,
h3, {
color: teal;
}
Show answer
The pseudo-class :hovr is a typo, and the trailing comma after h3 leaves an empty selector. Either problem makes the whole selector list invalid, so the browser drops the entire rule. A fix is h1, h2, h3 { color: teal; }, or h2:hover if a hover state was intended, with the trailing comma removed.