Subgrid
How a nested grid can borrow its parent's rows or columns, so the insides of cards line up across a row even when their content differs in length.
Subgrid
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A nested grid normally defines its own tracks, so it can't know how tall the cards beside it are. `grid-template-rows: subgrid` makes it use the parent's row tracks instead.
- The subgrid must itself be a grid item that spans the parent tracks it wants to share: `grid-row: span 3` plus `display: grid` plus `grid-template-rows: subgrid`.
- Parent row heights are the max of every card's content in that row, so titles, text and buttons line up across all cards without fixed heights.
- Subgrid works in either axis or both, inherits the parent's gap (you can override it) and can see the parent's named lines.
- Subgrid is Baseline: in every major browser since September 2023, and counted as widely available from March 2026.
The card alignment problem
Take a row of cards. Each has a title, some text and a button. The text length varies, so the buttons sit at different heights, and the layout looks ragged. The usual fix is fragile. You fix the text height, or push the button down with flexbox inside each card. A flex column with margin-top: auto on the button does pin the buttons to the bottom (see common flexbox patterns), but the titles and the text blocks still don’t line up across cards.
The underlying problem is that each card is an island. If a card is a grid, its rows are sized by its own content, and it can’t see its neighbors.
Borrowing the parent’s tracks
Subgrid lets the card say, “don’t define your own rows, use the ones my parent has”.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
gap: 0 1rem;
}
.card {
display: grid;
grid-row: span 3; /* occupy 3 rows of the parent */
grid-template-rows: subgrid; /* ...and use them as my rows */
}
Each card has three children, a title, text and a button, and they fall into the card’s three rows. Those rows are the parent’s rows. Because the parent’s row heights are sized by every item in them, the title row is as tall as the tallest title across all the cards, and the same for the text and button rows.
Here is what that produced in a test with three cards of different heights (titles 20, 60, 20px; text 100, 30, 50px; buttons 20px). All three buttons were at y = 160, which is 60 + 100, the tallest title plus the tallest text. Without subgrid, giving each card its own grid-template-rows: auto 1fr auto, the title rows measured 20px and 60px in neighboring cards, because each card was sized separately.
Two details are easy to miss:
- The card has to span as many parent rows as it has children. Three children,
span 3. If it spans fewer, the content spills into extra implicit rows. - The parent doesn’t need a
grid-template-rows. The implicit rows it creates for the cards are the ones that get shared, which is why the test above worked with only a column template on.cards.
Gaps, names and the other axis
A subgrid inherits the parent’s gap in each axis, so spacing stays consistent. If you want different spacing within the card, set gap on the subgrid, which only changes the spacing between its own items. The card’s own padding adds space inside the borders of the shared tracks, which is useful because it lets a card have an inner margin while its content still aligns with others.
Subgrid can also see the parent’s named lines, so an item inside a subgrid can be placed on a line named in the outer grid. That’s how a deeply nested component can still follow a page-wide layout such as the full-bleed pattern from grid areas and named lines.
You can subgrid one axis or both:
.card { grid-template-columns: subgrid; grid-template-rows: subgrid; }
Support
Subgrid reached every major browser in September 2023, so check the date on any older subgrid tutorial: before then it was not cross-browser. Web platform baseline data counts it as widely available from March 2026, so you can use it without a fallback in most projects.
What the video covers
Coding2GO’s “Learn CSS Subgrid for Perfect Alignment” (November 2025, about four and a half minutes) says in its description that subgrid solves the problem of card layouts, pricing sections and feature grids where buttons and text blocks refuse to line up because content lengths differ, and that child elements inherit the parent grid’s rows or columns without hacks or fixed heights. I could not read a transcript, so I do not describe its demos beyond that. It was published shortly before this lesson was written, so it is the freshest source in the group, and short. The worked numbers, gap rules and named-line notes here are the prose’s own. The runner-up, from October 2023, pairs subgrid with container queries.
Where this fits
Subgrid is the last piece of the grid model that came after the original grid fundamentals. It pairs with container queries, where a card changes layout based on its container and still aligns with its siblings. The next lesson, multi-column layout, covers a layout mode that flows text rather than placing items.
Check yourself
Three cards in a row each have a title, paragraph and button, but the buttons sit at different heights because the paragraphs differ in length. Why does a normal nested grid not fix it?
Each card is its own grid and sizes its own rows from its own content. Cards can't coordinate with their siblings. With subgrid they share the parent's row tracks, and each row's height is the tallest content in that row across all cards.
What three declarations does a card need to use subgrid for its rows?
`grid-row: span 3` (so it occupies three parent rows, one for each of its parts), `display: grid`, and `grid-template-rows: subgrid`. The parent also must be a grid with the card as a grid item.
Do subgrid items inherit the parent's `gap`?
Yes, by default the subgrid uses the parent's gap for that axis. You can set `gap`, `row-gap` or `column-gap` on the subgrid to override it, and its own padding adds extra space at the edges of the shared tracks.
Can you use subgrid for columns only?
Yes. `grid-template-columns: subgrid` shares just the columns and leaves rows to be defined normally, and the reverse. Each axis is independent.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.