Skip to content
Tutorials Center
My learning

Topics

Lesson 78 of 139 ยท Grid

Subgrid

Learn CSS subgrid: let nested elements share their parent grid's tracks so card titles, text and buttons line up perfectly across a whole row.

A grid only lays out its direct children. The children's own children, such as the title, text and button inside a card, know nothing about the grid. Each card is laid out on its own, so when one card has a two-line title and its neighbor has a one-line title, their paragraphs start at different heights and nothing lines up.

Subgrid fixes that. A grid item with grid-template-rows: subgrid (or grid-template-columns: subgrid) doesn't create its own tracks. It borrows the tracks of its parent grid, so its children are placed on the parent's lines. Content in different cards ends up in the same rows, and rows grow to fit the tallest content across the whole set.

Subgrid has been supported in Chrome, Edge, Firefox and Safari since 2023, so you can use it in production today, ideally with a fallback that still looks fine in older browsers (shown at the end of this lesson).

The problem subgrid solves

Here is a typical row of cards built with what you know from Grid Tracks. Look at where the descriptions and buttons start.

Cards without subgrid

The grid stretches all three cards to the same height, but inside each card the content just stacks from the top. The long title in the middle card pushes its text down, and the buttons end up at three different heights. Each card's internal grid has no idea the others exist.

Turning on subgrid

To make the cards share rows, two things need to happen:

  1. Each card must span as many rows of the parent grid as it has parts. Our cards have three parts (title, text, button), so each card spans three rows.
  2. The card uses grid-template-rows: subgrid, so its three children land in those three parent rows.
CSS
.card {
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;
}

The parent grid now has rows that run across all the cards. Row 1 holds every title, row 2 every description, row 3 every button. Each row is as tall as its tallest cell, wherever that cell lives.

Cards with subgrid

Now every description starts on the same line and every button sits on the same line, no matter how much text each card holds. Edit any title and watch all three cards adjust together. When the preview is narrow and cards wrap, each new row of cards gets its own set of three parent rows, so alignment works per visual row.

A few details are worth noticing:

  • The row gap of the parent (8px here) now separates the parts inside each card, because those parts sit in the parent's rows. The subgrid inherits the parent's gaps by default.
  • That same small gap also sits between one row of cards and the next when they wrap. A little margin-bottom on each card adds the extra breathing room between card rows.
  • The card's padding still works. The browser adds it around the items in the first and last shared tracks, so content stays inside the border.

How subgrid works

A few rules explain everything subgrid does:

  • A subgrid must be a grid item and a grid container. It needs display: grid and it must sit inside another grid.
  • It uses the parent's tracks in the dimension you choose. grid-template-columns: subgrid adopts the parent's columns; grid-template-rows: subgrid adopts its rows. You can use one, the other, or both.
  • It covers only the tracks it spans. A card spanning three rows gets exactly three rows. If it has more children than that, the extra ones are placed in implicit tracks that are not shared with anything, which usually looks broken. Match the span to the number of parts.
  • Sizes flow both ways. Content inside the subgrid contributes to the parent's track sizes. That's what makes the middle card's long title enlarge row 1 for everyone.
  • Line numbers restart. Inside a subgrid, lines are numbered from 1 again, counting only the tracks it spans. Named lines from the parent are inherited, though, and the subgrid can add its own names.
  • The other dimension is independent. With only grid-template-rows: subgrid, the card can still define its own columns with grid-template-columns.

Subgrid for columns: aligned forms

Subgrid is just as useful for columns. A classic case is a form where each field is wrapped in its own element (a <div>, or a <fieldset>), yet you want every label in one column and every input in another.

Form fields on shared columns

The form defines two columns: max-content for labels and 1fr for inputs. Each .field spans both (grid-column: 1 / -1) and adopts them with grid-template-columns: subgrid. The label column becomes exactly as wide as the longest label ("Email address"), across all fields, while each field stays a tidy wrapper in the HTML. Press Save to see the submitted data in the console.

Before subgrid, you had to choose between clean markup (a wrapper per field) and aligned columns (no wrappers, every label and input as a direct grid child). Now you can have both.

Nested subgrids and named lines

Subgrids can nest: a subgrid's child can itself be a subgrid, as long as each level is a grid item of the level above. Named lines pass down too, so a child several levels deep can still be placed with names you defined on the outermost grid:

CSS
.page {
  display: grid;
  grid-template-columns: [full-start] 1fr [content-start] minmax(0, 60ch) [content-end] 1fr [full-end];
}
.section {
  display: grid;
  grid-column: full;
  grid-template-columns: subgrid;
}
.section > * {
  grid-column: content; /* uses the parent's line names */
}

Every .section spans the full width, so its background can reach the edges, while its children stay in the centered content column. You will meet this "breakout" layout again in Grid Patterns. Named areas and lines are covered in Grid Template Areas.

Fallbacks for older browsers

Browsers that don't understand subgrid treat grid-template-rows: subgrid as an invalid value and ignore it. The card then falls back to a normal grid of its own. If you also set grid-row: span 3 unconditionally, the parent grows extra rows for nothing, so it is cleaner to wrap the subgrid version in a feature query:

CSS
.card {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.card a {
  margin-top: auto; /* still pushes buttons to the bottom */
}

@supports (grid-template-rows: subgrid) {
  .card {
    display: grid;
    grid-row: span 3;
    grid-template-rows: subgrid;
    gap: normal; /* back to the parent's gap */
  }
}

Older browsers get equal-height cards with buttons at the bottom (the Flexbox card pattern from Flexbox Patterns); modern browsers also line up the titles and text. Nobody gets a broken layout.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does grid-template-rows: subgrid do?

    A subgrid does not create its own tracks in that dimension. It adopts the parent's tracks that it spans, so its children sit on the parent's lines.
  2. 2. Your cards have a title, an image, a description and a button, and use grid-template-rows: subgrid. How many rows should each card span?

    Each part needs its own shared row, so the card spans four parent rows (grid-row: span 4). Extra children would land in implicit rows that are not shared.
  3. 3. Which gap applies between the parts inside a subgridded card by default?

    A subgrid inherits the parent grid's gap in the subgridded dimension, though it can override it with its own gap property.
  4. 4. What must be true of an element that uses subgrid?

    A subgrid is both an item of its parent grid and a grid container itself. Padding is allowed, and you can subgrid just one dimension.

Summary

  • Normally only direct children of a grid sit on its tracks; nested content is laid out separately in each item.
  • grid-template-rows: subgrid and grid-template-columns: subgrid let a grid item reuse its parent's tracks, so nested content lines up across siblings.
  • The subgrid must span as many parent tracks as it has parts, for example grid-row: span 3 for a three-part card.
  • Subgrids inherit the parent's gaps and line names, restart line numbering at 1, and contribute their content's size to the shared tracks.
  • Subgrid is supported in all major browsers since 2023; use @supports to give older browsers a simpler fallback.

Next: Grid Patterns, a collection of real layouts that put the whole Grid chapter to work.