Sub topic 12
Browser support and graceful fallbacks
This chapter assumes you can write :has() selectors, you know how @media and @supports at-rules wrap CSS rules, and you have written basic JavaScript with document.querySelector. It covers which browsers support :has(), how a browser treats a rule it cannot parse, and how to build pages that work well without :has() and look better with it.
Where support stands
:has() shipped in browsers in stages:
| Browser | First version with `:has()` | Release date |
|---|---|---|
| Safari (macOS and iOS) | 15.4 | March 2022 |
| Chrome and Edge | 105 | August 2022 |
| Firefox | 121 | December 2023 |
Firefox was the last engine to ship it. Since December 2023, :has() has worked in all current versions of the major browsers. It is part of the "Baseline" set of web features that work across browsers.
"Works in current browsers" is not the same as "works for all your users". Visitors on older Firefox releases, older iOS versions that cannot be updated, and embedded browsers in kiosks or in-app web views can all lack :has(). Check your analytics, then decide how much effort a fallback deserves. For many sites a plain, usable layout without the enhancement is enough.
What an unsupported browser does with your rule
CSS has a forward-compatible parsing rule. When a browser meets a selector it cannot parse, it drops that whole rule and continues with the next one. No error appears and nothing is logged. Take this rule:
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}A browser without :has() skips the entire block. The .card stays as it is, and it gets whatever the rest of your stylesheet gives it. This is safe by default. The enhancement is lost, and nothing else breaks.
The comma-list trap
Dropping the whole rule becomes a problem when you put :has() in a selector list. The browser treats the list as one unit. If any part is invalid, it drops the entire rule:
/* Wrong: in a browser without :has(), BOTH selectors stop working */
.notice,
.card:has(img) {
border: 1px solid #ccc;
}
/* Right: split them so the supported part survives */
.notice {
border: 1px solid #ccc;
}
.card:has(img) {
border: 1px solid #ccc;
}In the first version, .notice silently loses its border in older browsers. Keep :has() selectors in their own rules, or use @supports as shown below.
The same applies inside :is() and :where() lists in older engines. Never mix :has() with selectors you need everywhere.
Design the base styles first
A reliable fallback strategy has three steps:
- Write base styles that make every page state usable without
:has(). - Write the
:has()rules as improvements on top of that base. - Make sure the base and the enhancement do not fight each other.
Step 3 is where mistakes happen. If the base style sets a layout that the enhanced rule must fully undo, an unsupported browser shows the base and a supported browser shows the enhanced version. Both must be acceptable. Here is a card with one layout that works for both:
.card {
padding: 1rem;
}
.card img {
max-width: 100%;
height: auto;
}
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}Without :has(), the image stacks above the text, which is a normal card. With :has(), the layout becomes two columns. Neither version is broken.
Feature queries with @supports selector()
An @supports rule tests whether the browser can handle a feature. The selector() function tests whether it can parse a given selector. This is the correct tool for :has():
.card {
padding: 1rem;
}
@supports selector(:has(*)) {
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
}
}The test uses :has(*) because the query needs a valid argument. The * is only a placeholder. selector() itself is supported in Chrome 83, Firefox 69, and Safari 14.1, which all predate :has(). So the check is reliable in every browser that matters. A browser too old to know selector() skips the whole @supports block, which is also the result you want.
Why use @supports when unsupported rules are dropped anyway?
For a single standalone rule, the browser's own dropping behavior is already enough. @supports is worth adding in three cases:
- You want to group several
:has()rules in one place, so a reader sees which rules are enhancements. - You need styles that apply only when
:has()is absent. That needs@supports not. - You want to use a selector list that mixes
:has()and other selectors, because inside a supported block there is no risk of the list being dropped.
Fallback-only styles with @supports not
The negated form lets you write alternate styles for browsers without :has():
@supports not selector(:has(*)) {
.field label {
display: block;
font-weight: 600;
}
}Use this sparingly. Every fallback block is code you must maintain and test, and few people run those browsers.
Replacing a state-driven rule with JavaScript
Some :has() rules carry real behavior, such as the dark-mode toggle:
<label><input type="checkbox" id="dark-toggle"> Dark mode</label>
<main>Page content</main>:root:has(#dark-toggle:checked) {
color-scheme: dark;
}In a browser without :has(), the checkbox does nothing. If theme switching must work everywhere, add a class-based rule and use JavaScript only when :has() is missing:
:root.dark {
color-scheme: dark;
}const toggle = document.getElementById('dark-toggle');
if (!CSS.supports('selector(:has(*))')) {
toggle.addEventListener('change', () => {
document.documentElement.classList.toggle('dark', toggle.checked);
});
}CSS.supports() accepts the same selector(...) syntax as the CSS at-rule. The listener is attached only when the browser cannot do the job in CSS. Browsers with :has() keep the pure-CSS behavior, and older browsers get a working but script-driven version.
Detecting support before using the selector APIs
querySelector, querySelectorAll, matches, and closest parse their argument as a selector. When the browser cannot parse it, they throw a SyntaxError instead of returning nothing. An unguarded call can break your script:
function supportsHas() {
try {
document.querySelector(':has(*)');
return true;
} catch (error) {
return false;
}
}
const hasSupport = supportsHas();
const cards = hasSupport
? document.querySelectorAll('.card:has(img)')
: document.querySelectorAll('.card.has-image');In the fallback branch, the selector depends on a class that your server or build step adds to cards with images. This is the traditional approach, and it works in every browser. If you already maintain those classes, ask whether :has() still earns its place in the stylesheet.
CSS.supports('selector(:has(*))') does the same check in one line and is preferred when it is available. The try/catch version works even in browsers that lack CSS.supports with selector().
How the browser decides
The diagram shows what happens to one :has() rule at parse time.
The key point is that dropping a rule is not an error. The risk lies in a base layout that only makes sense with the enhancement, or in a selector list that loses more than you intended.
Common mistakes
- Putting
:has()in a selector list with other selectors. The whole list is dropped in older browsers. Split the rules. - Testing
@supports selector(:has())with an empty argument. An empty:has()is invalid, so the query returns false even in supporting browsers. Use:has(*). - Relying on
:has()for required behavior. A form that can only show errors through:has()hides them from users on older browsers. Pair such rules with a base style or a class. - Calling selector APIs without guarding.
querySelector(':has(...)')throws in unsupported browsers. - Testing only in your own browser. Test the fallback by temporarily renaming
:hasto something invalid in your stylesheet. That simulates the dropped rules without installing an old browser.
Deciding how much fallback you need
| Situation | Reasonable approach |
|---|---|
| Visual polish, such as a card layout | No fallback. The base layout is enough. |
| Several related enhancements | Group them in one `@supports selector(:has(*))` block. |
| Behavior that users depend on, such as a theme toggle | Add a class-based rule and JavaScript for browsers without `:has()`. |
| Scripts that call selector APIs | Guard with `CSS.supports` or `try/catch`. |
Match the effort to the cost of failure. If an older browser shows a plain card, the cost is low. If it hides an error message, the cost is high.
Exercise
A stylesheet contains .alert, .field:has(input:invalid) { color: crimson; }. A user on a browser without :has() support reports that alerts are no longer red. Explain why, and show the fix.
Show answer
The browser drops the whole rule when any selector in the list is invalid, so .alert loses its color along with the :has() selector. Split the list into two rules: .alert { color: crimson; } and .field:has(input:invalid) { color: crimson; }. The first now works in every browser, and the second is simply ignored where :has() is unsupported.
Exercise
Write a feature query that applies .card:has(img) { display: grid; } only in browsers that support :has(), and explain why the query uses :has(*) rather than :has().
Show answer
Use @supports selector(:has(*)) { .card:has(img) { display: grid; } }. The query needs a valid selector to test, and an empty :has() is invalid, so it would return false even in browsers that support :has(). The * is a placeholder argument that makes the test selector valid.