Sub topic 10
Fallbacks with @supports
This chapter assumes you can write a basic subgrid (grid-template-rows: subgrid) and know how a card spans parent rows with grid-row: span 3. Here you will make that layout safe in browsers that do not understand subgrid.
Why a fallback is needed
A browser that does not know the subgrid keyword treats grid-template-rows: subgrid as an invalid value and drops the whole declaration. The card then has no row template at all. Its children fall into implicit auto rows. This does not break the page, but the card's internal layout can look wrong. A footer link may sit directly under the text instead of at the bottom of the card.
The fix has two parts. First, write a plain grid layout that works everywhere. Second, use @supports to replace it with the subgrid version where the browser supports it.
The pattern
.card {
display: grid;
grid-row: span 3;
grid-template-rows: auto 1fr auto;
}
@supports (grid-template-rows: subgrid) {
.card {
grid-template-rows: subgrid;
}
}The base rule gives each card its own three rows: an auto-sized title, a stretching body, and an auto-sized footer. This keeps the link at the bottom of every card, but the titles of different cards do not line up with each other. In a supporting browser, the @supports block replaces those private rows with the parent's rows, and the alignment across cards appears.
Notice that display: grid and grid-row: span 3 sit outside the @supports block. Both work in every grid-capable browser. Put in the query only what depends on subgrid.
How the decision is made
Rules that keep it working
- Place the
@supportsblock after the base rule. A@supportsblock adds no specificity. If it comes first, the base rule has the same specificity and wins by source order, so the subgrid never applies. - Test the property you use. Check
grid-template-rows: subgridfor row subgrids andgrid-template-columns: subgridfor column subgrids. Support for the two axes arrived together in current browsers, but testing the one you use is the accurate check. - Keep the fallback good enough to ship. Some visitors will see it. Aim for a design that is acceptable, not identical.
| Situation | What the browser does | Result |
|---|---|---|
| Subgrid supported | Applies the base rule, then the `@supports` override | Rows shared with the parent |
| Subgrid not supported | Skips the `@supports` block | Card uses `auto 1fr auto` |
| `@supports` block written first | Base rule overrides it by source order | Subgrid never applies |
Common errors
A frequent mistake is to wrap the entire card rule in @supports. Unsupported browsers then get no grid on the card at all. Another is to leave grid-template-rows: subgrid in the base rule with no fallback line before it. Because the invalid declaration is simply dropped, there is no error message, which makes the problem easy to miss. Test by temporarily changing the query to a made-up value such as subgrid-x and checking that the fallback looks acceptable.
Exercise
A developer writes the @supports (grid-template-rows: subgrid) block first in the stylesheet, followed by .card { grid-template-rows: auto 1fr auto; }. In a browser that supports subgrid, the cards do not align. Why, and how do you fix it?
Show answer
The @supports block adds no specificity, so both rules have equal specificity and the later one, auto 1fr auto, wins by source order. Move the @supports block after the base .card rule so the subgrid declaration overrides it.