Sub topic 13
Limitations and common pitfalls
This chapter lists the rules that make a subgrid silently stop working, and the points where it behaves differently from an ordinary nested grid. It assumes you can already declare grid-template-rows: subgrid, place a card with grid-row: span 3, and use @supports. The earlier chapters in this tutorial cover those.
What subgrid cannot do
A subgrid is a grid container that borrows its tracks from its parent. Most of its limits follow from that one fact.
| Limit | Why it exists |
|---|---|
| The subgrid must be a direct grid item of a grid container. | Only a parent grid has tracks to lend. |
| You cannot set track sizes in the subgridded axis. | The parent owns the sizes. |
| The subgrid's track count equals the number of parent tracks it spans. | The subgrid has no tracks of its own to add. |
| Subgrid applies per axis, not per cell. | The keyword goes on `grid-template-columns` or `grid-template-rows`. |
| Line names from the subgrid never reach the parent. | Names flow downward only. |
Pitfall 1: the element is not a direct grid item
subgrid only takes effect when the element is itself a grid container and is also a grid item of another grid container. If any other box sits between them, the keyword is ignored. The element then gets a single track, as if you had written none, and nothing aligns.
The most common cause is a wrapper div added for styling or by a framework.
<div class="cards">
<div class="wrap">
<article class="card">
<h2>Title</h2>
<p>Text.</p>
<a href="#">Link</a>
</article>
</div>
</div>.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}Here .card is a child of .wrap, which is not a grid, so the subgrid fails. You have two fixes. Remove the wrapper, or make it disappear from layout so the card becomes a grid item of .cards:
.wrap {
display: contents;
}Another variant is a .card that sets display: flex or display: block. Without display: grid on the element itself, the subgrid declaration has no effect. Subgrids can nest, but each level must be both a grid item and a grid container.
Pitfall 2: forgetting to span parent tracks
A subgrid gets exactly as many tracks as the area it occupies in the parent. If you do not say how many, the item occupies one track. A card with three children and grid-template-rows: subgrid but no grid-row: span 3 gets one row, and all three children compete for it.
Check three things:
- The item has a span or an explicit line range in the subgridded axis, such as
grid-row: span 3. - The span matches the number of rows (or columns) the children need.
- The parent can supply those tracks. If the card spans more tracks than the parent defines explicitly, the parent creates implicit tracks, and they are sized by
grid-auto-rowsorgrid-auto-columns.
Pitfall 3: the child count does not match the span
Each direct child of the subgrid takes one track under auto-placement. If the card spans three rows and has two children, the third row is empty. If it has four children, the fourth has no track of its own, and it will not add a fourth row to the parent. In practice you get an extra implicit placement, overlap, or content that stretches the wrong row.
This often shows up with optional content such as a card that sometimes has no image or no footer. Fix it in one of these ways:
- Always render the element, and leave it empty when there is nothing to show.
- Place children explicitly so a missing item does not shift the others.
.card > .title { grid-row: 1; }
.card > .text { grid-row: 2; }
.card > .action { grid-row: 3; }With explicit rows, a card that lacks .text leaves row 2 empty, and the link still sits in row 3 with the other cards' links.
Pitfall 4: gaps, padding and borders
Three spacing rules surprise people.
Gap defaults to the parent's gap. In a subgridded axis, row-gap: normal and column-gap: normal resolve to the parent's gap, so you usually write nothing. If you set your own value, the track lines stay on the parent's lines. The browser shifts the content of the items next to each line to produce your gap. A smaller subgrid gap lets items extend into the parent's gap space. A larger one insets them.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.25rem;
}Padding, border and margin on the subgrid are not ignored. They still push the items inward. The parent's first and last spanned tracks grow to fit that extra space, because the subgrid's edge size is added to the sizes of the items at its edges. Two cards with different padding therefore produce different parent track sizes, even though they share the same rows.
Backgrounds and borders belong to the card box, not the tracks. The card is a single box spanning several parent rows. Visual styling on .card itself still looks like a normal card. Only its children are aligned across cards.
Pitfall 5: trying to size tracks in a subgridded axis
Track sizes belong to the parent. These declarations do not work as you might expect:
.card {
display: grid;
grid-template-rows: subgrid 1fr auto; /* invalid */
}The subgrid keyword cannot be combined with track sizes, so the whole declaration is dropped. grid-auto-rows and grid-auto-columns on the subgrid are also ignored in the subgridded axis. To change how tall the shared rows are, change them on the parent, for example grid-auto-rows: auto 1fr auto, or set min-height on the children that contribute to them.
What you can still do inside a subgrid is alignment. align-self, justify-self, align-items and justify-items work as they do in any grid, and they position the item inside the parent's track.
Pitfall 6: one axis does not imply the other
grid-template-rows: subgrid makes only the row axis follow the parent. The column axis is an ordinary grid, with its own grid-template-columns or a single implicit column. This is useful, but it means a card with grid-row: span 3 that you also expect to align horizontally with its neighbours needs grid-template-columns: subgrid and a column span too.
When you use both axes, the item needs a span in both. A card that is one column wide and three rows tall gets one inherited column and three inherited rows.
Pitfall 7: parent track sizing and content
Subgrid items contribute to the sizes of the parent's tracks as if they were direct children of the parent. That is what makes cards in a row share the height of the tallest title. It also means a subgrid can change the parent in ways you do not expect:
- One card with a long, unbreakable word can widen a column of the parent.
- A row set to
autogrows to the tallest item across every subgrid that spans it. - A subgrid's own
min-widthormin-heightparticipates in the parent's sizing.
If a parent track grows more than you want, set min-width: 0 on children that should shrink, use minmax(0, 1fr) in the parent, or let long text wrap with overflow-wrap: anywhere.
Pitfall 8: support and fallbacks
Subgrid is supported in current Firefox (since version 71), Safari (since 16) and Chromium browsers (since 117, September 2023). Older browsers ignore subgrid as an invalid value. In an invalid declaration the browser drops the whole line, so without a fallback the card has no row template at all.
Write the fallback first, then override it inside @supports:
.card {
display: grid;
grid-row: span 3;
grid-template-rows: auto 1fr auto;
}
@supports (grid-template-rows: subgrid) {
.card {
grid-template-rows: subgrid;
}
}Test the unsupported path by temporarily renaming the @supports condition to something false. The card should look acceptable, even if the rows no longer line up across cards.
Debugging checklist
When a subgrid does not align, work through these questions in order.
Browser developer tools help here. In Firefox and Chromium, turning on the grid overlay for the parent and the card shows whether the card's lines coincide with the parent's.
Exercise
A card list uses .card { display: grid; grid-template-rows: subgrid; } inside .cards { display: grid; grid-template-columns: repeat(3, 1fr); }. Each card has a title, a paragraph and a link, but the three parts are stacked in one row and do not align across cards. Name two missing or wrong things and give the fix for each.
Show answer
First, the card has no span, so it occupies one parent row and the subgrid receives only one track. Add grid-row: span 3. Second, check for a wrapper between .cards and .card. A non-grid wrapper makes subgrid compute to none, so remove it or give it display: contents.
Exercise
You write grid-template-rows: subgrid auto 1fr auto; to control the heights of the rows inside a card. What happens, and where should the sizing go instead?
Show answer
The declaration is invalid, because subgrid cannot be combined with track sizes, so the browser drops it. Row sizes belong to the parent grid, for example grid-auto-rows: auto 1fr auto on .cards, or min-height on the contributing children.
Exercise
A card with padding: 2rem and another with padding: 0.5rem both use grid-template-rows: subgrid over the same three parent rows. Explain how the padding affects the shared rows.
Show answer
The padding stays inside each card and insets its first and last items. The parent's first and last spanned rows grow to fit the larger of the two padding amounts, so both cards share the same row sizes, but the card with the larger padding determines how tall they are.