Sub topic 9
Forgiving parsing of the selector list
This chapter assumes you know basic selectors, combinators and comma-separated selector lists. It also assumes you know how specificity decides conflicts, and that you have met :is() and :where() as ways to match alternatives. Here you will learn what happens when one item in the list is invalid, and why that differs from an ordinary selector list.
The problem: one bad selector breaks the whole rule
An ordinary selector list is strict. If any selector in the list fails to parse, the browser discards the entire rule, including the selectors that were valid.
/* One unknown selector invalidates the whole rule, so nothing matches */
a,
:unsupported-pseudo {
color: red;
}The browser does not recognize :unsupported-pseudo, so it throws away the whole rule. Plain a elements do not turn red either. This is a deliberate design: CSS ignores rules it cannot fully understand, so that new syntax in older browsers fails safely.
The cost shows up when you write one rule that mixes old and new selectors. Authors used to split these into separate rules, one selector per rule, so that a single unsupported selector could not take down the others.
What forgiving parsing changes
The argument of :is() and :where() is a forgiving selector list. The browser parses each comma-separated item on its own. If an item is invalid, the browser drops that item and keeps the rest.
/* Forgiving parsing ignores the unknown selector, so links still match */
:is(a, :unsupported-pseudo) {
color: red;
}Here :unsupported-pseudo is dropped, and the rule behaves as :is(a). Links turn red.
The rule stays valid in every case below:
- Some items are valid and some are not. Only the valid ones can match.
- Every item is invalid. The rule is still syntactically valid, but the list matches nothing.
- The list is empty, as in
:is(). This is valid too and matches nothing.
/* All three are valid rules. None of them matches any element. */
:is() {
color: red;
}
:is(:unsupported-pseudo) {
color: red;
}
:where(p::first-line) {
color: red;
}The last example matters. A pseudo-element is not allowed inside the argument list, so the item is treated as invalid and dropped. Because the item is dropped instead of the whole rule, the rule fails quietly. We return to this in the pitfalls section.
Specificity when an item is dropped
A dropped item contributes nothing to specificity, because it is no longer part of the list. Recall the rule for :is(): the specificity is that of the most specific item in the list. If the most specific item was invalid and got dropped, the next most specific valid item sets the value.
/* #main is valid, so this rule has specificity (1,0,1) */
:is(#main, .note) p {
color: blue;
}
/* ::first-line is invalid inside :is(), so it is dropped.
The most specific remaining item is .note, giving (0,1,1). */
:is(#main::first-line, .note) p {
color: blue;
}In the second rule the whole #main::first-line item is invalid, because pseudo-elements are not allowed. The ID does not count, even though it appears in the source. If you debug a specificity surprise, check for items that quietly disappeared.
A practical use: mixing newer and older selectors
Forgiving parsing helps when a browser supports :is() but not one of the selectors inside it. A common case is a state selector that has not shipped everywhere yet.
:is(button, a):is(:hover, :focus-visible, :some-future-state) {
outline: 2px solid currentColor;
}A browser that does not know :some-future-state still applies the outline for :hover and :focus-visible. Without :is(), the unknown selector would invalidate the whole rule and you would have to repeat it in a separate rule.
This only helps if the browser supports :is() itself. A browser that predates :is() fails to parse the whole rule, so it is discarded. Forgiving parsing is not a fallback for missing :is() support. The last chapter of this tutorial covers browser support and the older :-webkit-any() and :-moz-any() names.
What counts as invalid
An item is invalid if the browser cannot parse it as a complex selector. Typical causes:
- An unknown pseudo-class or an unknown attribute-selector syntax, such as
:unsupported-pseudo. - A pseudo-element, such as
::beforeor::first-line. These are not allowed inside:is()and:where(). - A selector that uses an undeclared namespace prefix.
Forgiveness covers invalid items, not broken syntax around them. A stray unmatched bracket or parenthesis can still make the parser lose its place, so the surrounding rule may be discarded anyway. Do not treat forgiving parsing as protection against typos in general.
Forgiving versus strict: where else it applies
Not every functional pseudo-class forgives. The distinction matters when you combine them.
| Selector | Invalid item in the list | Result |
|---|---|---|
| `a, :bad` | `:bad` | The whole rule is discarded |
| `:is(a, :bad)` | `:bad` | `:bad` is dropped, `a` still matches |
| `:where(a, :bad)` | `:bad` | `:bad` is dropped, `a` still matches, specificity stays zero |
| `:not(a, :bad)` | `:bad` | The whole rule is discarded |
| `:has(a, :bad)` | `:bad` | The whole rule is discarded |
:not() takes a normal, strict selector list. :has() was originally specified as forgiving, but the specification later changed it to strict, and current browsers treat it that way. Do not rely on forgiving behavior inside either one. If you need a forgiving wrapper inside :not(), nest an :is():
/* The inner :is() forgives :unsupported-pseudo.
The rule is valid and excludes links only. */
p:not(:is(.intro, :unsupported-pseudo)) {
margin-block: 1rem;
}Be careful with the result. The :unsupported-pseudo item is dropped, so the exclusion covers only .intro. If the dropped item was meant to exclude something, you will get matches you did not intend.
Pitfalls specific to forgiving parsing
Forgiveness trades a loud failure for a silent one. Three situations cause trouble.
- Pseudo-elements inside the list. The rule is accepted but matches nothing. Put the pseudo-element after the function.
/* 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;
}- Vendor-prefixed pseudo-elements. You cannot group
::-webkit-scrollbarwith::-moz-scrollbar-likeselectors inside:is(), because both are pseudo-elements. Keep each in its own rule. This is the old reason for repeating rules, and:is()does not remove it.
- Typos. A misspelled pseudo-class such as
:hovris simply dropped. In a top-level selector list the rule would fail entirely and DevTools would show it struck through or missing. Inside:is()the rule is shown as valid and just matches fewer elements. When styles are missing, read each item in the list slowly.
Quick checklist
- Use
:is()or:where()when you want unsupported or uncertain selectors to degrade without breaking the rest. - Remember that dropped items have no effect on specificity or matching.
- Do not put pseudo-elements inside the list.
- Do not expect
:not()or:has()to forgive. Wrap an inner:is()if you must, and check what the dropped item would have excluded. - When a rule seems to do nothing, suspect an item that was silently dropped.
Exercise
A stylesheet contains h1, h2, :made-up-state { margin: 0; } and a second rule :is(h1, h2, :made-up-state) { margin: 0; }. In a browser that supports :is() but not :made-up-state, which headings get margin: 0 from each rule?
Show answer
The first rule is a strict selector list, so the unknown selector invalidates the whole rule and neither h1 nor h2 gets the margin from it. The second rule uses forgiving parsing, so :made-up-state is dropped and the rule behaves as :is(h1, h2). Both h1 and h2 get margin: 0.
Exercise
What is the specificity of :is(#hero::before, .banner) p, and why?
Show answer
It is (0,1,1). The item #hero::before contains a pseudo-element, which is not allowed inside :is(), so it is dropped as invalid and its ID does not count. The most specific remaining item is .banner, which contributes (0,1,0), and the p adds (0,0,1).
Exercise
You write p:not(:unsupported-pseudo, .intro) { color: gray; }. Does :not() forgive the unknown selector? How could you change the rule so the unknown item does not invalidate the rest?
Show answer
No. :not() takes a strict selector list, so the unknown selector invalidates the whole rule and no paragraphs turn gray. To tolerate the unknown item, wrap the list in :is(): p:not(:is(:unsupported-pseudo, .intro)). The unknown item is then dropped, and the rule excludes only .intro.