Sub topic 3
Forgiving Parsing of the Selector List
Assume you know basic selectors, combinators, selector lists, the cascade, and specificity. This chapter focuses on edge cases around :is() and :where().
Forgiving parsing
A normal selector list can fail when one selector is invalid:
a,
:unsupported-pseudo {
color: red;
}Because the list contains an invalid selector, the rule may be discarded. :is() and :where() use forgiving parsing. The browser ignores invalid arguments and keeps valid ones:
:is(a, :unsupported-pseudo) {
color: red;
}This rule still matches links. If every argument is invalid, the functional pseudo-class matches nothing.
Limits and common pitfalls
Pseudo-elements cannot appear inside the argument list:
:is(p, li)::first-line {
font-weight: bold;
}This is valid. The following selector matches nothing:
:is(p::first-line, li::first-line) {
font-weight: bold;
}Keep the pseudo-element outside :is() or :where(). Also remember that these functions group alternatives; they do not create a new element or change combinator meaning.
CSS nesting and `:is()`
A nested selector list is conceptually similar to wrapping the parent list in :is():
.card,
#hero {
.title {
color: crimson;
}
}This behaves approximately like :is(.card, #hero) .title. The most specific parent alternative affects the nested rule, so the ID can raise its specificity.
Related pseudo-classes and layers
:not() and :has() follow the same specificity pattern as :is(): each takes the specificity of its most specific argument. :where() alone always contributes zero specificity.
Cascade layers offer another way to control conflicts. A rule in a later layer can override an earlier layer even when its selectors have different specificity. Use layers for stylesheet-wide ordering, and use :where() for low-specificity component defaults.
Browser support and legacy names
Modern evergreen browsers support :is() and :where(). Older implementations used names such as :matches() or vendor-prefixed :any(). These names are not reliable replacements today. Check the browsers you support before using forgiving parsing as a compatibility technique.
Exercise
Why can :is(a, :unsupported-pseudo) still match links?
Show answer
Its forgiving selector list ignores the invalid argument and retains the valid a argument.