Sub topic 12
Browser support and progressive enhancement with @supports
This chapter assumes you can write @container size queries, declare containers with container-type, and use @supports at a basic level. It covers how to ship container queries and style queries so that older browsers still get a usable page.
Why a fallback is worth writing
Size container queries and container units are supported in all current major browsers. Style queries on custom properties are newer, and support for them has lagged behind size queries. Your audience may also include older browsers, embedded webviews, or locked-down corporate machines.
A browser that does not understand a feature does not fail loudly. It skips the rule it can't parse and applies the rest. That is useful, because it means the plain layout you write first is the one these browsers use. The goal is to make that plain layout acceptable, and to add the enhanced layout on top.
The default-first pattern
Write the base styles for the simplest layout, with no container features. Then add the container-based layout as an enhancement.
Here is the markup used throughout this chapter:
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>The base .card is a single column. Browsers without container query support stay in that layout.
Feature-testing with @supports
An @container rule that an old browser can't parse is simply dropped. But the container-type declaration is a separate problem. If you set up the container in one place and use it elsewhere, you want both parts to depend on the same check. @supports lets you do that.
.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 condition (container-type: inline-size) is true only if the browser recognizes that property and value. In a browser that doesn't, the whole block is skipped and .card stays display: block.
Fallbacks for container units
Container units such as cqi have their own failure mode. A browser that doesn't know the unit treats the whole declaration as invalid and ignores it. You can use that by writing a plain value first, then the container-unit value after it.
.card h2 {
font-size: 1.25rem;
font-size: clamp(1.125rem, 4cqi, 2rem);
}The second declaration replaces the first only if the browser can parse it. Otherwise the first one stays. Use this pattern for padding and gaps too.
A subtler case is a browser that supports cqi but where no container applies. There, the unit resolves against the small viewport size, so 4cqi becomes 4% of the viewport width. If a wrapper is missing or container-type is not set, you get viewport-based sizes, not an error. The clamp() bounds keep that result sensible.
Testing for style query support
Testing for style queries is harder. @supports can check a property, but a style query is a condition inside @container, not a property. Many authors therefore don't test it separately. They write the unstyled version as the default and let the style query add to it:
.alert-region {
--tone: warning;
}
.alert {
border: 2px solid gray;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
background: #fff4e0;
}
}A browser that can't evaluate style() treats the query as false or invalid, and .alert keeps its gray border. The warning is still a bordered box, only less colorful. Don't make the base state depend on the style query for anything essential, such as readability or meaning. Put that information in text or an icon too.
Common errors
- Putting only the container rule inside
@supports. Ifcontainer-typeis applied outside it, an unsupported browser may behave inconsistently. Keep both inside the same block. - Checking the wrong thing.
@supports (container-type: inline-size)tests size containers. It says nothing about style query support, so don't use it to guardstyle()rules. - Dropping the base layout. If the only layout is inside the container rule, old browsers get no layout at all.
- Skipping the unit fallback. Without a plain
font-sizefirst, a browser that doesn't knowcqiignores the declaration and falls back to the inherited size.
Exercise
A page uses font-size: 4cqi; on a heading and has no earlier font-size declaration on that heading. What happens in a browser that does not recognize cqi, and how do you fix it?
Show answer
The browser treats the declaration as invalid and ignores it, so the heading uses the inherited or default font size. Add a plain declaration such as font-size: 1.25rem; before it, so unsupported browsers keep that value and supporting browsers override it. Wrapping the value in clamp() also keeps the result bounded.
Exercise
Why is @supports (container-type: inline-size) not a reliable guard for a @container style(--tone: warning) rule?
Show answer
It only tests for size container support. A browser can support container-type yet not evaluate style queries, so the condition can pass while the style query does nothing. Write the style-query styles as an optional enhancement over a complete default.