Sub topic 12
Browser support and progressive enhancement with @supports
This chapter covers how to ship container queries, container query units, and style queries to browsers that may not support them. It assumes you have read the earlier chapters on container-type, @container rules, cqi and related units, and style() queries. It also assumes you know @supports at a basic level, clamp(), and how the cascade orders competing declarations.
What happens in a browser that lacks the feature
CSS is built to be forward compatible. A browser that does not understand part of a stylesheet skips that part and keeps going. Three behaviors matter here:
- An unknown property or value is dropped.
container-type: inline-sizein an old browser is ignored, and the element stays an ordinary box. - An unknown at-rule block, or one whose condition cannot be parsed, does not apply. Everything inside it is skipped.
- A declaration with an invalid value is dropped at parse time.
font-size: clamp(1rem, 4cqi, 2rem)is invalid wherecqiis not a known unit, so the browser behaves as if the line were absent.
The third case makes ordinary cascade fallbacks work. If you write a safe declaration first and the enhanced one after it, an old browser keeps the first and a new browser uses the second.
.card h2 {
font-size: 1.25rem; /* used if the next line is dropped */
font-size: clamp(1.125rem, 4cqi, 2rem);
}This is the cheapest form of progressive enhancement. It needs no @supports at all.
Support at a glance
Support differs between the three features. Check a current source such as MDN's compatibility tables or caniuse before you decide how much effort a fallback deserves. The table below is a general picture, not a guarantee.
| Feature | Rough support picture | Failure in an unsupported browser |
|---|---|---|
| `container-type` and size `@container` queries | All current major browsers, since 2022 to early 2023 | Property and rule are ignored |
| Container query units (`cqi`, `cqw` and so on) | Shipped together with size queries | Declaration using the unit is invalid and dropped |
| `@container style()` on custom properties | Narrower. Chromium-based browsers first, other engines later. Verify per browser | Condition is not matched, so the rule does not apply |
Size queries are widely available. Style queries are the feature where you should still plan a fallback for real users.
The key design rule: the default must stand alone
Write the base styles so the component is usable without any container feature. Then add container features as upgrades.
- Put the single-column, readable layout outside every
@containerrule. - Put the multi-column layout inside the
@containerrule. - Treat style queries as decoration or variants, not as the only place essential information lives.
For the card from earlier chapters, the base is display: block. The grid layout is inside @container card (min-width: 30rem). A browser that ignores the rule still shows a stacked card that works at every width.
Because unsupported rules are skipped anyway, you may ask why @supports is needed. The next section answers that.
When `@supports` is worth using
@supports is a conditional group rule. Its block applies only if the browser understands the declaration in the condition. You need it when the fallback and the enhancement must be mutually exclusive, so that the old layout is actively replaced rather than just left in place.
There are three typical cases:
- You want to apply a fallback that exists only for old browsers, such as a viewport media query.
- You want to avoid applying a rule that would behave badly if only half the feature set were understood.
- You want to group the whole enhancement in one place so the intent is obvious to the next reader.
Grouping the enhancement
This example is the pattern from the plan. Everything that depends on containers sits inside one block.
.card {
display: block;
}
@supports (container-type: inline-size) {
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}
}The markup is the same wrapper and article used before.
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>@container is allowed inside @supports because both are conditional group rules, and conditional group rules can nest.
Testing a value, not only a property
@supports tests whether a declaration parses. That means you can test a unit by putting it in any property that accepts lengths.
@supports (width: 1cqi) {
.card {
padding: 2cqi;
}
}This is a more precise guard for unit-based styling than container-type. A browser could in theory support one but not the other, although in practice they shipped together.
Supplying a media query fallback
@supports not (...) lets you serve a different strategy to browsers without container queries. Here the fallback uses the viewport as a rough stand-in.
.card {
display: block;
}
@supports (container-type: inline-size) {
.card-wrapper {
container: card / inline-size;
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}
}
@supports not (container-type: inline-size) {
@media (min-width: 48rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}
}The viewport rule is a guess. It can be wrong when the card sits in a narrow sidebar on a wide screen. That is acceptable for a fallback, because the aim is a layout that is reasonable, not identical.
The decision flow
The flow below shows how a browser treats a component built this way.
Fallbacks for container units
Units need two layers of protection, and it helps to keep them separate.
First, the browser may not know the unit. Use the cascade fallback shown at the start. The earlier declaration is the one an old browser keeps.
Second, the browser knows the unit but no container applies. In that case the units resolve against the small viewport size, as covered in the earlier chapter on unit fallback. That is not a failure. It still gives a usable length. It does mean cqi can behave like a viewport unit if you forget to declare a container. If you want a different value in that case, you cannot detect it with @supports, because the unit parses fine. The fix is to make sure a container exists, or to use a custom property fallback.
.card-wrapper {
container: card / inline-size;
}
.card {
padding: 1rem;
padding: 2cqi;
}
.card h2 {
font-size: 1.25rem;
font-size: clamp(1.125rem, 4cqi, 2rem);
}With clamp() the enhanced value is bounded, so even the viewport-based result in the no-container case stays between 1.125rem and 2rem. The bounds give you a safety net you do not get with a bare 4cqi.
Fallbacks for style queries
Style queries need a different approach, because you cannot reliably detect them with @supports. The @supports condition tests properties and selectors. It does not test @container conditions. A proposed at-rule() test exists in specifications, but do not rely on it without checking current support.
You have two practical options.
Option 1: let the base styles be the fallback
If a browser does not match style(), the rule simply does not apply. Write the default so it is acceptable.
.alert-region {
--tone: warning;
}
.alert {
border: 2px solid gray;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
background: #fff4e0;
}
}In a browser without style queries, the alert keeps its gray border. The message is still readable. The orange tone is an upgrade.
Option 2: duplicate the signal with a class or attribute
If the tone carries meaning that users must not miss, do not depend on the style query alone. Set an attribute in markup and style it with a normal selector. Use the style query only to avoid repeating the logic for components deep in the tree.
.alert[data-tone="warning"] {
border-color: orange;
background: #fff4e0;
}This costs some duplication, but the information reaches every browser.
Detecting support from JavaScript
Sometimes script needs to know, for example to load a polyfill or choose a different component. CSS.supports() accepts the same tests as @supports.
const hasContainerQueries = CSS.supports("container-type: inline-size");
const hasContainerUnits = CSS.supports("width: 1cqi");
document.documentElement.classList.toggle(
"no-container-queries",
!(hasContainerQueries && hasContainerUnits)
);
console.log({ hasContainerQueries, hasContainerUnits });A class on the root element lets you write fallbacks that do not depend on @supports ordering. In most projects the CSS-only approach is simpler and avoids a flash of unstyled layout.
Containment changes behavior, even when you do not notice
One more reason to treat the enhancement as a unit: setting container-type: inline-size applies containment. The element's inline size can no longer depend on its contents, and layout, style, and size containment effects apply. If you place container-type in the base rule instead of inside @supports, supporting browsers get those effects whether or not any @container rule matches. Test the wrapper in a supporting browser, and keep an eye on the collapse problem from the pitfalls chapter, where a container with no explicit inline size can shrink to zero in contexts like a shrink-to-fit flex item.
Practical checklist
- Write the single-column, no-feature layout first, outside any container rule.
- Declare
container-typeand@containerrules inside@supports (container-type: inline-size)when the fallback must differ. - Put a safe declaration before each declaration that uses
cqi,cqw, or another container unit. - Wrap unit-based values in
clamp()so the no-container case stays bounded. - Treat style queries as an enhancement. Carry essential meaning in markup or text.
- Check current browser data before choosing how far to go. Support changes, and this chapter's table will age.
Exercise
A teammate puts font-size: clamp(1rem, 4cqi, 2rem); as the only font-size declaration on .card h2. A user on a browser that does not know the cqi unit opens the page. What font size does the heading get, and how would you change the CSS so it is a deliberate choice rather than an accident?
Show answer
The declaration is invalid because of the unknown unit, so the browser drops it. The heading falls back to the inherited or default size, which is probably not what the designer intended. Add a safe declaration before it, for example font-size: 1.25rem; followed by font-size: clamp(1rem, 4cqi, 2rem);. Browsers that understand cqi use the second line, and the others keep the first.
Exercise
You add @container style(--tone: warning) { .alert { border-color: orange; } } to a stylesheet. Can you guard it with @supports so that unsupporting browsers get a different style? If not, what should you do instead?
Show answer
Not reliably. @supports tests declarations and selectors, not @container conditions, so it cannot tell you whether style() queries work. Make the default .alert style acceptable on its own, so a browser that never matches the style query still shows a usable alert. If the tone carries essential meaning, also set it with a class or attribute such as data-tone="warning" and style that with a normal selector.
Exercise
Why does the media query fallback in @supports not (container-type: inline-size) count as an approximation, and where does it fail?
Show answer
A media query measures the viewport, while a container query measures the component's wrapper. They agree only when the component spans the page. If the card sits in a narrow sidebar on a wide screen, the media query sees a wide viewport and applies the two-column layout to a card that has little room. The fallback is acceptable because it only has to be reasonable for older browsers, not identical to the enhanced result.