Sub topic 12
How Subgrid Content Affects Parent Track Sizing
This chapter assumes you know how auto, max-content, minmax() and fr tracks are sized in a normal grid, and that you can declare a basic subgrid with grid-template-rows: subgrid.
Subgrid items count toward the parent
A normal nested grid is a black box to its parent. The parent sizes its tracks using only the nested grid's overall size. A subgrid works differently. Its children take part in the parent's track sizing, as if they were direct children of the parent.
This is why subgrid can align content. Suppose three cards sit in one row and each title needs a different height. Each title contributes its height to the shared parent row. That row grows to fit the tallest title, and every card uses the same row.
Which tracks respond
Only tracks with an intrinsic sizing function respond to content. These are auto, min-content, max-content, fit-content(), and the minimum side of minmax() or fr. A fixed track such as 8rem ignores its contributions. Content that does not fit overflows.
Implicit rows are auto by default. In the card example, the three rows each card spans are implicit rows, so they size to their content. The .cards rule needs no explicit row sizes.
Padding, borders and margins on the subgrid
A subgrid can have its own padding, border and margin. These do not belong to any track. The browser treats them as extra space added to the items at the subgrid's edges. The first row's items get the top padding, and the last row's items get the bottom padding. The parent tracks grow to fit that space.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
padding: 1rem;
border: 1px solid #ccc;
row-gap: 0.5rem;
}Here the first parent row is as tall as the tallest title plus 1rem of padding, and the last row is as tall as the tallest link plus 1rem. The border adds 1px at each edge in the same way. Every card gets the same padding contribution, so the cards stay aligned.
Content-sized columns in forms
Column sizing works the same way. In the form example, the first column is max-content. Each .field is a subgrid across both columns, so each label contributes its width to column 1. The column becomes as wide as the longest label in the whole form.
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}Adding a longer label to any one field widens the label column for all fields. Replacing max-content with 12rem would stop this, because a fixed track ignores contributions.
Gap overrides
If the subgrid sets its own row-gap or column-gap, the parent gap is replaced inside the subgrid. The browser treats the difference as extra margin on the items next to each gap. The tracks stay the parent's. They still grow to fit the larger spacing, so a bigger gap in the subgrid can enlarge parent tracks.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Content overflows its row | The parent row is fixed (`px`, `rem`) or a `fr` track with a fixed minimum |
| Cards align but are taller than expected | Subgrid padding and gap overrides add to the parent track sizes |
| One long label widens every field | The column is `max-content`, so the longest label sets the width |
| Rows stay equal when you expect different heights | A shared `1fr` row distributes space evenly after content minimums |
Exercise
A form uses grid-template-columns: 10rem 1fr on the parent, and each .field uses grid-template-columns: subgrid. A field gets a label 20rem wide. What happens to the label column, and which parent value would make the column grow with the label?
Show answer
The label column stays 10rem, because a fixed length ignores contributions from subgrid items, so the long label overflows. Using max-content (or auto) for the first column lets the widest label set its width.