Sub topic 3
The subgrid keyword
This chapter covers the syntax of subgrid: where the keyword goes, what it replaces, and when the browser ignores it. It assumes you know basic CSS selectors and the box model. It also assumes you can build a grid with display: grid, grid-template-columns, grid-template-rows, fr units and gap, and place items with grid-column and grid-row, including span and line numbers. The previous chapter showed why a nested grid normally ignores the tracks of its parent. Here you will declare a subgrid and learn the rules that make it work.
What the keyword does
subgrid is a value, not a property. You write it where you would normally list track sizes:
.child {
display: grid;
grid-template-columns: subgrid;
}Normally grid-template-columns defines new tracks for the element's own children. With subgrid, the element defines no tracks of its own in that axis. It reuses the parent's tracks that it covers. Its children are then placed on the parent's lines and take part in the parent's track sizing.
The value is accepted by two properties:
grid-template-columns: subgrid;inherits columns.grid-template-rows: subgrid;inherits rows.
You can set either one, or both. Each axis is independent, so a grid can use subgrid for rows and its own explicit tracks for columns.
Three conditions that must hold
A subgrid only works when all of these are true:
- The element has
display: grid(orinline-grid). Setting onlygrid-template-columns: subgridon a block element does nothing. - The element is a grid item of a parent grid container. Its parent must itself be
display: grid. A grid inside a flex container, or inside a grid's grandchild, cannot usesubgridfor that parent. - The element is placed in the parent so that it spans at least one track in the axis where you use
subgrid.
If condition 2 fails, the value computes to none and the element behaves like an ordinary grid with no explicit tracks in that axis. No error appears. This silent failure is the most common mistake.
A first example
Here is the card layout from the plan. The parent grid creates as many columns as fit. Each card spans three rows of the parent and uses them as its own rows.
.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;
}<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>Each .card has three direct children. They fill the three subgrid rows in order: the heading in row 1, the paragraph in row 2, the link in row 3. Row 1 is sized by the tallest heading among all cards in the same parent row. The short title therefore gets the same row height as the long one, and the links line up along one baseline of the layout.
Two details matter here:
- The
grid-row: span 3is what tells the parent how many rows the card covers. Without it the card covers one row, and the subgrid would have only one row for three children. - The cards sit in the same three parent rows only when the parent creates implicit rows for them. With
auto-fillcolumns, cards wrap to new parent rows in groups of three. Each group of three rows is shared by the cards on that line.
Using subgrid in one axis
In the card example the columns are not mentioned. The .card has no grid-template-columns, so its children fill one implicit column, which is the card's width. The card still sits in a single parent column, so columns need no inheritance.
For a form, you want the opposite: inherit columns and keep rows independent.
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}Every .field spans both parent columns and inherits them. A label in the first column and an input in the second then line up across all fields, because they all use the same two tracks. The width of the first column is the widest label, since max-content is calculated over every field's label.
Setting grid-template-columns: subgrid and grid-template-rows: subgrid on the same element makes it a subgrid in both axes. The element then borrows both the rows and the columns it spans. Use this when a nested block must line up with its parent in two directions, such as a feature panel that sits inside a larger dashboard grid.
Spanning decides how many tracks you get
The number of tracks a subgrid inherits equals the number of parent tracks it spans in that axis. Spanning two columns gives the subgrid two columns. Its children are numbered with local line numbers, starting at 1 at the subgrid's own first line, not at the parent's.
.panel {
grid-column: 2 / 4;
display: grid;
grid-template-columns: subgrid;
}
.panel > .badge {
grid-column: 2;
}.panel covers parent lines 2 to 4, which is two tracks. Inside it, .badge placed at grid-column: 2 lands on the second inherited track, which is parent column 3. If you wrote grid-column: 3 instead, you would be pointing past the last line of the subgrid. A subgrid cannot add tracks to its parent, so such a placement does not create a new column.
Naming lines in a subgrid
After the subgrid keyword you can add a list of line names. Each bracket group names one line of the subgrid, from the first line onward.
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
.section {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid [edge-start] [main-start] [main-end] [edge-end];
}
.section > .main {
grid-column: main-start / main-end;
}The parent has three columns, so the subgrid has four lines, and the example names all four. Children can use main-start and main-end for placement, so .main sits in the middle track. The names are local additions. They do not rename the parent's lines. Line names that the parent already defines are also visible inside the subgrid.
Gaps
By default, a subgrid uses the same gap as its parent in the inherited axis. You can override it with row-gap or column-gap (or gap) on the subgrid. That is what row-gap: 0.5rem does in the card example. The override changes spacing for the subgrid's children only. The parent's gap still separates the tracks themselves.
Because the gap is applied between tracks, a smaller subgrid gap makes children sit closer together. The extra space is not lost, since the subgrid still covers the full parent tracks. The leftover space is added next to the items inside their track.
Padding, borders and margins
A subgrid is a box, and it can have padding, border and margin. These do not become new tracks. The browser treats them as extra space at the start and end of the subgrid, and it adds that space to the contribution the subgrid's items make to the parent tracks. A card with padding: 1rem therefore makes its first and last rows slightly taller, and the content stays aligned with the parent's lines.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Children stack in a single track, no alignment | Parent is not a grid container | Put `display: grid` on the parent |
| Subgrid has only one row | Missing `grid-row: span N` | Span as many parent rows as the subgrid has children |
| Nothing changes | `display: grid` missing on the child | Add `display: grid` to the subgrid element |
| Placement is off by one | Using parent line numbers inside the subgrid | Count lines from the subgrid's own first line |
| Subgrid is two levels down | Grandparent tracks are not inherited from a non-subgrid parent | Make each level in between a subgrid as well |
Support and fallback
Subgrid works in current versions of all major browsers. Older browsers ignore the subgrid value as invalid. Declare explicit tracks first, then replace them inside an @supports block:
.card {
display: grid;
grid-row: span 3;
grid-template-rows: auto 1fr auto;
}
@supports (grid-template-rows: subgrid) {
.card {
grid-template-rows: subgrid;
}
}Without support, each card keeps its own three rows. The cards still look fine, but heights do not match across cards. With support, the rows come from the parent.
Exercise
A developer writes .child { grid-template-columns: subgrid; } inside <div class="wrapper"><div class="child">…</div></div>, where .wrapper is display: flex. The children of .child do not line up with any grid. Give two reasons the subgrid does nothing, and fix them.
Show answer
First, .child is not a grid container, because it lacks display: grid. Second, its parent .wrapper is a flex container, so .child is not a grid item and the value computes to none. Fix it by setting display: grid on .wrapper and on .child, defining the parent's columns, and giving .child a grid-column span that covers the tracks it should inherit.
Exercise
A subgrid has grid-column: 2 / 5 and grid-template-columns: subgrid [a] [b] [c] [d]. How many tracks does it inherit, and which parent line does the name c refer to?
Show answer
It spans parent lines 2 to 5, so it inherits three tracks and has four lines. The names are assigned in order, so a is parent line 2, b is line 3, c is line 4 and d is line 5.