Sub topic 8
Selector lists and grouping
This chapter assumes you can write a basic rule with a type, class, or ID selector and that you know the (IDs, classes, types) specificity triple from earlier chapters.
Writing a selector list
A selector list is two or more selectors separated by commas. The browser applies the rule's declarations to every element that matches any selector in the list.
h1,
h2,
h3 {
font-family: Georgia, serif;
line-height: 1.2;
}This is shorthand for three separate rules with identical declarations. Put one selector per line, because it makes diffs and error hunting easier.
h1 { font-family: Georgia, serif; line-height: 1.2; }
h2 { font-family: Georgia, serif; line-height: 1.2; }
h3 { font-family: Georgia, serif; line-height: 1.2; }Each selector keeps its own specificity
A list does not merge its selectors into one. For a given element, the browser asks which selectors in the list match it. It uses the specificity of the most specific matching selector and ignores the rest.
h1,
.title,
#hero {
color: navy;
}Take the element <h1 id="hero" class="title">. All three selectors match it. The rule competes with specificity (1,0,0), the value of #hero. A plain <h1> without the ID competes at (0,0,1). The same rule therefore has different weight on different elements.
One invalid selector breaks the whole rule
In a normal rule, the list is not forgiving. If the browser can't parse one selector, it drops the entire rule, including the valid selectors.
/* The whole rule is ignored: ":unknown-state" is not a valid pseudo-class */
.card,
.panel:unknown-state {
border: 1px solid #ccc;
}This is a common cause of "my rule does nothing" when you use a new pseudo-class that an older browser doesn't know. The fix is to split the risky selector into its own rule.
.card {
border: 1px solid #ccc;
}
.panel:unknown-state {
border: 1px solid #ccc;
}Forgiving lists inside :is() and :where()
The lists inside :is() and :where() are forgiving. The browser ignores an invalid selector there and still uses the valid ones. These functions also shorten repeated prefixes.
:is(header, main, footer) :is(h1, h2) {
margin-block: 0.5em;
}This is equivalent to six combined selectors. As the previous chapter's examples showed, :is() takes the specificity of its most specific argument. :where() contributes zero.
Grouping versus separate rules
Group selectors when the declarations are the same and should change together. Keep rules separate when they merely happen to match today.
| Situation | Choice |
|---|---|
| Headings that must share a typeface | Group in one list |
| Two components with equal padding by coincidence | Separate rules |
| A selector using a very new feature | Separate rule |
Exercise
A rule reads p, .lead, #intro { color: teal; }. For <p class="lead">, which specificity does the rule use? What happens to the whole rule if you add a:not-a-real-pseudo to the list?
Show answer
The rule uses (0,1,0), the specificity of .lead, because it is the most specific selector that matches the element. p matches too but is weaker, and #intro does not match. Adding the invalid selector makes the browser drop the entire rule, so no element is colored teal.