Sub topic 12
Browser Support and Progressive Enhancement
This chapter assumes you know how to write @container rules, declare a container with container-type, and use @supports. It covers what happens in browsers that lack container queries and how to keep your layout usable there.
Where support stands
Size container queries (@container with container-type: inline-size) work in current versions of Chrome, Edge, Safari, and Firefox. Browsers released before 2022 do not support them. Check a compatibility table such as caniuse.com or MDN for your own audience before you decide how much fallback work is worth doing.
Style queries on custom properties, shown in the previous chapter, have narrower support than size queries. Treat them as an enhancement more strictly than size queries.
What an unsupporting browser does
A browser that does not understand a feature ignores it. It skips the container-type declaration, because the property is unknown. It also skips the whole @container block, because it does not recognize the at-rule.
The result is that your base styles apply and the conditional styles never do. This is a good failure mode, provided your base styles are a complete layout. It becomes a problem when the base styles are empty and everything lives inside @container.
Write the base layout first
Progressive enhancement means the simplest layout comes first and works everywhere. The enhanced layout is added on top for browsers that can handle it.
For a card, the base layout is one column. The two-column layout is the enhancement.
.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;
}
}
}The .card rule sits outside @supports, so every browser gets it. Only browsers that pass the @supports test get the container and the two-column rule.
Why use @supports at all
Browsers that skip @container would ignore the block anyway, so the @supports wrapper is not strictly needed for the example above. It earns its place in two cases:
- You apply styles to the container that only make sense when the query works. For example, a wrapper might need
width: 100%or extra padding when it acts as a container, and you would not want those in browsers without support. - You want a visible, single place that marks the enhanced code, so a later reader knows what can be deleted once support is no longer a concern.
Choosing what the fallback should be
A fallback does not have to match the enhanced design. It only has to be readable and usable. Three common choices:
- Single column. Stack content vertically. This works for almost every component and is the default choice.
- Media query approximation. Use a viewport
@mediarule as a rough stand-in, placed before the container rule so it applies everywhere. This is accurate only when the component sits in a predictable place. - Flexible intrinsic layout. Use
flex-wraporgrid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)). These respond to available space without any query, and many components need no container query at all.
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1rem;
}Reach for the intrinsic layout first. Use a container query when you need a change that auto-fit cannot express, such as moving an image from above the text to beside it.
Common mistakes
- Putting the only layout inside
@container. Unsupporting browsers get no layout rules at all. Keep a complete base layout outside the query. - Overriding the base with higher specificity. If the base uses a more specific selector than the container rule, the enhancement loses even in supporting browsers. Keep selectors equal in specificity, and let source order decide.
- Using the fallback as the real design. Test the enhanced layout in a current browser and the fallback by temporarily renaming the property in your stylesheet, for example
container-typetocontainer-typo. Both views should look intentional.
Testing the fallback
You do not need an old browser. Change the @supports condition to something false, such as (container-type: not-a-real-value), reload, and look at the page. Restore the condition when you are done.
Exercise
A stylesheet has .card { } (empty) and puts all its grid layout inside @container (min-width: 400px). What does a browser without container query support show, and how would you fix it?
Show answer
It ignores the whole @container block, so the card has no layout rules and its content just flows as default block content. Fix it by writing a complete single-column base layout in .card outside the query (for example display: block with spacing), then keeping only the two-column grid inside @container, optionally wrapped in @supports (container-type: inline-size).
Exercise
Why might a fallback built with grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)) make a container query unnecessary?
Show answer
The auto-fit grid already adjusts the number of columns to the space the element has, which is the same goal a container query serves. A container query is only needed for changes that this kind of layout cannot express, such as restructuring the component's internal arrangement.