Sub topic 13
Limitations and Common Pitfalls
This chapter assumes you know CSS Grid placement (grid-column, grid-row, span), and you have seen the subgrid keyword, track inheritance, and @supports. Here you will meet the cases where subgrid does not behave the way you expect.
The subgrid must be a grid item
A subgrid only works when its element is a direct child of a grid container. The subgrid keyword is valid on any grid container, but if the parent is not a grid, the keyword has nothing to inherit from. The browser then treats the value as none, and the element gets no explicit tracks.
The same happens if you wrap the element in an extra div. Only direct children of the parent grid can inherit its tracks.
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
/* Works: .card is a direct child of .cards */
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}The span must match the content
A subgrid inherits only the tracks it spans. If a card spans three rows but has four children, the fourth child lands in an implicit row. That row is created by the card itself, so it is not aligned with anything in the parent.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}Count the direct children and match the span to that count. Extra children flow into implicit rows, and fewer children leave inherited rows empty.
Subgrid does not skip levels
A subgrid inherits from its direct parent only. If you need a grandchild to line up with the outer grid, make the middle element a subgrid too. Each level in the chain must declare subgrid in the axis you care about.
.outer {
display: grid;
grid-template-columns: repeat(4, 1fr);
}
.middle {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}
.inner {
display: grid;
grid-column: span 2;
grid-template-columns: subgrid;
}Common pitfalls at a glance
| Pitfall | Symptom | Fix |
|---|---|---|
| Parent is not a grid | Subgrid collapses to one implicit track | Set `display: grid` on the parent |
| Wrapper element in between | Children do not align | Remove the wrapper or make it a subgrid |
| Span smaller than child count | Extra children fall into implicit rows | Raise the span to match the children |
| Only the outer level is a subgrid | Grandchildren do not align | Add `subgrid` to each level |
| Explicit `grid-template-*` size set on the subgrid axis | Your sizes are ignored | Use only `subgrid` in that axis |
Sizing and overflow
Subgrid content contributes to the size of the parent tracks, but only through the subgrid's items. Padding, borders and margins on the subgrid element itself are added to the first and last tracks it spans. Large padding can therefore make the outer tracks bigger than you planned.
Another limit is that a subgrid cannot change the parent's track count. It can only use the tracks that already exist. If you need more rows, add them to the parent grid.
Auto-placement can surprise you
Children of a subgrid are auto-placed into the inherited tracks as usual. The auto-placement algorithm does not know about your visual intent, so a child with a large span may push later siblings onto a new row. Place children explicitly when the order matters.
Debugging with DevTools
Browser developer tools can overlay grid lines. Turn the overlay on for both the parent and the subgrid. If the subgrid lines do not coincide with the parent lines, one of the pitfalls above is at work.
Browser support
Subgrid is supported in current versions of Chrome, Edge, Firefox and Safari, but older versions lack it. Keep an @supports fallback if you must serve those browsers, and check current support tables before you decide.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: auto 1fr auto;
}
@supports (grid-template-rows: subgrid) {
.card {
grid-template-rows: subgrid;
}
}Exercise
A card is declared with grid-row: span 3; grid-template-rows: subgrid;, but it has four direct children. What happens to the fourth child, and how do you fix it?
Show answer
The fourth child is placed in an implicit row created by the card, which is not part of the parent's tracks, so it does not align with the other cards. Fix it by spanning four parent rows (and adding that row to the parent layout), or by reducing the card to three direct children, for example by grouping two children in one wrapper.
Exercise
A .section element contains a .card, and .card is meant to line up with the columns of the page-level grid. Only .card has grid-template-columns: subgrid. Why does it fail?
Show answer
.card inherits from .section, its direct parent, not from the page grid. Make .section a subgrid in the column axis as well, spanning the page columns, so that the tracks pass down through each level.