Sub topic 2
Why Nested Grids Do Not Line Up
This chapter explains why a grid inside a grid cannot align with its parent's tracks. The next chapters build on it to show how subgrid fixes this. You should already know these things:
- Basic CSS selectors and the box model.
display: grid,grid-template-columns,grid-template-rows,frunits andgap.- Placing items with
grid-columnandgrid-row, includingspanand line numbers.
The setup: a grid inside a grid
A common layout has a page-level grid whose items are components such as cards, form rows or article blocks. Each component has its own internal structure, such as a title, some text and a link. It is natural to make each component a grid too, so you can control its internal layout with the same tools.
A grid item can be a grid container. Nothing stops you from writing this:
.cards { display: grid; }
.card { display: grid; }This works, and it lays out each card correctly on its own. The trouble starts when you expect the cards to relate to each other. Each card knows nothing about its neighbors.
Who owns a track
A grid container creates a grid formatting context. That context owns its own tracks, lines, gaps and sizing. A track is a row or column, and a line is the edge between tracks.
When a grid container is also a grid item, it plays two separate roles:
- In the parent grid, it is one box placed into one or more parent cells. The parent only sees the outer size of that box.
- In its own grid, it is the container. It builds a fresh set of tracks inside its content box and places its children there.
The two grids are independent. The child's grid-template-rows does not read the parent's rows. The parent's row sizing does not read the child's internal rows. The only information that crosses the boundary is the child's overall size: its minimum and maximum content size, and how much space the parent gives it.
| Level | Defines | Sized by |
|---|---|---|
| `.cards` (parent) | Its columns, plus one implicit row | The outer size of each `.card` |
| `.card` (nested) | Its own three rows and any columns | Its own children and the space the parent gives the card |
Each row in that table is a separate grid. The tracks in .card are not the same objects as the tracks in .cards, even when they happen to have similar sizes.
A concrete failure: cards with uneven titles
Here is a small page with two cards. The first title fits on one line. The second wraps onto two.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nested grid problem</title>
<link rel="stylesheet" href="cards.css">
</head>
<body>
<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>
</body>
</html>Save the CSS below as cards.css next to the HTML file. The border and background are only there to make misalignment visible.
.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;
padding: 1rem;
border: 1px solid #888;
}
.card h2 {
margin: 0;
font-size: 1.1rem;
border-bottom: 2px solid teal;
}
.card p {
margin: 0;
background: #eef;
}Open the page in a window wide enough for two cards side by side. Here is what you will see:
- The cards are equal in height. The parent grid has one implicit row, and grid items stretch by default (
align-items: stretch). The row is as tall as the taller card, and both cards fill it. - The "Read more" links line up at the bottom. Each card's middle row is
1fr, so it absorbs the leftover height and pushes the last row down. - The teal title underlines do not line up. The first card's title row is one line tall. The second card's is two lines tall.
- The text blocks start at different heights. Each card's row 2 begins right after its own row 1, and the two row 1s have different heights.
Each card resolved its auto rows against its own children. Card 1 never asked how tall card 2's title is. The 1fr row only hides the problem at the bottom edge. It does nothing for the edges in the middle.
Why the usual workarounds fall short
Before subgrid, developers had several options. Each trades one problem for another.
- Fix the title height. Setting
min-height: 3remon the title makes both cards agree, but only while the title fits in the number of lines you guessed. A narrower viewport, a larger user font size or a translated string can wrap onto more lines and break the alignment again. Larger values waste space when titles are short. - Flatten the markup. If the title, text and link of every card are direct children of the parent grid, the parent's rows align them. But then there is no card element to carry a border, background, padding or link target. You also need to position every piece manually.
- Use
display: contentson the card. This removes the card's own box so its children join the parent grid. It restores alignment but loses the card as a styled box. Historically, browsers have also had accessibility bugs withdisplay: contentson semantic elements, so check current behavior before relying on it. - Use flexbox with
margin-top: auto. This pins the last item to the bottom. It aligns footers but not headers or bodies. - Measure with JavaScript. Script can read each title's height and set a shared value. It adds code, causes layout work, and can flicker on resize.
None of these let a card keep its own box and let its children share rows with the neighboring card. That combination is what the problem needs.
The same problem in the column axis
Rows are not special. A form built from a list of fields shows the same issue sideways. Suppose each field is a small grid with a label column and an input column, and each field is a grid item of a parent form:
.field {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.5rem;
}max-content sizes each field's label column to that field's own label. A field labeled "Name" gets a narrow label column. A field labeled "Email address" gets a wider one. The inputs then start at different horizontal positions, because every .field computed its columns independently. The label column is a different track in each field, even though the author thinks of it as one column of the form.
What a solution must do
The failure comes from a single design fact: a nested grid defines new tracks. To make children of different cards line up, the card must stop defining tracks in the relevant axis and use the parent's tracks instead. The card then has to:
- Remain a box in the parent grid, with its own border, background and padding.
- Span the parent tracks it wants to use.
- Let its own children be placed into those parent tracks.
- Let its children's content size those shared tracks, so the tallest title in the row sets the title row height for everyone.
This is what the subgrid keyword does. It replaces a track list such as auto 1fr auto with a request to inherit the parent's tracks. The next chapter covers the syntax and the inheritance rules. For now, notice that point 4 is why the solution changes the sizing model, not only the positioning.
Common mistakes when diagnosing this problem
- Expecting identical track sizes to mean shared tracks. If two nested grids both use
1fr 2fr, the proportions match only if the cards are the same width. The tracks are still separate, and content-based sizing (auto,max-content) can differ between them. - Blaming
align-itemsoralign-content. These properties control where items sit inside tracks. They cannot make tracks in different grids agree. - Adding
grid-auto-rowsto the parent. This affects the parent's rows, but the card's inner rows still belong to the card. - Assuming equal card height means aligned content. Stretching makes the outer boxes equal. It says nothing about the positions of the items inside them.
Exercise
In the card example, the two "Read more" links line up at the bottom even though the cards are separate grids. Why do they line up, and why do the title underlines still not line up?
Show answer
The parent's single row stretches both cards to the same height. In each card, the middle row is 1fr, so it takes all leftover space and pushes the last auto row to the bottom of the card. That works because both cards end at the same bottom edge. The title rows are auto rows sized separately by each card's own title, so a one-line title and a two-line title produce different row heights, and the underlines and the start of the text fall at different positions.
Exercise
A teammate fixes the misaligned titles with min-height: 3rem on every h2. Name two situations in which the fix fails, and state what property of the nested grid makes any such fix fragile.
Show answer
It fails when a title wraps onto more lines than 3rem allows, for example with a narrow viewport, a larger user font size or a longer translated string. It also wastes space when titles are shorter than 3rem. The fix is fragile because each card's rows are sized only by that card's own content, so the cards share no track sizes. Any agreement between them depends on a guessed constant rather than on the actual content.