Sub topic 9
The implicit grid
This chapter explains how a grid grows beyond the tracks you declare. It assumes you know how to create a grid container, define columns with grid-template-columns, use fr and repeat(), and place items by line number. If any of those are unfamiliar, review the earlier chapters first.
Explicit and implicit tracks
The explicit grid is the set of tracks you declare with grid-template-columns, grid-template-rows, and grid-template-areas. The implicit grid is every extra track the browser creates when content does not fit in the explicit grid.
Implicit tracks appear in two situations:
- There are more grid items than cells. With auto-placement, the extra items go into new rows (or new columns, depending on the flow direction).
- An item is placed on a line that does not exist, for example
grid-row: 5in a grid with only two explicit rows.
By default, implicit tracks have the size auto. They are as tall (or wide) as their content needs, and align-content: stretch can enlarge them when the container has spare space.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px 100px;
}This grid has three columns and two rows, so six cells. If the HTML contains nine items, the first six fill the explicit grid. The last three go into a third row. That row is implicit, and its height depends on the content of those three items.
Sizing implicit tracks
Two properties set the size of implicit tracks:
grid-auto-rowssets the height of implicit rows.grid-auto-columnssets the width of implicit columns.
They accept the same values as the explicit track properties, including minmax(), fr, and lengths. If you give a list of values, the pattern repeats across the implicit tracks.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 120px;
}Here no rows are declared at all. Every row is implicit, and each is 120px tall, no matter how many items you add.
A common and safer choice is minmax() with auto:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(100px, auto);
}Each row is at least 100px tall and grows when its content is taller. A fixed 120px row, by contrast, lets long content overflow the item.
You can also alternate sizes:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 80px 160px;
}Implicit row 1 is 80px, row 2 is 160px, row 3 is 80px again, and so on.
Placing items outside the explicit grid
Line-based placement can also create implicit tracks. Suppose the grid has two explicit rows, which means three row lines (1, 2, 3).
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 100px);
grid-auto-rows: 50px;
}
.far {
grid-row: 5;
}The item .far is placed in the row that starts at line 5. The browser creates implicit rows 3 and 4 to reach it, and also row 5 itself. All three are 50px tall because of grid-auto-rows. Rows 3 and 4 are empty, but they still take up space, and gap applies between them.
Negative line numbers count from the end of the explicit grid only. grid-row: -1 refers to the last explicit line, so it never reaches into implicit tracks. This is a frequent source of bugs: a footer set to grid-row: 1 / -1 will span only the declared rows, not the implicit ones.
Implicit columns and flow direction
Implicit columns appear when items are placed on lines beyond the explicit columns, or when grid-auto-flow is column. In column flow, items fill down each column first, and the browser adds columns as needed.
.strip {
display: grid;
grid-template-rows: repeat(2, 80px);
grid-auto-flow: column;
grid-auto-columns: 200px;
overflow-x: auto;
}No columns are declared. Each pair of items makes a new 200px column, so the content scrolls horizontally. This is a simple way to build a horizontal strip of cards.
Auto-placement and dense packing
grid-auto-flow controls how the browser fills the grid and so which tracks become implicit.
| Value | Behavior |
|---|---|
| `row` (default) | Fills each row left to right, then adds rows. |
| `column` | Fills each column top to bottom, then adds columns. |
| `row dense` | Same as `row`, but later items may fill earlier gaps. |
| `column dense` | Same as `column`, with gap filling. |
Without dense, the placement cursor never moves backward. If an item spans two columns and does not fit at the end of a row, it moves to the next row and leaves a hole. With dense, the browser goes back and fills the hole with a later item that fits.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 120px;
grid-auto-flow: row dense;
gap: 8px;
}
.wide {
grid-column: span 2;
}Common errors
- Expecting
grid-template-rowsto cover all rows. It defines only the explicit rows. Rows created by overflow ignore it and usegrid-auto-rows. - Setting
grid-auto-rowsand expecting it to change explicit rows. It affects only implicit rows. Explicit rows keep the sizes fromgrid-template-rows. - Using
-1to reach the last row when implicit rows exist, as described above. - Overflowing fixed-height rows.
grid-auto-rows: 120pxclips or overflows long content. Useminmax(120px, auto)when the content length is unknown. - Accidental implicit columns. An item with
grid-column: 4in a three-column grid creates a fourth column sized bygrid-auto-columns(defaultauto), which can break the layout without any warning.
You can see the implicit tracks in browser developer tools. In Chrome, Firefox, and Edge, turn on the grid overlay for the container. Explicit and implicit lines are drawn differently, and implicit lines are usually shown as dashed.
Worked example: a tile board
This example uses an explicit header row, then lets the tiles create as many rows as they need.
.board {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 60px;
grid-auto-rows: minmax(100px, auto);
gap: 12px;
}
.board > h1 {
grid-column: 1 / -1;
}The h1 spans all four columns of the one explicit row, which is 60px tall. Every tile after it goes into implicit rows that are at least 100px tall. Adding more tiles never requires a CSS change.
Exercise
A grid has grid-template-columns: repeat(2, 1fr); grid-template-rows: 100px 100px; grid-auto-rows: 40px; and contains 7 auto-placed items with no spans. How many rows does it have, and how tall is each one?
Show answer
There are 4 rows. Seven items in two columns need ceil(7 / 2) = 4 rows. The first two rows are explicit and 100px tall. Rows 3 and 4 are implicit and 40px tall, because of grid-auto-rows.
Exercise
A grid has three explicit rows and many auto-placed items that create extra implicit rows. A sidebar uses grid-row: 1 / -1. Does the sidebar reach the bottom of the grid? How can you fix it?
Show answer
No. The line -1 is the end of the explicit grid, so the sidebar covers only the three declared rows. To cover every row, either declare the rows explicitly, or give the sidebar a span large enough for the expected number of rows, such as grid-row: 1 / span 10. Spanning past the end creates implicit tracks, so choose a value no larger than needed.