Sub topic 14
Browser support and legacy names
This chapter assumes you know how :is() and :where() work and how specificity is calculated. It covers which browsers support them, which older names existed, and how to write safe fallbacks if you still need them.
Current support
:is() and :where() have worked in every major browser since late 2020 and 2021. The first versions that support them are:
| Browser | First version with `:is()` and `:where()` |
|---|---|
| Chrome | 88 |
| Edge | 88 |
| Firefox | 78 |
| Safari | 14 |
Most projects can use both without a fallback. Check your analytics or your project's browserslist before deciding. Only sites that must support browsers from before 2021 need the techniques below.
Why a missing pseudo-class is a problem
A browser that does not recognize :is() treats the selector as invalid. In a normal selector list, one invalid item invalidates the entire rule. If you group :is() with other selectors, an old browser drops all of them:
/* In a browser without :is(), the whole rule is discarded,
including the h1 selector, which it could have matched */
h1,
:is(h2, h3) {
margin-block: 1rem;
}The forgiving parsing you learned earlier only applies inside the parentheses of :is() and :where(). It does not protect the list around them.
The first rule is to keep :is() and :where() rules separate from rules you need everywhere.
Legacy names
Before :is() was standardized, browsers shipped prefixed versions of the same idea:
:-webkit-any()in Chrome and older Safari.:-moz-any()in Firefox.:matches()in early Safari versions and in older drafts of the specification.
These names are obsolete. Their behavior was not identical to :is(). For example, they had narrower rules about which selectors were allowed in the argument list, and they did not use forgiving parsing. Avoid them in new code. If you meet them in an old stylesheet, you can usually replace them with :is() once your browser targets allow it.
Never put two vendor names in one selector list. The browser discards the whole rule because it does not know one of the names. Use a separate rule for each:
/* Each rule stands alone, so an unknown name only drops its own rule */
article :-webkit-any(h1, h2) a {
text-decoration: underline;
}
article :-moz-any(h1, h2) a {
text-decoration: underline;
}
article :is(h1, h2) a {
text-decoration: underline;
}Testing for support with @supports
The @supports selector() condition lets you apply a rule only when the browser understands a selector. A browser too old to know @supports selector() skips the block, which is the safe outcome.
/* Baseline: works everywhere */
article h1 a,
article h2 a {
color: inherit;
}
/* Enhancement: only applied where :is() is understood */
@supports selector(:is(a, b)) {
article :is(h1, h2) a {
text-decoration: underline;
}
}This keeps the baseline styles in a rule that every browser reads. You then add the compact :is() version as an enhancement.
A practical checklist
- Look up your minimum browser versions.
- If every target is Chrome 88, Firefox 78, Safari 14 or newer, use both pseudo-classes directly.
- If not, keep rules with
:is()or:where()out of shared selector lists. - Add a plain fallback rule or an
@supports selector()block.
Exercise
A stylesheet contains h1, :is(h2, h3) { margin-block: 1rem; }. In a browser that does not support :is(), which headings get the margin, and how would you fix the rule?
Show answer
None of them. The unknown :is() invalidates the whole selector list, so the h1 part is dropped too. Fix it by moving :is(h2, h3) into its own rule, or by writing h1, h2, h3 as plain selectors for the baseline and putting any :is() version inside @supports selector(:is(a, b)).