Sub topic 12
Browser support and graceful fallbacks
This chapter assumes you know :has() syntax, specificity, and @supports at a basic level. It covers which browsers support :has(), how to detect support, and how to keep a page usable where support is missing.
Where :has() works
:has() shipped in Safari 15.4 (March 2022) and Chromium browsers 105 (August 2022). Firefox added it in version 121 (December 2023). Since then every current major browser supports it, and it counts as "Baseline" (supported across the major engines). Older browsers, such as Firefox 120 and earlier or older enterprise-managed browsers, do not support it.
Check a compatibility table such as caniuse.com or MDN against your own audience's analytics before deciding how much fallback work you need.
What happens in an unsupporting browser
A browser that does not recognize :has() treats the selector as invalid. It then discards the whole rule. The page does not break, but the rule never applies.
This leads to a gotcha with selector lists. In the next rule, an old browser discards the entire rule, including the .plain part, because one selector in the list is invalid.
.plain,
.card:has(img) {
padding: 1rem;
}Keep :has() selectors in their own rules so that other selectors are not lost along with them.
Progressive enhancement with @supports selector()
The safest pattern is to write base styles that work everywhere. Then put the :has() rules inside @supports selector(:has(*)).
.card {
padding: 1rem;
}
@supports selector(:has(*)) {
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}
}The selector() function asks the browser whether it can parse the given selector. The argument :has(*) is a minimal valid use. @supports selector() itself has been available since 2019 to 2021 in all major engines, so it is safe to use as the test.
You can also target only the browsers that lack support:
@supports not selector(:has(*)) {
.card {
border: 1px solid #ccc;
}
}Design the fallback first
Treat the base styles as the real design and the :has() rules as an improvement. Some patterns degrade well, and others do not.
| Pattern | Without `:has()` | Fallback idea |
|---|---|---|
| Card layout with image | Single column | Acceptable as is |
| Invalid field label color | No color change | Use `input:invalid` on the input itself |
| Dark mode toggle on `:root` | Toggle does nothing | Set the theme with JavaScript |
A toggle that does nothing is worse than a missing decoration. For features like that, provide a JavaScript path.
Feature detection in JavaScript
CSS.supports() accepts the same selector() test, so a script can add a class only when it is needed.
const hasSupport = CSS.supports('selector(:has(*))');
if (!hasSupport) {
const toggle = document.querySelector('#dark-toggle');
toggle.addEventListener('change', () => {
document.documentElement.classList.toggle('dark', toggle.checked);
});
}Pair this with a plain .dark class rule in your CSS. The :root:has(#dark-toggle:checked) rule then handles modern browsers, and the class handles older ones.
Common mistakes
- Putting
:has()rules first and base styles afterward, so there is no baseline. - Writing
@supports selector(:has())with an empty argument. An empty:has()is invalid, so the test fails everywhere. Use:has(*). - Depending on
:has()for essential behavior such as showing or hiding required content, with no fallback.
Exercise
A stylesheet contains h2, .note:has(a) { margin: 0; }. In a browser without :has() support, which elements receive margin: 0, and how would you rewrite the rule so h2 keeps its style?
Show answer
None of them. The browser treats :has(a) as invalid and discards the whole rule, including h2. Split it into h2 { margin: 0; } and a separate @supports selector(:has(*)) { .note:has(a) { margin: 0; } }.
Exercise
Write the condition that applies a rule only in browsers that do not support :has(), and name the JavaScript call that performs the same test.
Show answer
@supports not selector(:has(*)) { ... } is the CSS condition, and CSS.supports('selector(:has(*))') is the JavaScript call.