Lesson 7 of 30
Nesting a grid inside a grid item normally gives you two unrelated coordinate systems. subgrid lets the inner grid borrow the outer grid's actual track lines, so content in different items can finally line up.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
Without subgrid, why don't a set of nested grids (one inside each of several sibling grid items) automatically align their internal rows with each other?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Put a grid inside an item that's itself part of a grid, and by default the two grids know nothing about each other. The outer grid lays out its items; the inner grid, inside one of those items, defines its own independent set of column and row tracks sized only by its own content. There's no built-in relationship between the outer grid's lines and the inner grid's lines — which is exactly why aligning content across sibling items (say, five pricing cards, each with a different amount of feature text) used to require fragile workarounds: fixed heights, JavaScript measuring the tallest element, or <table>-style layout pressed into service purely for alignment.
subgrid removes that gap. Instead of an inner grid defining its own tracks, grid-template-columns: subgrid (or grid-template-rows: subgrid) tells it to inherit the exact track sizing already established by its parent grid. The inner grid's lines then line up precisely with the outer grid's lines, which means items in different subgrids — different cards — can align to the same row or column position, because they're all ultimately measured against the one outer grid.
.pricing-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1.5rem;
}
.pricing-card {
display: grid;
grid-row: span 4;
grid-template-rows: subgrid;
}
Each .pricing-card is a grid item of .pricing-row, but it's also a grid in its own right, spanning four rows: title, price, feature list, and a call-to-action button. Without subgrid, each card would size those four internal rows independently — a card with a longer feature list pushes its own price and button down, while a card with a short list keeps everything compact, so nothing lines up across the row even though the cards sit side by side. With grid-template-rows: subgrid, all three cards' internal rows are locked to the same four row tracks defined once, at the outer .pricing-row level. The tallest card's content in each row determines that row's height for every card, so every title sits on the same line, every price sits on the same line, every button sits on the same line — real alignment, driven by layout, not by matching numbers by hand.
grid-template-columns: subgrid with grid-template-rows left as a normal track definition is completely valid — inherit alignment on the axis that needs it, keep independent sizing on the axis that doesn't.gap value independent of the parent grid's gap, which is easy to forget and can produce visually inconsistent spacing between rows/columns if left unset.@supports (grid-template-rows: subgrid) if the layout needs a distinct fallback."How would you align the internal rows of a set of card components across a row, when each card has a different amount of content?" tests whether a candidate reaches for a real layout primitive (subgrid) or a JavaScript height-matching hack — the honest, complete answer also names the @supports fallback for browsers where subgrid isn't safe to assume.