Full answer
When each card is its own grid or flex box, the card does not know about its neighbours. The title of one card can take two lines and another one line, so the content below starts at different heights.
subgrid lets a nested grid use the rows or columns of its parent grid instead of making its own. If every card spans three parent rows and uses grid-template-rows: subgrid, then the first row is shared by all titles in that row. The row height is the tallest title, and the text and buttons below line up across all cards.
Steps:
- The parent grid defines the columns. The cards flow into columns.
- Each card spans a number of rows with
grid-row: span 3, and itself isdisplay: grid; grid-template-rows: subgrid. - The card's children take the rows in order: title, text, button.
gapis inherited from the parent by default, and the card can override it.
Without subgrid, the older fixes are fixed heights (fragile), JavaScript measuring, or a flex column with margin-top: auto on the button. The last one is a good simple solution when only the bottom needs to line up. Subgrid is better when several rows, such as title, price and list, should align.
Subgrid is a newer feature. Check the support table, and keep a simple fallback with @supports (grid-template-rows: subgrid).
All three buttons start at the same height, and all three paragraphs start at the same height, even though the second title is much longer. Where subgrid is not supported, the flex fallback still pushes the buttons to the bottom of equal-height cards.
Keep in mind that the card's content is placed into rows of the parent, so a card that has more children than rows will create extra implicit rows in the parent.
<style>
body { font-family: sans-serif; margin: 0; }
.cards { display: grid; grid-template-columns: repeat(3, 120px); gap: 8px 12px; }
.card { border: 1px solid #999; padding: 8px; display: flex; flex-direction: column; }
.card button { margin-top: auto; }
@supports (grid-template-rows: subgrid) {
.card { display: grid; grid-row: span 3; grid-template-rows: subgrid; gap: 4px; }
.card button { margin-top: 0; align-self: start; }
}
h3 { margin: 0; font-size: 16px; }
p { margin: 0; }
</style>
<div class="cards">
<div class="card" id="c1"><h3 id="t1">Short</h3><p id="p1">Text</p><button id="b1">Go</button></div>
<div class="card" id="c2"><h3 id="t2">A much longer title that wraps across lines</h3><p id="p2">Text</p><button id="b2">Go</button></div>
<div class="card" id="c3"><h3 id="t3">Medium title here</h3><p id="p3">Text</p><button id="b3">Go</button></div>
</div>Checked in Chrome 156 only, where subgrid is available. The flex fallback was not run separately in an older browser. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Nested cards normally cannot align content with neighbouring cards.
- grid-template-rows: subgrid shares the parent's row tracks.
- The card must span the rows with grid-row: span N.
- A flex column with margin-top: auto is a simple fallback.
- Check the support table and use @supports.
Common mistakes
- Giving each card a fixed height to align content, which breaks when text grows or the page is zoomed.
- Forgetting grid-row: span N on the card, so the card sits in one row and the subgrid has nothing to share.
- Using subgrid with no fallback and no support check.
Follow-ups
- How does a subgrid get its gap?
- When is margin-top: auto in a flex column enough?
Tips
- Explain the root problem first: nested boxes cannot see their neighbours.
- Mention the flex margin-top: auto fallback, and say when it is enough.