CSS Grid Subgrid: Aligning Nested Elements Across the Parent Grid

CSS subgrid: align the inside of cards across a whole row

A row of post cards in a grid looks tidy only until the titles have different lengths: the excerpts start at different heights, the “Read more” links float at different positions. Each card is its own little layout and knows nothing about its neighbors. With grid-template-rows: subgrid, the parts of every card use the rows of the parent grid, so titles, excerpts and links line up across the whole row.

Problem: A theme’s archive shows posts in three columns. Each card has an image, a title, an excerpt and a meta line. Titles of one to three lines push the rest of each card to a different height, and fixing it with min-height on the title breaks with longer translations.

Solution: Make each card span four rows of the parent grid with grid-row: span 4 and give it display: grid; grid-template-rows: subgrid, so the card’s four children sit in the shared rows. The tallest title in a row decides that row’s height for all cards in it.

The markup

<div class="ha-cards">
    <article class="ha-card">
        <img class="ha-card__image" src="…" alt="">
        <h2 class="ha-card__title"><a href="…">A short title</a></h2>
        <p class="ha-card__excerpt">…</p>
        <p class="ha-card__meta">12 March 2026</p>
    </article>
    <!-- more cards -->
</div>

The CSS

.ha-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(18rem, 1fr));
    column-gap: 1.5rem;
    row-gap: 0;
}

.ha-card {
    display: grid;
    grid-row: span 4;
    grid-template-rows: subgrid;
    row-gap: .5rem;
    margin-block-end: 2rem;
}

.ha-card__meta {
    align-self: end;
}

Each card takes four implicit rows of the parent grid. Because the card’s rows are a subgrid, the image, title, excerpt and meta line of all cards in the same visual row share the same four tracks: the row for titles is as high as the longest title of that row. row-gap on the subgrid overrides the parent’s gap inside the card, and the parent’s row-gap: 0 plus a margin on the card keep space between the card rows.

In a block theme

The Query Loop’s Post Template block renders ul.wp-block-post-template with one li per post, and grid layout is a setting of the block. Add a class to the Post Template (Advanced → Additional CSS class) and target its items:

.ha-aligned-cards.is-layout-grid > li {
    display: grid;
    grid-row: span 4;
    grid-template-rows: subgrid;
    gap: .5rem;
}

The inner blocks of each post (Post Featured Image, Post Title, Post Excerpt, Post Date) must be direct children of the li for this to work, so place them without an extra Group block around them. The number in span 4 must match the number of blocks.

Columns too

grid-template-columns: subgrid works the same way for columns: a form row whose labels and inputs align with the columns of the whole form, or a pricing table where the feature rows of each plan align. A grid item can use subgrid in one direction and its own tracks in the other.

NOTE: Subgrid is supported in all current browsers since 2023 (Firefox 71, Safari 16, Chrome 117). Without support, the card falls back to its own grid with automatic rows, so the content is still displayed, only not aligned across cards. Grid basics: CSS grid layout fundamentals.