Subgrid and Micro-Grid: Aligning Nested Components with the Parent Grid
Use CSS subgrid to align nested components with the parent grid: card rows that line up, forms, inherited line names, and micro-grids inside components.
grid-template-rows: subgrid or grid-template-columns: subgrid reuses the parent’s tracks, so card titles, bodies and buttons line up across a whole row, and form labels share one column. A micro-grid is the opposite tool: a small, independent grid inside a component for parts that only need to align with each other. Good component layouts use subgrid on the axis that must align with siblings and a micro-grid on the axis that does not.
A product listing has three cards side by side. One product name wraps to two lines, one description is a sentence longer, and the “Add to cart” buttons now sit at three different heights. Designers call it messy, and they are right. The usual fixes are fixed heights that break with real content, line clamping that hides information, or JavaScript that measures every card and sets min-height.
CSS subgrid is a value for grid-template-columns and grid-template-rows that tells a nested grid to use its parent’s tracks instead of defining its own. Track sizes, gaps and line names pass down, and the nested items’ content sizes pass back up. One card with a long title makes the title row taller for every card in that row. Chrome, Edge, Firefox and Safari have all supported it since September 2023. A micro-grid, as I use the term, is a component’s own internal grid, the counterpart to the page-level macro grid. You use it when a component arranges its own parts and does not need to align with anything outside.
My position: most “equal height” JavaScript and most fixed card heights in production today are subgrid bugs you can delete. And most components need both tools, one per axis.
Why nested grids cannot align
When a grid item becomes a grid container itself with display: grid, it starts a brand-new grid formatting context. Only its own children size its tracks. Card A’s title row has no idea that card B’s title wrapped to two lines, so their body rows start at different heights. This is the limit of plain nested grids, and a single CSS Grid cannot fix it.
Before subgrid, there were two workarounds. You could flatten the markup so titles, bodies and buttons were all direct children of the outer grid, which destroys the card as a component and its semantics. Or you could use display: contents on the card, which removes the card’s box, so it can no longer have a background, border or padding. Subgrid keeps the card’s box and lets its children join the parent’s tracks.
Aligning card rows with subgrid
The pattern has two parts. The card spans as many parent rows as it has internal parts, and the card declares those rows as a subgrid.
<ul class="cards">
<li class="card">
<h3 class="card__title">Wireless Noise-Cancelling Headphones</h3>
<p class="card__body">Forty hours of battery and a carry case.</p>
<a class="card__cta" href="/p/123">View product</a>
</li>
<!-- more cards -->
</ul>
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(18rem, 100%), 1fr));
gap: 1.5rem;
padding: 0;
list-style: none;
}
.card {
display: grid;
grid-row: span 3; /* one parent row per part: title, body, CTA */
grid-template-rows: subgrid; /* use the parent's rows */
row-gap: 0.75rem; /* tighter spacing inside the card */
padding: 1rem;
border: 1px solid var(--color-border);
border-radius: var(--radius-card);
}
.card__cta {
align-self: end; /* buttons sit on one baseline across the row */
}
Here is what happens. Auto-placement puts each card in the next column and gives it three implicit rows. Cards in the same visual row share those three rows. The title row becomes as tall as the tallest title in that row. The body row matches the longest body, and every CTA lands at the same height. When the grid wraps to a new line of cards, that line gets its own three rows, and its own content sizes them.
Two details make it work cleanly. First, a subgrid inherits the parent’s gap by default. Without row-gap: 0.75rem on the card, the space between a title and its body would be the 1.5rem gap meant for separating cards. Second, the card’s padding and border add extra space at the subgrid’s edges. Content still lines up across cards as long as every card uses the same padding.
Aligning form labels with a column subgrid
Forms are the second big use case. Each field usually needs a wrapper for its error message and hint, and that wrapper is exactly what breaks label alignment in a normal grid.
.form {
display: grid;
grid-template-columns: [label] max-content [input] minmax(0, 1fr);
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1; /* span both parent columns */
grid-template-columns: subgrid; /* reuse them */
}
.field label { grid-column: label; }
.field input,
.field .hint,
.field .error { grid-column: input; }
The label column is as wide as the longest label in the whole form, not in each field. Hints and errors line up under their inputs. If you translate the form and one label doubles in length, every input moves together. Because the column names come from the parent, the field CSS reads like a description of intent rather than a list of line numbers.
Line names pass through
Named lines on the parent are visible inside a subgrid, which makes subgrid the natural partner of the full-bleed layout pattern in CSS Grid. A section can stretch its background edge to edge while its content stays in the readable column:
.content-grid {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] min(65ch, 100% - 2rem) [content-end]
minmax(1rem, 1fr) [full-end];
}
.content-grid > * { grid-column: content; }
.band {
grid-column: full; /* the background spans everything */
display: grid;
grid-template-columns: subgrid; /* but children still see content-start/end */
background: var(--color-surface-alt);
padding-block: 3rem;
}
.band > * { grid-column: content; }
You can also add your own names inside the subgrid, such as grid-template-columns: subgrid [media] [text], which assigns names to the inherited lines from the start. Use it sparingly, because readers then have to look in two places to understand a name.
Micro-grids: the component’s own layout
Not every internal layout needs to align with siblings. A list row with an avatar, a name, some metadata and a menu button only needs its own parts aligned. That is a micro-grid: an ordinary nested grid, scoped to one component, usually built with named areas so it reads like a sketch.
.person {
display: grid;
grid-template-columns: auto minmax(0, 1fr) auto;
grid-template-areas:
"avatar name menu"
"avatar meta menu";
column-gap: 0.75rem;
align-items: center;
}
.person__avatar { grid-area: avatar; }
.person__name { grid-area: name; }
.person__meta { grid-area: meta; }
.person__menu { grid-area: menu; }
Micro-grids become even more useful with container queries, because the component can rearrange its own areas based on its own width, wherever it is placed. A card in a narrow sidebar stacks; the same card in a wide main column puts the image beside the text. The container queries guide shows that pattern in full.
Combining both on different axes
The two tools combine naturally because you set subgrid per axis. A product card can take its rows from the parent, so titles and buttons align across the row, while laying out its columns with its own micro-grid:
.product {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid; /* align with sibling cards */
grid-template-columns: 4rem minmax(0, 1fr);/* micro-grid: thumbnail + text */
column-gap: 0.75rem;
}
.product__thumb { grid-row: 1 / -1; grid-column: 1; }
.product__title,
.product__price,
.product__cta { grid-column: 2; }
Ask one question per axis: “must this line up with the component’s siblings?” If yes, subgrid. If no, micro-grid.
Limits and gotchas
- You must span the right number of tracks. A subgrid has no implicit tracks in its subgridded direction. If a card spans three rows but has four children, the extra child lands in the last track instead of creating a new one, and it overlaps. Keep the card structure fixed, or wrap optional parts so the count never changes.
- The parent decides the sizing. The parent sets track sizes. A subgrid cannot make its own rows
auto 1fr auto; the parent’s auto-created rows areauto, controlled withgrid-auto-rowson the parent. - Gaps pass down. Override
row-gaporcolumn-gapon the subgrid when the inner spacing should differ. - Padding must match across siblings. Lines align, painted edges only align if every card has the same padding and border.
- Old Safari versions. Safari 15 and earlier lack subgrid. If you still support them, wrap the subgrid rules in
@supports (grid-template-rows: subgrid)and accept unaligned cards as the fallback.
Frequently Asked Questions
What is CSS subgrid?
Subgrid is a value for grid-template-columns and grid-template-rows that makes a nested grid use the tracks of its parent grid instead of creating its own. The nested items align with the parent’s rows or columns, and their content sizes feed back into the parent’s track sizing.
Is CSS subgrid supported in all browsers?
Yes. Subgrid has been supported in Chrome, Edge, Firefox and Safari since September 2023. Only very old browsers such as Safari 15 lack it, and for those a feature query with @supports can provide an unaligned fallback.
How do I make card buttons line up at the same height?
Make the card a grid item that spans one parent row per part, for example grid-row: span 3, then set grid-template-rows: subgrid on the card. Cards in the same row then share row heights, so titles, bodies and buttons align regardless of content length.
What is the difference between subgrid and a nested grid?
A nested grid defines its own independent tracks sized only by its own children. A subgrid reuses the tracks of its parent on the subgridded axis, so its children align with items outside the component. You can subgrid one axis and use independent tracks on the other.
Align with the parent, arrange within yourself
Subgrid finally lets components stay components while still lining up with their neighbors. Use it on the axis where alignment across siblings matters, keep a small micro-grid for the component’s own internal arrangement, and delete the equal-height scripts. The micro-grid half becomes much more powerful once components can respond to their own size, which is the subject of container queries vs. media queries.
Rule of thumb: if two components need to line up, their parent’s grid should own the line, and subgrid should share it.
Originally posted on 10 July 2025 and post last modified on 10 July 2025