Align card rows with CSS subgrid
Share title, description and action tracks across grid cards so a wrapped heading leaves the content below it aligned.
You put two product cards in a grid. One title wraps, so its description starts lower than the one next door. The card boxes can have the same height while the contents sit on different row lines.
Give each card three shared rows
Use one row for the title, one for the description and one for the action. Make each card span all three rows in the parent grid, then use subgrid to lay out its children on those same rows.
diagram
Independent and shared rows
Save the markup in an HTML file and link a stylesheet named cards.css from its head. The line break in the second title makes the alignment easy to see whenever the cards sit side by side.
<section class="cards" aria-label="Plans">
<article class="card">
<h2>Basic</h2>
<p>One project</p>
<button>Choose Basic</button>
</article>
<article class="card">
<h2>Business<br>Plus</h2>
<p>Shared projects</p>
<button>Choose Business</button>
</article>
</section>With the stylesheet below loaded and enough room for two columns, the descriptions and buttons should start at the same heights.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
gap: 0.75rem;
}
.card {
display: grid;
align-items: start;
gap: 0.75rem;
padding: 1rem;
border: 1px solid;
}
.card > * {
margin: 0;
}
@supports (grid-template-rows: subgrid) {
.card {
grid-row: span 3;
grid-template-rows: subgrid;
}
}Resize the page until the cards stack. Each card now occupies its own group of three parent rows. Cards in the same group share row sizing, and cards in another group can size their rows for their own content.
Check the row span when an item moves
The Grid Level 2 spec describes how subgrid children contribute to sizing the parent tracks. In this case, the two-line title makes the first row tall enough for both titles. The second row begins underneath that shared track.
The card still needs display: grid, and its span tells the parent how many tracks to give it. If you add a fourth direct child, decide whether it belongs inside an existing row. Increase the span for all cards if the new child needs its own shared row.
Browsers that skip the feature query still display a grid of cards, with each card sizing its own rows. To compare the two outcomes in DevTools, switch off both declarations inside the query and check where the second description starts.