Subgrid: Aligning Nested Content to the Parent Grid
Making a nested grid share its parent's tracks with subgrid: lining up card titles, text and buttons across a row, aligning form fields and page content to one set of columns, inherited line names, gap overrides, extra items, and when to use it.
kvi5hnuCode Sprout team member6 min readOn this page
A grid only lays out its direct children. The contents of each card in a card grid are laid out by the card itself, so a long title in one card pushes its text and button down, and nothing lines up across the row. Before subgrid, the fixes were fixed heights or JavaScript.
Subgrid lets a grid item that is itself a grid use its parent's tracks instead of defining its own. The card's title, text and button then sit on rows shared with the other cards, and line up. Subgrid is in all browsers: Firefox 71, Safari 16, and Chrome 117 since 2023.
Using subgrid
subgrid is a value for grid-template-columns or grid-template-rows, on a grid item that is also a grid:
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(14rem, 100%), 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3; /* the card covers three of the parent's rows */
grid-template-rows: subgrid; /* and uses them for its own children */
gap: 0.5rem;
}
Each card has three children: title, text and footer. The card spans three rows of the parent, and with subgrid its children go into those rows. Every row is as tall as its tallest content across all the cards in that row, so all titles take the same height, all texts start at the same line, and all buttons line up.

Tested in Chrome: two cards, one with a short title and one whose title wraps onto three lines. With subgrid, both paragraphs start at the same y position, and both footers too.
The important details:
- The subgrid must span the right number of tracks in the parent:
grid-row: span 3for three children. The parent creates the rows implicitly, as usual. - Subgrid can be used on one axis or both:
grid-template-columns: subgridshares columns,grid-template-rows: subgridshares rows, and both share both. - On the subgridded axis, the subgrid has no tracks of its own:
grid-auto-rowsand track sizes don't apply there.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.6rem; margin-top: 0.75rem; }
.card {
display: grid;
grid-row: span 3;
gap: 0.4rem;
padding: 0.6rem; border: 1px solid #d8cfc0; border-radius: 8px;
}
body:has(#sub:checked) .card { grid-template-rows: subgrid; }
.card h3 { margin: 0; font-size: 0.95rem; color: #8a3b12; }
.card p { margin: 0; background: #f4efe6; padding: 0.3rem; border-radius: 4px; }
.card a { align-self: end; justify-self: start; padding: 0.3rem 0.6rem; border-radius: 6px; background: #2f7d4f; color: white; text-decoration: none; }
With subgrid, both descriptions start on the same line and both prices line up. Without it, each card stacks its own content, and the short card's description and price sit higher.
Columns: one set of lines for nested content
Subgridding columns lets nested elements line up with the page's columns. A common case is a form where each row is a wrapper element, but all labels and inputs should share two columns:
.form { display: grid; grid-template-columns: max-content 1fr; gap: 0.75rem 1rem; }
.form .field {
grid-column: 1 / -1; /* the wrapper spans both columns */
display: grid;
grid-template-columns: subgrid; /* and its label and input use them */
}
The label column is as wide as the longest label in the whole form, not in each row, which a nested grid without subgrid can't do.

On the left, every input starts where its own label ends; with subgrid, all three inputs line up because the rows share the form's columns.
Inherited line names
A subgrid gets the parent's line names (article 68), so nested content can be placed by name:
.page {
display: grid;
grid-template-columns: [full-start] 1fr [content-start] minmax(0, 60rem) [content-end] 1fr [full-end];
}
.section { grid-column: full; display: grid; grid-template-columns: subgrid; }
.section > * { grid-column: content; } /* aligned to the page's content column */
.section > .wide { grid-column: full; }
Tested in Chrome: a subgrid spanning all columns places its child with grid-column: content, using the parent's names. A subgrid can also add names of its own: grid-template-columns: subgrid [a] [b] [c].
Line numbers inside a subgrid start again at 1 at the subgrid's first line; they don't keep the parent's numbering.
Gaps
A subgrid inherits the parent's gaps on the subgridded axis, but can override them. The tracks stay the same size, so the difference is shared out around each item: tested in Chrome, with a parent column-gap: 20px and a subgrid column-gap: 0, an item in a 200px column became 220px wide, extending 10px into each neighbouring gap.
Extra items and limits
On a subgridded axis, the subgrid can't create implicit tracks. What happens to extra children depends on the direction auto-placement grows in. Tested in Chrome:
- With
grid-template-columns: subgridover two columns and three children, the third goes onto a new row, because rows aren't subgridded and can still be created. It sits under the first. - With
grid-template-rows: subgridover one row and three children, no new rows can be made, so all three are clamped into the last row and overlap each other.
So with row subgrids, keep the number of children equal to the span, or wrap optional parts so the structure is the same in every card. If one card has no description, put an empty element there or place the footer explicitly with grid-row: 3.
When to use subgrid
| Situation | Tool |
|---|---|
| parts of sibling components must line up (card titles, prices, form labels) | subgrid |
| nested content must align with the page's columns | subgrid on columns |
| a component lays out its own content independently | a normal nested grid or flexbox |
| items should just be equal height | the default stretch in grid or flexbox is enough |
What's current
- Subgrid works in all browsers since September 2023 (Chrome 117), and is Baseline.
- DevTools in all browsers show subgrids in the grid overlay, with the parent's lines.
- Grid lanes (article 72) are a separate feature for masonry layouts.
Common questions
Why doesn't subgrid do anything?
The item must be a grid itself (display: grid) and span the tracks it wants to share, such as grid-row: span 3.
Can a subgrid have its own gap? Yes. The tracks stay the same, and the difference is applied around the subgrid's items.
What happens to children beyond the spanned tracks? The subgrid can't add tracks on that axis. If auto-placement can grow along the other axis, they go into new tracks there; otherwise they're clamped into the last track and overlap.
Can I use subgrid on just one axis?
Yes: grid-template-rows: subgrid with your own grid-template-columns, or the reverse.
Summary
grid-template-rows: subgridorgrid-template-columns: subgridmakes a nested grid use its parent's tracks.- The subgrid must span the tracks it shares; it has no tracks of its own on that axis.
- Line names pass down from the parent; line numbers restart.
- Gaps are inherited but can be overridden.
- Use it for aligning parts of sibling components, and nested content to page columns.
Practice questions
Each .product card has an image, a name, a price and a button. Write CSS so all four line up across a row of cards.
Answer
.products { display: grid; grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr)); gap: 1rem; }
.product { display: grid; grid-row: span 4; grid-template-rows: subgrid; }
A card has grid-template-rows: subgrid but no grid-row: span .... What happens?
Answer
It spans one row, so its subgrid has a single row track, and it can't add rows. Tested in Chrome: all the children are placed in that one row, on top of each other. Add grid-row: span N for N children.
Form rows are wrappers with a label and an input. How do you make all labels share one column as wide as the longest label?
Answer
.form { display: grid; grid-template-columns: max-content 1fr; }
.form .row { grid-column: 1 / -1; display: grid; grid-template-columns: subgrid; }
Inside a subgrid that spans the parent's columns 2 to 4, what is the line number of the parent's line 3?
Answer
Line 2. Line numbers restart at 1 at the subgrid's first line, which is the parent's line 2.
Previous: Aligning Grid Items and Tracks, Overlapping Items, and the grid Shorthand

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.