Sub topic 9
Forgiving parsing of the selector list
This chapter assumes you know selector lists with commas, the basic selector types, and how specificity decides conflicts. It also assumes you have seen the basic syntax of :is() and :where().
How a normal selector list fails
A normal selector list is all-or-nothing. If one selector in the list is invalid, the browser drops the entire rule. The invalid selector might be a typo, or a pseudo-class the browser doesn't know yet.
/* One unknown selector invalidates the whole rule, so nothing matches */
a,
:unsupported-pseudo {
color: red;
}Here even plain links stay unstyled, even though a is valid.
What forgiving parsing changes
The argument list of :is() and :where() is parsed in a forgiving way. The browser discards any selector it can't parse and keeps the valid ones.
/* Forgiving parsing ignores the unknown selector, so links still match */
:is(a, :unsupported-pseudo) {
color: red;
}Links turn red. The unknown selector is treated as if you had never written it.
The diagram below shows the two outcomes.
Why this is useful
Forgiving parsing helps when you want to use a selector that only some browsers understand. A vendor-prefixed pseudo-class is a typical case. Put it inside :is() next to a standard selector, and a browser that doesn't know the prefixed one still applies the rule to the standard selector.
An ignored selector also contributes nothing to specificity. Only the valid arguments are considered when :is() picks its most specific argument.
Pitfalls
Forgiveness has a cost: mistakes stay silent. A misspelled class selector is still valid, so it simply never matches. A misspelled pseudo-class is invalid, so it is dropped without any warning. If your rule seems to apply to only some of the elements you listed, check the list for a typo.
Pseudo-elements are the most common invalid argument. They are not allowed inside the list, so the browser ignores them. Because the whole list is then empty, the rule matches nothing.
/* Pseudo-elements go outside the function */
:is(p, li)::first-line {
font-weight: bold;
}
/* A pseudo-element inside the argument list never matches */
:is(p::first-line, li::first-line) {
font-weight: bold;
}The second rule is not an error. It is a valid rule with an empty effective list, so it silently matches nothing.
Three more points to remember:
- Forgiveness applies only to the argument list. The selector outside the function, such as
articleinarticle :is(h1, h2), is parsed strictly. An invalid part there drops the whole rule. - A browser that doesn't support
:is()at all does not give you forgiveness. It sees the whole selector as invalid and drops the rule. :not()and:has()do not share this behavior. Their lists are parsed strictly, so one invalid selector in them invalidates the rule. Don't assume the forgiving behavior carries over to them.
Quick reference
| Selector | One invalid item | Result |
|---|---|---|
| `a, :bad` | `:bad` | Whole rule dropped |
| `:is(a, :bad)` | `:bad` | `a` still matches |
| `:where(a, :bad)` | `:bad` | `a` still matches, at zero specificity |
| `:not(a, :bad)` | `:bad` | Whole rule dropped |
Exercise
A stylesheet contains :is(h2, h3::first-line) { color: teal; }. Which elements turn teal, and why?
Show answer
All h2 elements turn teal. The h3::first-line argument contains a pseudo-element, which is invalid inside the list. Forgiving parsing discards it instead of dropping the rule, so only h2 remains in the list.
Exercise
You write :is(.card, .tile):hovr { outline: 2px solid; }. Does forgiving parsing rescue the rule? Why or why not?
Show answer
No. The typo :hovr is outside the :is() argument list, where parsing is strict. The browser sees an invalid selector and drops the whole rule.