Sub topic 12
How Subgrid Content Affects Parent Track Sizing
This chapter assumes you know how to declare a subgrid with grid-template-rows: subgrid or grid-template-columns: subgrid. It also assumes you know that a subgrid spans parent tracks and numbers its lines locally. You should also know how a grid sizes auto, fr, min-content and max-content tracks.
Subgrid does more than let children line up with the parent's tracks. The children of a subgrid also take part in sizing those tracks. This is why the card and form patterns work, and it explains several layout surprises.
The core rule
A track in the parent grid is sized by the items placed in it. Without subgrid, an item that is itself a grid is one box. It reports a single size to the parent, and its inner grid is hidden from the parent's sizing algorithm.
With a subgrid, the nested grid does not create its own tracks in that axis. Its children become participants in the parent's tracks. When the parent sizes the tracks, it asks each subgrid child how much room it needs, as if the child were a direct child of the parent.
The effect depends on the parent's track sizing functions:
| Parent track | Do subgrid children affect it? |
|---|---|
| `auto` | Yes. The track grows to fit the largest contribution. |
| `min-content`, `max-content`, `fit-content()` | Yes. Contributions are measured with that keyword's rule. |
| `1fr` (that is, `minmax(auto, 1fr)`) | Yes, through the `auto` minimum. The track can grow past its share of free space. |
| `minmax(0, 1fr)` | No. The minimum is zero, so content never forces growth. |
| Fixed length such as `3rem` or `12rem` | No. The track keeps its size and content may overflow. |
Worked example: rows sized by the tallest title
Start with the card markup from the previous chapters:
<div class="cards">
<article class="card">
<h2>Short title</h2>
<p>Brief text.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h2>A much longer title that wraps onto two lines</h2>
<p>Longer body text that takes more room than the first card.</p>
<a href="#">Read more</a>
</article>
</div>Here is the CSS. The parent never declares row sizes, so its rows are implicit and use the default grid-auto-rows: auto.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}Each card spans three implicit rows. Cards in the same visual row share those three rows:
- The first row holds the
h2of every card in that row. Its height is the tallest title, so the long title makes the row two lines tall for both cards. - The second row holds the paragraphs. Its height is the tallest paragraph.
- The third row holds the links. Its height is the tallest link.
No JavaScript or fixed heights are involved. The sizing algorithm collects the contributions of all h2 elements into row 1, all p elements into row 2, and so on, then picks the largest in each.
The diagram shows the order of that calculation.
Fixed tracks ignore content
If you give the parent explicit row sizes, subgrid children cannot change them:
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
grid-auto-rows: 3rem;
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}Every implicit row is now exactly 3rem high. A two-line h2 in the first row may be taller than 3rem. It then overflows into the row below, because the track does not grow. Alignment still works, since all cards use the same tracks, but content can collide.
If you want some tracks fixed and some flexible, give the flexible ones a content-based size. The next example declares the pattern auto 1fr auto for implicit rows:
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
grid-auto-rows: auto 1fr auto;
gap: 1rem;
}The sizing function list repeats for each group of three implicit rows. Because each card spans three rows, every card lines up with one copy of the pattern. The middle row absorbs any extra height when the grid container is taller than its content. When the container has no set height, 1fr behaves like auto.
Columns work the same way
The sizing rule applies to both axes. In the form pattern, the parent defines a max-content first column:
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}<form>
<div class="field">
<label for="n">Name</label>
<input id="n" type="text">
</div>
<div class="field">
<label for="e">Email address</label>
<input id="e" type="email">
</div>
<div class="field">
<label for="p">Phone number (optional)</label>
<input id="p" type="tel">
</div>
</form>max-content sizes the first track to the widest contribution in it. Each .field is a subgrid, so its label is a contribution to column 1. The widest label, "Phone number (optional)", sets the width of the column for every field. Without subgrid, each .field would be a single item spanning both columns. Its labels would be hidden from the parent, and the column width could not depend on them.
Edge padding, borders and margins count too
A subgrid is a box, and it can have padding, a border and a margin. These cannot sit inside the parent tracks, so the browser treats them as extra space for the items at the subgrid's edges. The padding on the top edge of a row-axis subgrid adds to the contribution of the items in its first row. Bottom padding adds to the last row. For a column-axis subgrid, the left and right edges (or inline start and end) do the same.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
padding: 1rem;
border: 1px solid #ccc;
}The card background and border draw around all three rows. The h2 sits 1rem plus 1px below the top edge of row 1. The tracks include that space, so the first row grows by that amount and the cards in the row stay aligned.
Padding therefore affects the track size of the first and last tracks only, not the middle ones. Different cards can have different padding. In that case, the track that holds their edge items grows to fit the largest total (item plus padding).
Overridden gaps also change contributions
If a subgrid sets its own gap, row-gap or column-gap, the parent's track positions do not move. The spacing between the parent's lines stays the same. Instead, the browser adjusts the margin of the items next to each line by half the difference between the two gaps.
In the first card example the parent gap is 1rem and the card's row-gap is 0.5rem. The difference is 0.5rem, so each item next to an inner line gets -0.25rem of extra margin. Items extend into the parent gap by that amount. The visual spacing between the title and the paragraph is 0.5rem. The track sizes are computed with that margin included.
A larger gap on the subgrid works the opposite way. It adds positive margin to the items and makes the tracks taller to hold them.
Items spanning several subgrid tracks
A subgrid child can span more than one inherited track. An item that spans two tracks does not set either track's size alone. It contributes extra space only when its own size is larger than the two tracks combined, and the browser then divides that extra space between the tracks. Single-track items are handled first. A common surprise is a wide spanning item growing one track more than you expected, because the extra space went to the flexible or content-sized track.
What subgrid cannot size
- A subgrid does not affect parent tracks in an axis where it is not a subgrid. If you use
grid-template-columns: subgridonly, the child's rows are its own and its content heights never reach the parent's rows. - A child's size can depend on the track it sits in, for example
height: 100%or a percentage width. The browser cannot resolve that against a track whose size depends on the child. During sizing it treats the percentage asauto, and it resolves the percentage against the final track afterward. If a layout looks different from what the content seems to need, look for percentage-based sizes inside the subgrid. - Only content-based sizing contributes.
minmax(0, 1fr), fixed lengths andgrid-template-*: repeat(3, 10rem)ignore the subgrid's children.
Debugging checklist
- Open the grid inspector in the browser developer tools and check that the subgrid overlay shows the parent's lines.
- Confirm how many tracks the subgrid spans. If it spans two tracks but has three children in that axis, the third child goes to an implicit track of the subgrid.
- Look at the parent's track sizes. If a track is fixed or
minmax(0, …), content cannot resize it. - Check padding, borders and gap overrides on the subgrid. They add to the first, last and inner tracks as described above.
- Check for percentages or
height: 100%on subgrid children that depend on the track.
Exercise
A form uses grid-template-columns: 12rem 1fr on the parent, and each .field uses grid-template-columns: subgrid. One label is "Phone number (optional)" and is wider than 12rem. What happens, and which parent column definition would make the first column fit the widest label automatically?
Show answer
The first column stays 12rem wide because a fixed track ignores subgrid contributions, so the long label overflows or wraps. Changing the first column to max-content (or auto) lets the labels of all fields contribute, so the column grows to the widest label and every field shares that width. Use max-content 1fr to avoid wrapping labels.
Exercise
A card subgrid has padding: 1rem and spans three auto rows. Which of the three parent rows get extra height from that padding, and why?
Show answer
Only the first and last rows. Top padding is added to the contribution of the items in the first spanned row, and bottom padding to the items in the last spanned row. The middle row has no edge of the subgrid, so the padding does not affect it.