Sub topic 10
Limits and Common Pitfalls
:is() and :where() shorten selectors, and the earlier chapters showed where they help. This chapter covers where they stop working and where they surprise people. It assumes you know the following:
- Basic selectors and combinators.
- The specificity triple written as (ID, class, type).
- How
:is()and:where()differ in specificity. - The forgiving selector list from the previous chapter.
Most of the pitfalls below come from one fact. Both pseudo-classes take a list, but the browser handles that list differently from a normal comma-separated selector list.
Pseudo-elements do not belong in the argument list
Pseudo-elements such as ::before, ::after and ::first-line are not valid inside :is() or :where(). The same goes for ::marker, ::placeholder and the rest. Put them after the function instead.
/* Works: the pseudo-element follows the function */
:is(p, li)::first-line {
font-weight: bold;
}
/* Never matches: pseudo-elements are invalid inside the list */
:is(p::first-line, li::first-line) {
font-weight: bold;
}The second rule produces no error. Forgiving parsing drops both invalid items, which leaves an empty list, and an empty list matches nothing. The stylesheet loads and the rule has no effect.
Pseudo-classes such as :hover, :focus-visible and :nth-child() are allowed inside the list.
:is(a:hover, button:hover, summary:hover) {
cursor: pointer;
}Specificity comes from the strongest argument, not the matching one
:is() takes the specificity of its most specific argument. The browser does this when it reads the selector. It does not wait to see which argument matched the element.
:is(#main, .note) p {
color: blue;
}
.note p {
color: red;
}<div class="note">
<p>Which color am I?</p>
</div>The paragraph sits inside .note and not inside #main. Even so, the first rule has specificity (1,0,1) because of #main. The second rule has (0,1,1). The text is blue, which is usually not what the author expected.
There are two common fixes:
- Split the rule so the ID is not in the list:
#main p, .note p { ... }. Each item then keeps its own specificity. - Remove the ID from the list and use a class, so every argument has similar weight.
A list that mixes arguments of very different specificity is a warning sign. If you cannot say what the list's specificity is without thinking, split it.
Forgiving parsing can hide mistakes
In a normal selector list, one invalid item invalidates the whole rule. Inside :is() and :where(), the browser drops only the invalid item.
/* Whole rule is dropped */
a, :unsupported-pseudo { color: red; }
/* Links still match */
:is(a, :unsupported-pseudo) { color: red; }This helps with new selectors in older browsers. The same behavior also hides typos. In :is(.card, :hovr) the typo :hovr is skipped without a warning, and the rule matches every .card all the time. A rule that looks like a hover style then applies permanently.
| Situation | Plain selector list | Inside `:is()` or `:where()` |
|---|---|---|
| One item uses an unknown pseudo-class | Whole rule ignored | That item ignored, others match |
| One item contains a pseudo-element | Valid (pseudo-elements are allowed) | That item ignored |
| Every item is invalid | Whole rule ignored | Matches nothing |
| Empty list, such as `:is()` | Invalid | Valid, matches nothing |
When a rule with :is() has no effect, test each argument on its own in a plain selector. A broken item shows up straight away that way.
Leading combinators are not allowed
The arguments are complex selectors that you could use on their own. A selector that starts with a combinator is relative, and relative selectors belong to :has().
/* The item "> p" is invalid here and is silently dropped */
.box :is(> p, > ul) {
margin-block: 1rem;
}
/* Put the combinator outside the function */
.box > :is(p, ul) {
margin-block: 1rem;
}
/* Or use :has() when you want to select the parent */
.box:has(> p) {
border: 1px solid gray;
}Combinators in the middle of an argument are fine. :is(nav a, footer a) is valid.
Zero specificity loses to everything, including `*`
:where() contributes (0,0,0). That makes it easy to override, but it also means the rule sits at the bottom of the author cascade. The universal selector * also has zero specificity, so a tie is decided by source order.
:where(a) {
color: navy;
}
* {
color: black;
}Every link is black here. Both rules have specificity (0,0,0), and * comes later. Reverse the order and the links are navy, because the type selector inside :where() still sets which element the rule matches. It adds nothing to the weight.
Three consequences follow:
- Put
:where()defaults early in the stylesheet, before any broad rules. - Do not rely on
:where()to win a conflict. It can only supply a default. - Author styles beat the browser's built-in styles whatever the specificity. A
:where()rule overrides browser defaults, such as the margin onul, even at zero.
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}The .menu rule wins on specificity alone, in either source order.
Nesting follows `:is()` rules
In nested CSS, a parent selector list behaves like :is() around the list. That brings the same "strongest argument" behavior.
.card,
#hero {
.title {
color: crimson;
}
}
/* Behaves like :is(.card, #hero) .title, with specificity (1,1,0) */A .card .title rule written in this form carries ID-level weight, because #hero is in the parent list. Keep unrelated parents in separate rules when their specificity differs a lot.
Large lists hide how many selectors you wrote
Each :is() multiplies the number of selectors it stands for. The following looks short but expands to eight combinations:
:is(main, aside) :is(h2, h3) :is(a, button) {
text-decoration: none;
}The rule is correct, but it is harder to read and to override than it looks. Specificity is still just (0,0,3), because each list contributes its strongest argument, a type selector. Matching cost is not the concern here. Browsers handle these selectors well. The cost is how hard the rule is for a person to reason about. If you cannot quickly list which elements it targets, write separate rules.
A checklist for a rule that does nothing
Summary of pitfalls
| Pitfall | Symptom | Fix |
|---|---|---|
| Pseudo-element inside the list | Rule never matches | Place it after the function |
| ID or other strong argument in `:is()` | Rule overrides more than expected | Split the list |
| Typo in an argument | Item silently ignored | Test each argument alone |
| Leading combinator inside the list | Item ignored | Put the combinator outside or use `:has()` |
| `:where()` against `*` | Default is overridden by source order | Declare defaults first |
| Parent list with an ID in nesting | Nested rule is heavier than it looks | Separate the rules |
Exercise
This rule is meant to bold the first line of every paragraph and list item, but nothing changes. Why, and how do you fix it? :is(p::first-line, li::first-line) { font-weight: bold; }
Show answer
Pseudo-elements are invalid inside the argument list. Forgiving parsing drops both items, so the list is empty and matches nothing. Move the pseudo-element outside: :is(p, li)::first-line { font-weight: bold; }.
Exercise
A page has <div class="note"><p>Text</p></div>. One rule is :is(#main, .note) p { color: blue; } and a later one is .note p { color: red; }. There is no element with the ID main. What color is the text, and why?
Show answer
Blue. The :is() rule takes the specificity of its strongest argument, #main, so it is (1,0,1) even though only .note matched. The later rule is only (0,1,1), so source order does not help it.
Exercise
You write :where(a) { color: navy; } at the top of a stylesheet, and later * { color: black; }. Links are black. Why?
Show answer
Both rules have specificity (0,0,0). When specificity ties, the later rule wins, so * overrides the :where() default. Moving the :where() rule after the * rule, or putting defaults before broad rules, changes the result.