Sub topic 6
Aligning Card Headers, Bodies and Footers
This chapter assumes you know display: grid, grid-template-columns with repeat() and minmax(), fr units, gap, and placing items with grid-row: span N. It also assumes you have seen the subgrid keyword. The earlier chapters introduced it and explained how a subgrid borrows its parent's tracks. Here you apply that to the most common real use: a row of cards whose titles, text and links line up.
The problem: cards that look ragged
A card grid usually starts out like this. The parent grid creates the columns. Each card is an ordinary block, or a flex column, inside one grid cell.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}Grid already makes every card in a row the same height. The content inside each card is not coordinated, though. If one title wraps to two lines and its neighbour's title fits on one, the body text of the two cards starts at different heights. The "Read more" links end up at different heights too, unless you hack them down with margin-top: auto on a flex column.
The cause is that each card lays out its children independently. A card cannot see how tall its neighbour's title is, so it cannot size its own title row to match. A grid inside the card would define its own rows, and those rows would have no relationship to the rows of any other card.
The idea: one set of rows shared by all cards
Subgrid fixes this by making the cards stop defining rows of their own. Each card spans three rows of the parent grid and uses those rows as its own. The title row is then a single parent row shared by every card in the same visual row. Its height is the tallest title among those cards, and the same applies to the body row and the link row.
A complete working example
This page runs as shown in any browser that supports subgrid. Save it and open it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Subgrid cards</title>
<style>
body {
font-family: system-ui, sans-serif;
margin: 2rem;
}
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
padding: 1rem;
border: 1px solid #999;
border-radius: 0.5rem;
}
.card > * {
margin: 0;
}
</style>
</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>
<article class="card">
<h2>Medium title</h2>
<p>Some text.</p>
<a href="#">Read more</a>
</article>
</div>
</body>
</html>Resize the window. At a wide size all three cards sit side by side, and their three rows line up. At a narrow size, the cards wrap onto further lines, and each new line still aligns internally.
Why each declaration is needed
The pattern has three essential lines on .card. Removing any one of them breaks it in a specific way.
display: gridmakes the card a grid container. Without it,grid-template-rowsdoes nothing, and the children stack as normal blocks.grid-row: span 3makes the card occupy three parent rows. This count must equal the number of children you want to align. The subgrid takes exactly as many tracks as the card spans.grid-template-rows: subgridtells the card to use those three parent rows instead of creating its own.
The columns are untouched. The card sits in one column of the parent, and its children stack inside it. The subgrid applies only to the row axis. Mixing axes is covered in its own chapter.
Where the parent rows come from
The parent declares no grid-template-rows, so there are no explicit rows. Grid creates implicit rows as it auto-places items. The first card spans rows 1 to 3. The second card goes in the next free column, so it also spans rows 1 to 3. A fourth card that no longer fits in the first line of columns goes to rows 4 to 6.
Because each card consumes three rows, a card in the second line gets its own three rows, and they do not interact with the first line. This is what you want: cards align with their neighbours in the same line, not with cards above or below.
The implicit rows have the default size auto, so each is as tall as the tallest subgrid item placed in it. You do not have to set any row sizes.
Where the extra space goes
All cards in one line are the same height, because each spans the same three rows. When the cards are taller than their content needs, for example because another grid area stretches the parent, the extra space is shared among the auto rows by the default align-content behaviour. If you want the body row alone to absorb the spare space, give the parent explicit rows in repeating groups, or use a fixed number of cards per line. For most card lists the default is fine, and no row is oversized.
Gaps inside the card
The card sets row-gap: 0.5rem. By default a subgrid inherits the gap of its parent, which here would be 1rem. Writing a different row-gap on the subgrid overrides it for the lines inside the card only. The space between one line of cards and the next still comes from the parent's gap.
Padding on the card also works as you would expect. The card's top padding adds to the first row, and its bottom padding adds to the last row. Both are counted in the shared track sizes, so the padding of one card affects the height of the shared rows. If cards have different padding, the rows do not align cleanly. Give all cards the same padding.
Things that go wrong
Most failures come from a mismatch between the number of children and the number of spanned rows.
| Symptom | Likely cause | Fix |
|---|---|---|
| Titles still do not line up. | The card is missing `grid-row: span 3`. It occupies one parent row with a one-row subgrid. | Add the span. |
| Cards line up, but the layout looks cramped or has large gaps. | The inherited parent `gap` is larger than you intended inside the card. | Set `row-gap` on the card. |
| Rows of cards drift apart in height. | A card has a different number of children than the span. Auto-placement then offsets later cards by the wrong number of rows. | Keep the child count equal to the span on every card. |
| A card with a fourth child (such as an image) misaligns. | The extra child falls outside the three shared rows and is not aligned across cards. | Change the span to 4 for all cards, or wrap the extra content into one of the existing three children. |
| Headings sit lower than other cards' headings. | The default top margin of `h2` or `p` is part of the item and enlarges its row. | Reset margins, as in `.card > * { margin: 0; }`. |
| Nothing is aligned in an older browser. | The browser does not support subgrid, so it ignores the declaration and the children stack as plain blocks. | Add a fallback, shown next. |
A card without a link, for example, still needs a third child. Use an empty <span></span> or a placeholder element so the three-row structure holds. Skipping a child shifts the others up into the wrong rows, because auto-placement fills the rows in order.
A safe fallback
A browser that does not recognize subgrid treats grid-template-rows: subgrid as invalid and drops it. You can provide explicit row sizes first, then upgrade 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;
}
}In the fallback, each card still has a sensible internal layout, with the body absorbing spare height and the link at the bottom. The rows just do not line up across cards. The span 3 stays in both cases, which is harmless in the fallback: the card simply occupies three implicit rows and distributes its own three rows within them. A later chapter covers fallbacks in more depth.
Choosing between subgrid and flexbox for cards
Before subgrid, the usual approach was a flex column in each card, with margin-top: auto on the footer. That keeps footers at the bottom of equal-height cards. It cannot align the body start, though, because each card's title height is independent. Use subgrid when you need alignment of several sections at once. Use the flex approach if you only need footers pinned to the bottom, or when you must support browsers without subgrid and cannot accept a visual difference.
Exercise
A card grid has .card { display: grid; grid-template-rows: subgrid; } but no grid-row declaration. Each card has a title, a paragraph and a link, and the titles still do not line up between cards. What is wrong, and what one declaration fixes it?
Show answer
Without grid-row: span 3, each card occupies a single parent row, so its subgrid has only one track, and all three children share that row (the extra children go outside the shared rows). Adding grid-row: span 3 to .card makes the card cover three parent rows, which the subgrid then uses for the title, text and link.
Exercise
Your cards have a title, text and link, and a designer adds an image above the title in every card. Which two properties on .card change so the image, title, text and link all align across cards, and what must be true of every card's markup?
Show answer
Change grid-row: span 3 to grid-row: span 4, so the subgrid gets four parent rows. Every card must also have exactly four direct children in the same order (image, title, text, link). If one card omits a child, use an empty placeholder element so later children are not placed in the wrong rows.