Subgrid

Topic 4 of 4 in Advanced grid6 min readBeginnerFree course

Overview

A card grid lines up the cards. But what about the insides of the cards? If one card has a two-line title, its description starts lower than its neighbours’, and its button ends up out of line. Each card is its own little layout, unaware of the others.

Subgrid fixes this: a grid item can adopt its parent’s tracks, so the parts of separate cards share the same rows.

Without subgrid, three cards with titles of different lengths have their text and Enrol buttons at different heights. With subgrid, dashed lines show the titles, text and buttons aligned across all three cards. Code: .card { display: grid; grid-row: span 3; grid-template-rows: subgrid; }.

Subgrid is supported in all major browsers (Firefox since 2019, Safari since 2022 and Chrome since 2023).

The problem

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}
.card {
  display: grid;
  grid-template-rows: auto 1fr auto;   /* title, text, button */
}

Each card’s rows are sized by its own content. A card with a long title gets a taller first row; its neighbours don’t know. Flexbox with margin-top: auto can align the buttons at the bottom, but not the titles and text in between.

The solution

Make each card span as many rows of the parent grid as it has parts, and tell it to use the parent’s rows:

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.card {
  display: grid;
  grid-row: span 3;                /* occupy 3 rows of .cards */
  grid-template-rows: subgrid;     /* and lay out my children on those rows */
}

Now the parent grid has rows created by the cards. Each row is as tall as the tallest content in it, across all cards — so the longest title determines the height of the title row for everyone. Every card’s text starts at the same line, and every button sits on the same line.

How it works

  • grid-template-rows: subgrid means “don’t create my own rows; use the ones my area covers in the parent grid”.
  • The card must span exactly as many parent rows as it has children in that direction (span 3 for title, text, button). If it has more children than spanned rows, the extra ones go into implicit rows inside the card, which don’t align.
  • The parent’s rows are implicit and auto-sized, so they grow to fit the content contributed by every card.
  • The parent’s gap is inherited, but a subgrid can override it with its own row-gap — useful for tighter spacing inside a card than between cards.
  • Padding and borders on the card still work; the subgrid’s tracks are inset accordingly.

With auto-fit

Subgrid combines with the responsive recipe from auto-fit and minmax:

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
  gap: 1.5rem;
}

.card {
  display: grid;
  grid-row: span 4;
  grid-template-rows: subgrid;
  gap: 0.75rem;
}

Each row of cards gets its own set of four rows, so alignment works within each visual row of cards. When the grid drops to one column, alignment simply has nothing to line up with, and each card behaves normally.

Subgrid for columns

grid-template-columns: subgrid lines up nested items with the parent’s columns. A classic use is forms, where labels and inputs inside separate <fieldset> or .field wrappers should share one column of labels:

CSS
.form {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.75rem 1rem;
}

.field {
  display: grid;
  grid-column: 1 / -1;               /* span both columns */
  grid-template-columns: subgrid;    /* label in col 1, input in col 2 */
}

All labels share one column as wide as the longest label, even though each label-input pair is wrapped in its own element for semantics or styling.

Subgrid in both directions

grid-template: subgrid / subgrid (or both properties set to subgrid) adopts rows and columns. It’s handy for complex layouts like a table-like list where each row is a separate component.

When to use it

  • Card grids where titles, descriptions, prices or buttons should align.
  • Forms with wrapped label/input pairs.
  • Page layouts where nested components should align to the main column grid.
  • Any time you’ve been tempted to set fixed heights on titles “so they line up” — subgrid does it with real content.

Before subgrid, people set min-height on titles or truncated them to force alignment. Both break with real content and translations. Subgrid aligns to whatever the content actually is.

Exercise

Pricing cards that line up

Three .plan cards sit in a 3-column grid. Each has a heading, a feature list of different length, a price and a button. Use subgrid so that the headings, lists, prices and buttons each line up in a row across all three cards, whatever their content. Keep each card’s own padding and background. On narrow screens (below 48rem), cards stack in one column and subgrid still works.

HTML
<div class="plans">
  <article class="plan"><h3>Starter</h3><ul><li>3 courses</li></ul><p class="price">$9</p><button>Choose</button></article>
  <article class="plan"><h3>Pro — everything, with certificates</h3><ul><li>All courses</li><li>Certificates</li><li>Projects</li></ul><p class="price">$29</p><button>Choose</button></article>
  <article class="plan"><h3>Team</h3><ul><li>All courses</li><li>5 seats</li></ul><p class="price">$99</p><button>Choose</button></article>
</div>

<style>
  * { box-sizing: border-box; }
  .plan { background: #f7f7f5; border: 1px solid #d6d6d2; border-radius: 12px; padding: 1.5rem; }
  .plan > * { margin: 0; }
  /* your code here */
</style>

Summary

  • A nested grid normally has its own tracks, so the insides of sibling cards can’t align with each other.
  • grid-template-rows: subgrid (or columns) makes a grid item reuse its parent grid’s tracks.
  • The item must span the right number of parent tracks — for example grid-row: span 4 for four parts.
  • Rows are then sized by the tallest content across all cards, so titles, text and buttons line up.
  • Subgrid has been supported in all major browsers since 2023.