Sub topic 12
Browser support and progressive enhancement
This chapter assumes you can write @container rules, declare containers with container-type, and use @supports and @media at a basic level. It covers where container queries work, what unsupported browsers do with your CSS, and how to layer the feature on so every visitor gets a usable page.
Where support stands
Size container queries (container-type: size and inline-size, @container with width and height conditions, and the cq* units) ship together. They have been in every major browser engine since early 2023:
| Engine | First version with size container queries |
|---|---|
| Chromium (Chrome, Edge) | 105 |
| Safari (WebKit) | 16 |
| Firefox (Gecko) | 110 |
Style queries are a separate feature with narrower support. Queries on custom properties, such as @container style(--theme: dark), arrived in Chromium and Safari later than size queries. Firefox has lagged behind them. Always check a current compatibility table such as caniuse or MDN's browser compatibility data before relying on them. The version numbers above describe the past. The state of style queries may have changed by the time you read this.
Support for the two features is independent. A browser can understand @container (min-width: 400px) and still ignore @container style(--theme: dark). Treat them as two separate decisions.
What an unsupported browser does
Progressive enhancement works here because CSS error handling is forgiving. An old browser does not fail on syntax it doesn't know. It skips it.
- A declaration it can't parse, such as
container-type: inline-size, is dropped. The rest of the rule still applies. - An at-rule it doesn't recognise, such as
@container (min-width: 400px) { ... }, is dropped along with everything inside it. - A declaration containing an unknown unit, such as
font-size: 4cqi, is invalid at parse time and dropped.
So the most important rule is to write the base styles as the version that works with no container query at all. Add the container-aware styles on top. If the enhancement never runs, the page is the base version.
/* Base: works everywhere */
.card {
display: block;
}
.card img {
max-width: 100%;
height: auto;
}
/* Enhancement: ignored entirely by browsers without container queries */
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}An older browser sees only the first two rules. The card stays a single column, which is a valid layout at any width.
Do you need @supports?
Often you don't. If your fallback is simply the base styles, unknown syntax is ignored and @supports adds nothing. You need @supports in two situations.
- Base styles that would misbehave next to the enhancement. If the enhanced version changes the base in a way that should only happen when the container query will actually run, group them so they succeed or fail together.
- A different fallback for old browsers. For example, you may want a viewport media query to approximate the container behaviour where container queries are missing.
The feature test for size container queries is a property test:
@supports (container-type: inline-size) {
/* Only browsers that know container-type get here */
}This checks whether the browser parses the property and value. It is a good proxy for size container queries because they shipped together. It says nothing about style queries, though.
Grouping the enhancement
This is the pattern from the topic plan. The base layout is outside, and the container declaration and the @container rule are inside @supports:
.card {
display: block;
}
@supports (container-type: inline-size) {
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}
}An at-rule nested inside @supports is valid. Browsers that support container queries also support @supports, which has been available far longer, so the wrapper is safe.
The benefit is clarity more than behaviour. Anyone reading the stylesheet sees which code is optional, and you can delete the whole block later without hunting through the file.
Giving old browsers a media query fallback
When a layout truly needs to change at some width, you can approximate it with the viewport. Use @supports not so the media query applies only where container queries don't exist:
.card {
display: block;
}
@supports (container-type: inline-size) {
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}
}
@supports not (container-type: inline-size) {
@media (min-width: 40rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}
}The two branches are mutually exclusive, so no browser applies both. The media query version is a worse approximation. It reacts to the viewport, so a card in a narrow sidebar on a wide screen gets the two-column layout and looks cramped. That is the original problem container queries solve. Whether the approximation beats the plain single column depends on your layouts. If wrong guesses make the component look broken, prefer the single column.
Fallbacks for container units
Container query units also degrade by parse failure, which you can use deliberately. Declare a safe value first and the enhanced value second:
.card-wrapper {
container-type: inline-size;
}
.card h2 {
font-size: 1.25rem; /* fallback */
font-size: clamp(1rem, 4cqi, 2rem); /* used if cqi is understood */
}A browser that doesn't know cqi drops the second declaration at parse time, so the first stays in effect. This is the standard "repeat the declaration" fallback used for any new value.
A related edge case is easy to miss. In a browser that supports container units, cqi on an element with no query container ancestor does not fail. It resolves against the small viewport size, as if you had written svi. So the heading above still scales if someone forgets the wrapper's container-type, but it scales with the viewport, not the component. This does not cause a visible error, so a missing container can be hard to notice.
Fallbacks for style queries
Style queries need their own thinking because @supports (container-type: inline-size) does not cover them. A browser can pass that test and still ignore @container style(...).
.theme-scope {
--theme: dark;
}
.card {
background: #fff;
color: #111;
}
@container style(--theme: dark) {
.card {
background: #222;
color: #eee;
}
}The default styling is the light version, and the style query adds the dark variant. A browser without style query support shows the light card, which is readable. The risky design is the reverse. If the default were dark text on a transparent background and only the style query set a dark background, an unsupporting browser could end up with dark text on a dark page.
For anything that carries meaning, such as state, readability, or required spacing, set it with a plain class or attribute. Use style queries for refinements. If you must have the same result everywhere, apply the same custom property through a selector as well:
.card[data-theme="dark"] {
background: #222;
color: #eee;
}Detecting support in JavaScript
Sometimes script needs to know, for example to skip a resize-observer workaround. Use CSS.supports:
const hasContainerQueries = CSS.supports("container-type", "inline-size");
if (!hasContainerQueries) {
document.documentElement.classList.add("no-container-queries");
}Prefer CSS-only fallbacks where possible. A class set by script adds a dependency on JavaScript running, and it flashes unstyled content if the script loads late.
Polyfills
A polyfill from the Chrome team's container-query-polyfill project has existed, and it is no longer actively maintained. Polyfills for layout features have a cost. They rely on observers and extra style recalculation, and they cannot reproduce every behaviour of native containment. Given that all major engines have supported size queries since early 2023, a polyfill is rarely worth it now. The exception is a product that must support browsers older than those versions. Check your analytics first.
Decision guide
| Situation | Approach |
|---|---|
| Single column is acceptable for old browsers | Base styles plus `@container`, no `@supports` needed |
| You want the enhancement easy to find and remove | Wrap the container declaration and `@container` rules in `@supports (container-type: inline-size)` |
| Old browsers need a multi-column layout | Add `@supports not (container-type: inline-size)` with a media query |
| Fluid sizes with `cq*` units | Declare a safe value first, then the unit value |
| Style queries | Make the default readable without them, and use selectors for essential state |
| Script needs to know | `CSS.supports("container-type", "inline-size")` |
Common mistakes
- Testing the wrong feature.
@supports (container-type: inline-size)does not prove style query support. - Putting the only layout inside
@container. Unsupported browsers then get no layout at all, such as a flex row that never forms. - Using
@supportsand a media query together withoutnot. Both then apply in modern browsers and can conflict. - Forgetting side effects. In supporting browsers,
container-type: inline-sizeapplies containment, as covered in the earlier chapter. The@supportsblock therefore changes the sizing behaviour of the wrapper only where the feature exists. Test both paths, not just the modern one. - Testing only in your main browser. Disable the feature by temporarily renaming
container-typeto a misspelling in devtools, or use a browser that lacks it, and confirm the base layout is acceptable.
Exercise
A card uses display: flex; flex-direction: row; as its base style, and @container (max-width: 399px) { .card { flex-direction: column; } } to stack on narrow containers. Why is this a weak progressive enhancement design, and how would you restructure it?
Show answer
Browsers without container query support drop the @container rule, so the card stays a row at every width, including narrow containers where a row is cramped. Invert it. Make the base the safe narrow layout (display: block or flex-direction: column) and put the wide layout in @container (min-width: 400px). Unsupported browsers then keep the single column.
Exercise
Write the CSS for a heading that uses clamp(1rem, 4cqi, 2rem) for its font size, with a fallback for browsers that don't understand the cqi unit. What happens to 4cqi in a supporting browser if no ancestor has a container-type?
Show answer
Declare font-size: 1.25rem; first and font-size: clamp(1rem, 4cqi, 2rem); second. A browser that doesn't know cqi drops the second declaration and keeps the first. In a supporting browser with no query container ancestor, cqi resolves against the small viewport size (as svi), so the heading scales with the viewport instead of failing.
Exercise
A stylesheet passes @supports (container-type: inline-size) and uses @container style(--theme: dark) to set a dark background, with the text colour set to light only inside the same style query. Name the failure and the fix.
Show answer
A browser can support size container queries but not style queries. It passes the @supports test and then ignores the style query, so the card keeps its default background and colours. If the default text colour is light on a light background, it becomes unreadable. Fix it by making the default styles readable on their own, then adding the dark variant through the style query. For essential state, also apply it with a plain selector such as .card[data-theme="dark"].