Sub topic 7
Subgrid in One Axis or Both
This chapter assumes you know display: grid, grid-template-columns and grid-template-rows, and grid-column and grid-row placement with span. It also assumes you have seen the subgrid keyword. Here you will choose which axes to subgrid and see what each choice means.
Two independent axes
A grid has two axes: the column axis (tracks that run left to right) and the row axis (tracks that run top to bottom). The subgrid keyword is a value of two separate properties, so you decide per axis:
grid-template-columns: subgridmakes the element use the parent's column tracks.grid-template-rows: subgridmakes the element use the parent's row tracks.
If you set only one of them, the other axis behaves like an ordinary grid. It has its own tracks, which you define with grid-template-* or let the grid create implicitly.
| Setting | Columns | Rows | Typical use |
|---|---|---|---|
| Columns only | Inherited from parent | Own tracks | Table-like rows that share column widths |
| Rows only | Own tracks | Inherited from parent | Cards whose sections align vertically |
| Both | Inherited | Inherited | A tile that sits on the parent's grid in both directions |
What must be true for either axis
Subgrid only takes effect when these conditions hold:
- The element is a direct grid item of a grid container. If its parent is not a grid,
subgridhas no tracks to borrow and the axis falls back tonone. - The element itself has
display: grid(orinline-grid). - The element spans the parent tracks you want. An item with no explicit placement spans one track, so its subgrid has exactly one track in that axis.
The third condition is the usual source of mistakes, and the examples below show it. The number of tracks a subgrid receives in an axis equals the number of parent tracks the item spans in that axis.
Subgrid in the column axis only
Think of a list of order lines. Each line is its own element, but the name, quantity and price should line up in columns across all lines. The vertical size of each line is independent, because some lines have an extra note.
<ul class="orders">
<li class="order">
<span class="name">Notebook</span>
<span class="qty">2</span>
<span class="price">$12.00</span>
</li>
<li class="order">
<span class="name">Fountain pen with a very long product name</span>
<span class="qty">10</span>
<span class="price">$149.50</span>
<span class="note">Gift wrap requested</span>
</li>
</ul>.orders {
display: grid;
grid-template-columns: 1fr max-content max-content;
column-gap: 1rem;
row-gap: 0.5rem;
list-style: none;
padding: 0;
}
.order {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid;
}
.note {
grid-column: 1 / -1;
font-size: 0.875em;
}The .order items span all three parent columns, so each one receives three column tracks. The max-content columns are sized by the widest quantity and price across every line, which keeps the numbers aligned.
The row axis is not subgridded. Each .order creates its own implicit rows. The first line has one row. The second has two, because .note spans all three columns and cannot fit beside the three cells already in row 1. Neither line's rows affect the other.
Inside .order, line numbers refer to the subgrid's own lines. The three inherited columns have four lines, so grid-column: 1 / -1 means all three columns. This is the same numbering you would get from a local three-column grid.
Subgrid in the row axis only
The card pattern from the previous chapters uses the opposite choice. Cards sit side by side in the parent's columns, and each card spans several parent rows so that titles, text and links line up between neighbours.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}The columns of .card are not subgridded. Children stack in the card's single implicit column, which fills the card's width. If you also wanted children to share the parent's columns, you would add grid-template-columns: subgrid. Cards usually do not need that, because the card is already one column wide.
Subgrid in both axes
Setting both properties lets an item sit on the parent grid in two directions. This suits a feature tile that covers a block of cells while its contents follow the same tracks as the cells around it.
<div class="board">
<div class="tile">1</div>
<article class="feature">
<h2 class="title">Feature</h2>
<p class="body">Spans two columns and two rows of the board.</p>
<p class="aside">Side note</p>
</article>
<div class="tile">2</div>
<div class="tile">3</div>
<div class="tile">4</div>
<div class="tile">5</div>
<div class="tile">6</div>
<div class="tile">7</div>
</div>.board {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 6rem);
gap: 0.5rem;
}
.tile {
background: #e8eef7;
}
.feature {
grid-column: 2 / span 2;
grid-row: 1 / span 2;
display: grid;
grid-template-columns: subgrid;
grid-template-rows: subgrid;
background: #fff3d6;
}
.feature .title {
grid-column: 1 / -1;
grid-row: 1;
}
.feature .body {
grid-column: 1;
grid-row: 2;
}
.feature .aside {
grid-column: 2;
grid-row: 2;
}The feature spans two columns and two rows, so it receives a two-by-two subgrid. The title stretches across both inherited columns in the first row. The body and the aside each take one column in the second row. Their widths and heights come from the board's 1fr columns and 6rem rows, not from the feature.
Local line numbers restart at 1 inside the subgrid, whatever the item's position in the parent. The feature starts at parent column line 2, but its first column line is still 1.
What each axis gives up
Subgridding an axis changes what you can set on it:
- You cannot give the axis an explicit track list.
grid-template-columns: subgridaccepts only optional line names after the keyword, not sizes orrepeat()with sizes. grid-auto-columnsis ignored in a subgridded column axis.grid-auto-rowsis ignored in a subgridded row axis. The tracks come from the parent.- The other axis is unaffected. You can still set
grid-template-rows,grid-auto-rowsandgrid-auto-flowfor it. - Alignment properties such as
align-itemsandjustify-itemsstill apply to the subgrid's items within the inherited tracks.
Extra children and spacing at the edges
Children placed beyond the tracks a subgrid inherited do not add tracks to the parent. They fall outside the shared tracks, so they will not align with neighbours. Keep the span and the number of placed children in step.
Padding and borders on the subgrid element also matter. In a subgridded axis, they are added to the items next to the subgrid's edges, and the parent's tracks grow to fit. A padding: 1rem on a card therefore makes the first and last shared rows taller. Because every card in the row shares those tracks, all cards grow together. The same applies to margins on the subgrid element.
Choosing between axes
Use these questions when you decide which axes to subgrid:
- Which direction must content align across siblings? That is the axis to subgrid.
- Does the item also need its own independent tracks in the other direction? If so, leave that axis alone.
- Do you need to place children against both the parent's rows and columns? Then subgrid both, and span the right number of tracks in each.
A table-like list wants columns only. A row of cards wants rows only. A tile that fits a mosaic wants both.
Exercise
A .row element is a grid item that spans all four columns of a parent grid and has display: grid; grid-template-columns: subgrid;. One row contains a long caption that wraps onto three lines. Does this change the height of the other .row elements, and why?
Show answer
No. Only the column axis is subgridded, so each .row creates its own implicit row tracks. Their heights are independent. The caption affects only its own .row. The column widths stay shared and aligned across rows.
Exercise
You write .feature { grid-column: 2 / span 2; display: grid; grid-template-columns: subgrid; grid-template-rows: subgrid; } and the content does not follow the parent's row sizes. What is missing, and what would you add to make the feature subgrid three parent rows?
Show answer
The item has no row placement, so it spans one parent row and receives a single inherited row track. Add grid-row: span 3 (or an explicit range such as grid-row: 1 / span 3) so the row subgrid has three tracks.