Sub topic 10
Fallbacks with @supports
This chapter assumes you know basic CSS Grid, the subgrid keyword, and the card and form patterns from the earlier chapters. It shows how to ship those layouts so that browsers without subgrid still get a usable page.
Why subgrid needs a fallback
Subgrid is supported in Firefox 71 and later, Safari 16 and later, and Chrome and Edge 117 and later. That covers most users today, but not all of them. Older browsers, and some embedded web views, may never get it.
A browser that does not understand subgrid does not crash. It ignores the declaration, so the nested grid ends up with no row or column template at all. For a card, that means the header, body and footer collapse into one implicit track each, with no sizing rules. Alignment is lost, and often the layout breaks as well.
A fallback is a version of the layout that works without subgrid. It does not need to look identical. It needs to be readable and usable.
How browsers treat unknown values
When a browser parses grid-template-rows: subgrid and does not recognize the value, it discards that whole declaration. Any earlier declaration for the same property in the same rule stays in effect. This gives a fallback that needs no @supports:
.card {
display: grid;
grid-row: span 3;
grid-template-rows: auto 1fr auto;
grid-template-rows: subgrid;
}Old browsers drop the second grid-template-rows and keep auto 1fr auto. Browsers with subgrid let the later declaration win.
This works only when the fallback differs from the subgrid version in a single property. If the subgrid version also needs a different gap, a different grid-row, or different child rules, the cascade trick can't switch them as a group. That is the job of a feature query.
Feature queries with @supports
An @supports rule applies its contents only when the browser can parse the condition. It does not test whether the feature works well, only whether the declaration is valid.
@supports (grid-template-rows: subgrid) {
/* subgrid-only rules */
}| Form | Meaning |
|---|---|
| `@supports (grid-template-rows: subgrid)` | The browser accepts subgrid in the row axis |
| `@supports (grid-template-columns: subgrid)` | The browser accepts subgrid in the column axis |
| `@supports not (grid-template-rows: subgrid)` | Only browsers without subgrid |
| `@supports (A) and (B)` | Both conditions must be valid |
| `@supports (A) or (B)` | At least one condition must be valid |
Every browser that supports subgrid supports it in both axes, so testing one axis is enough. Write the condition for the axis your rule uses, so the query matches the code it guards.
The recommended structure is progressive enhancement. Write the fallback as your normal, unconditional CSS. Then put the subgrid version inside @supports, where it overrides the fallback. Old browsers skip the block, because they don't recognize the @supports rule or can't parse the condition. Either way, they never see the subgrid code.
You can write the reverse with @supports not. That puts the fallback in the conditional block and the modern code in the base rules. It fails badly in browsers that predate @supports itself, because they skip the block and get no fallback. Prefer the enhancement structure.
Fallback for aligned cards
The card example from the earlier chapters has three rows: title, text and link. The fallback gives the card its own three rows and swaps in subgrid where supported.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: auto 1fr auto;
row-gap: 0.5rem;
}
@supports (grid-template-rows: subgrid) {
.card {
grid-template-rows: subgrid;
}
}In the fallback, each card still spans three parent rows and stretches to the height of its row of cards. The middle 1fr row absorbs the extra height, so every link sits at the bottom of its card. Titles in neighboring cards can differ in height, so body text may start at different positions. That is a visual flaw, not a broken layout.
With subgrid, the three card rows are the parent's rows. The tallest title in the row sets the first row's height for all cards, so the body text lines up.
The card's row-gap: 0.5rem stays outside the query because it is valid in both cases. In a subgrid, a gap you set overrides the parent's gap for that axis. Chapter-level reminder: if you don't set one, the subgrid uses the parent's gap.
Fallback for form labels
The form example needs the same treatment. Without subgrid, each .field is its own grid and can't see the other fields, so max-content would size every label column separately. The fallback is a fixed label column.
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: 10rem 1fr;
column-gap: 1rem;
}
@supports (grid-template-columns: subgrid) {
.field {
grid-template-columns: subgrid;
}
}The fixed 10rem keeps all labels and inputs aligned in old browsers, as long as no label is longer than 10rem. The subgrid version replaces the guess with the real widest label. The column-gap: 1rem equals the parent's column gap, so the spacing is the same in both versions.
Notice the trade-off. For cards, the fallback loses cross-card alignment. For forms, the fallback keeps alignment but loses automatic sizing. Choose the fallback that fails in the least harmful way for your content.
Rules that belong inside the query
Put a rule in the @supports block when it only makes sense with subgrid. Typical cases:
- A
grid-row: span 3that is harmless in the fallback, but that you want to write next to the subgrid code for clarity. - Placement that uses line names from the parent. Without subgrid, the child grid doesn't have those names.
- Gap overrides that would look wrong with the fallback's own row sizes.
- Rules that remove spacing the fallback needs, such as extra padding on the footer.
Keep the block small. Each rule you move into it is a rule you must keep in sync with the fallback.
Testing the fallback
You can't see the fallback in a modern browser unless you force it. Two practical ways:
- Temporarily change the condition to a value no browser supports, such as
(grid-template-rows: subgrid-test). The block never applies, and you see the fallback layout. - Use a browser with no subgrid support, or a recent browser's emulation tools if they offer it.
Test with uneven content. Short and long titles, labels of very different lengths, and narrow viewports show differences between the two layouts.
Detecting subgrid from JavaScript
Sometimes script must know whether subgrid is available, for example to skip a measuring routine that only exists to fake alignment. CSS.supports uses the same test as @supports.
const hasSubgrid = CSS.supports("grid-template-rows", "subgrid");
document.documentElement.classList.toggle("has-subgrid", hasSubgrid);
console.log(hasSubgrid ? "Subgrid available" : "Using fallback layout");Use this only for script logic. For styling, @supports is simpler and has no flash of unstyled layout.
Gotchas
- The query tests parsing, not correctness. A browser can accept
subgridand still have a bug in a specific case. Test the browsers you support. - Order matters. If the
@supportsblock comes before the base rule with the same selector and specificity, the base rule wins and subgrid never applies. Put the query after the fallback. - Specificity is not raised. A rule inside
@supportshas the same specificity as outside it. Match the selectors exactly. - Don't test the wrong property. Checking
display: gridsays nothing about subgrid, since every grid browser passes it. - Fallback children still need valid placement. If a child uses a parent line name, it will be ignored in the fallback and the item auto-places. Check that this order is still sensible.
Exercises
Exercise
A rule sets grid-template-rows: auto 1fr auto; followed on the next line by grid-template-rows: subgrid;. What happens in a browser that does not support subgrid, and why?
Show answer
The browser drops the second declaration because the value is invalid at parse time, so auto 1fr auto stays in effect. A browser with subgrid support applies the later declaration and uses the parent's rows.
Exercise
Your subgrid card layout needs grid-template-rows: subgrid and a different row-gap that only makes sense with subgrid. Which structure should you use, and where does the fallback go?
Show answer
Use a feature query. Put the fallback in the unconditional rules (for example grid-template-rows: auto 1fr auto and the normal gap), then put grid-template-rows: subgrid and the extra gap inside @supports (grid-template-rows: subgrid) { ... }, after the base rule. Cascade-only fallback can't switch several properties together.