Sub topic 2
Why nested grids do not line up
This chapter assumes you know basic CSS selectors and the box model. It also assumes you can write display: grid with grid-template-columns, grid-template-rows, fr units and gap, and place items with grid-column and grid-row, including span. Here you will see the alignment problem that subgrid solves. The next chapter introduces the fix.
A card layout that looks fine
Suppose you build a row of cards. The outer grid places the cards. Each card is a grid of its own that stacks a title, some text and a link.
<div class="cards">
<article class="card">
<h2>Short title</h2>
<p>Brief text.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h2>A much longer title that wraps onto two lines</h2>
<p>Longer body text that takes more room than the first card.</p>
<a href="#">Read more</a>
</article>
</div>.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-template-rows: auto 1fr auto;
row-gap: 0.5rem;
border: 1px solid #999;
padding: 1rem;
}The outer grid stretches both cards to the same height. The 1fr row pushes each link to the bottom of its card. The layout looks tidy until you compare the content inside the cards.
What goes wrong
The second title wraps onto two lines, so its first row is taller. The first card's row sizes come from its own content, and the second card's come from its own. Nothing connects them. As a result:
- The first card's text starts higher than the second card's text.
- The text blocks have different heights, so any dividers or borders between sections sit at different positions.
- The effect repeats in every row of cards and changes whenever the content or viewport width changes.
Why it happens
A grid container creates tracks only for its own children. A grid item that is also a grid container starts a new, independent formatting context. Its children are laid out against its own tracks, and the parent grid never sees them.
| Question | Answer for a normal nested grid |
|---|---|
| Whose tracks do the card's children use? | The card's own tracks |
| What sizes those tracks? | Only the card's own content |
| Does the parent know about the card's inner rows? | No. It sees one item with one overall size |
| Can two sibling cards share inner row heights? | Not by themselves |
The parent only controls the outer size and position of each card. The inner structure is invisible to it.
Workarounds and their costs
Developers have used several workarounds, and each has a drawback:
- Fixed heights or
min-heighton titles. This breaks when text wraps differently, when users zoom, or when translated content is longer. - Flattening the markup. You make titles, texts and links direct children of the parent grid, so they share its tracks. Cards then need a wrapper or
display: contents, anddisplay: contentsremoves the card's box. Its border, background and padding disappear. - JavaScript measurement. You measure the tallest title and copy that height to the others. This needs a resize listener, runs after layout, and can cause visible jumps.
The idea behind the fix
The problem is that the card cannot use the parent's tracks. CSS subgrid addresses this directly: a grid item that is also a grid can adopt its parent's row or column tracks instead of defining new ones. The card's three children then fill three rows of the parent grid, so a tall title in one card makes that row taller for every card in the same row.
The following chapters cover the syntax and how the inherited tracks behave.
Exercise
In the card example, the first card has a one-line title and the second has a two-line title. Why does the text in the first card start higher, even though both cards are the same height?
Show answer
Each card is a separate grid with its own rows. The first row of each card is sized only by that card's title, so the first card's row is shorter and its text begins higher. The parent grid places the cards but does not size their inner rows, so the two cards share no tracks.