CSS3 Advanced Modern Layout · 8 min read

CSS Subgrid

Lesson 26 of 41 in the CSS3 learning path.

How do I align nested grid items to the parent tracks?

Subgrid lets a grid item become a grid that uses the tracks of its parent instead of inventing its own. For a row of cards, the parent defines the columns, and each card sets grid-template-rows to subgrid and spans three rows. The heading, the text, and the link then sit on shared row tracks, so a long paragraph in one card pushes the links down together.

The parent's row-gap is the gap inside the card as well as the gap between card bands. Browsers that do not understand subgrid keep a normal three-row template on the card, so each card still stacks its own heading, text, and link, without lining up across the row.

Syntax & Parameters

Syntax
.card { display: grid; grid-row: span 3; grid-template-rows: subgrid; }

Example

Interactive preview available
styles.css
.catalog {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  column-gap: 1rem;
  row-gap: 0.5rem;
}
.catalog article {
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  background: #ffffff;
}
@supports (grid-template-rows: subgrid) {
  .catalog article {
    grid-template-rows: subgrid;
    grid-row: span 3;
  }
}
.catalog h2 {
  grid-row: 1;
  margin: 0;
  font-size: 1.05rem;
  align-self: end;
  background: #ecfdf5;
}
.catalog p {
  grid-row: 2;
  margin: 0;
  background: #f8fafc;
}
.catalog a {
  grid-row: 3;
  align-self: end;
  color: #0f766e;
  background: #cffafe;
}
@media (max-width: 40rem) {
  .catalog {
    grid-template-columns: 1fr;
  }
}

Expected Output

Three cards in one band. Headings share a row, paragraphs share the next row, and links share the last row, so the links line up even when one paragraph is longer. Tinted bands show those shared tracks. On a narrow viewport the cards stack in a single column.

Line-by-Line Walkthrough

  1. .catalog is a three-column grid. minmax(0, 1fr) lets a column shrink instead of overflowing on a long word.
  2. column-gap separates the cards. row-gap is also the gap the subgrid uses between the title, the text, and the link.
  3. Each article is a grid. The fallback template is auto, 1fr, and auto so older browsers still stack the three parts.
  4. Where subgrid is supported, the article adopts the parent rows and spans three of them, one per part of the card.
  5. The heading, paragraph, and link are assigned to rows 1, 2, and 3 so a missing child cannot shift the link onto the title row.
  6. align-self: end on the heading and the link pins them to a shared edge across the cards in that band.
  7. The paragraph margin is cleared, so the row size comes from the text, and the tallest paragraph sets that row for every card beside it.
  8. The @supports block is what turns subgrid on. Without it, grid-row: span 3 would stretch the card across empty parent rows.
  9. Below 40rem the catalog becomes one column. Each card still has the same three rows, stacked one card after another.

Common Pitfalls & Security

Subgrid does not create tracks. It adopts tracks that already exist on the parent. If the card does not span those rows, or the parent is not a grid, there is nothing to align.

  • grid-template-rows: subgrid without grid-row: span 3 gives the card no parent rows to adopt, and the children collapse onto one another.
  • Setting span 3 outside @supports, in a browser without subgrid, stretches each card over three parent rows and leaves odd gaps.
  • The row gap belongs to the parent. You cannot give the inside of the card a different row gap from the gap between card bands without changing the parent.
  • A card that omits the paragraph puts the link in the wrong row unless you assigned grid-row explicitly. The sample assigns the rows for that reason.
  • display: contents is a different tool. It removes the card box. Subgrid keeps the card border and still shares tracks.
  • column subgrid is the same idea with grid-template-columns: subgrid. Forgetting which axis you spanned aligns nothing on the other axis.

Best Practices & Modern Standards

Use subgrid when nested items must line up with siblings in other cells, especially card headings and card actions. Give every card the same set of children, assign them to explicit rows, and span exactly that many parent rows. Keep a fallback template of auto 1fr auto so the card is still readable without subgrid.

Put the gap on the parent and the padding on the card. Test with one short paragraph and one long paragraph side by side. If the links do not share a line, the cards are not spanning the same parent rows.

Real-World Use

The course catalog places three lesson cards in a row. One summary is a single line and another wraps to four lines. With subgrid, the Open links sit on one shared track instead of drifting up under the short summary. On a phone the grid becomes one column and each card keeps its heading, summary, and link in order.

Frequently Asked Questions

What does grid-template-rows subgrid use?

It uses the parent grid rows that the item spans. The card does not define new row sizes. The tallest content in that shared row sets the size for every card.

Why span three rows?

The card has three parts: heading, text, and link. Spanning three rows gives the subgrid one parent track for each part so those parts line up across the columns.

Do cards still line up without subgrid?

No. The fallback makes each card a private grid with auto, 1fr, and auto rows. The card still looks ordered, but a longer paragraph does not push the neighboring links down.

Can subgrid use the parent columns too?

Yes. grid-template-columns: subgrid adopts the parent columns the item spans. Use it when nested columns must line up with the parent tracks.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…