Sub topic 6
Aligning card headers, bodies and footers
This chapter assumes you know display: grid, grid-template-columns and grid-template-rows, fr units, gap, and placing items with grid-row and span. It also assumes you know the subgrid keyword from the previous chapters. Here you will apply it to one common layout: a row of cards whose titles, text and links line up.
The problem with independent cards
Picture three cards in a row. Each has a title, a paragraph and a link. If each card is its own flex column or its own grid, each one sizes its rows from its own content. One title wraps to two lines, so its paragraph starts lower than the paragraph in the next card. The links at the bottom do not line up either.
The cards are siblings, but their internal rows know nothing about each other. Subgrid fixes this by making the card use the rows of the parent grid. Every card in the same parent row then shares the same row heights.
The parent grid
The parent is a normal grid that creates the columns. It does not declare any rows, because the card items will create them by spanning.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}The card as a subgrid
Each card does two things. It spans three rows of the parent, and it uses those three rows as its own.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}The card has three direct children, so each child fills one subgrid row in order.
<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>The browser creates three implicit rows in the parent for each line of cards. Row 1 is as tall as the tallest title in that line. Row 2 is as tall as the tallest paragraph. Row 3 is as tall as the tallest link. Every card reads those heights, so all titles, bodies and links line up.
Why `span 3` matters
The number in span 3 must match the number of rows you want the card to use. A subgrid gets exactly as many tracks as the item spans. If you wrote span 2 with three children, the third child would be placed in an extra implicit row that belongs to the subgrid only. It would not share a height with the other cards.
The gap
The row-gap: 0.5rem on the card replaces the parent's row gap inside the card. The parent has no row-gap of its own here, but its gap: 1rem still applies to rows, and that would also separate each card from the one in the next line. Because the card sets its own row-gap, the space between title, text and link is 0.5rem. The space between lines of cards stays 1rem.
Falling back safely
Browsers without subgrid ignore the subgrid value. You can give them sensible fixed rows first and then upgrade with @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, the card still has three rows, and the middle 1fr row pushes the link to the bottom of the card. The links align only when the cards are the same height, which a stretched grid item usually gives you. Titles may not line up, but the layout stays usable.
Common mistakes
| Mistake | Result |
|---|---|
| Forgetting `grid-row: span 3` | The card spans one row, so the subgrid has one track and the children fall into implicit rows |
| Wrapping title and text in one extra `div` | The wrapper becomes one subgrid item, so only two rows are used |
| Setting `grid-template-rows` on `.cards` | You define fixed rows, and cards in later lines may not get matching rows |
Exercise
A card spans three parent rows with grid-template-rows: subgrid, but its title, text and link have four direct children (an extra badge above the title). What happens to the last child, and how do you fix it?
Show answer
The first three children fill the three subgrid rows. The fourth child goes into an implicit row that only that card has, so it does not align with other cards. Fix it by changing the card to grid-row: span 4 and giving every card four children, or by placing the badge in the same row as the title with an explicit grid-row: 1.