Sub topic 10
Auto-placement and grid-auto-flow
This chapter assumes you can define explicit tracks with grid-template-columns and grid-template-rows, and that you know what the implicit grid is. It covers how the browser decides where items go when you don't place them yourself, and how to change that.
How auto-placement works
An item with no grid-column or grid-row is auto-placed. The browser keeps a cursor and walks through the grid one cell at a time. It puts each item in the next free cell, in source order. By default, the cursor moves across a row, then down to the next row. This is the row flow.
When the items outnumber the explicit cells, the browser adds rows to the implicit grid. You size those rows with grid-auto-rows.
Choosing the direction with grid-auto-flow
grid-auto-flow sets the direction the cursor moves.
| Value | Behavior |
|---|---|
| `row` | Fill each row left to right, then add rows. This is the default. |
| `column` | Fill each column top to bottom, then add columns. |
| `row dense` | Fill by row, and backfill earlier gaps. |
| `column dense` | Fill by column, and backfill earlier gaps. |
With column, new tracks are added as implicit columns, so you size them with grid-auto-columns.
.timeline {
display: grid;
grid-template-rows: repeat(3, auto);
grid-auto-flow: column;
grid-auto-columns: 200px;
gap: 8px;
}Here three rows are fixed. Items fill the first column from top to bottom, then the browser creates 200px columns as needed.
Gaps and the dense keyword
Gaps appear when an item spans several tracks and doesn't fit in the space left in the current row. The cursor never goes backward by default, so it skips ahead and leaves empty cells.
Adding dense tells the browser to restart its search from the start for each item. Smaller later items then fill earlier holes.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 120px;
grid-auto-flow: row dense;
gap: 8px;
}
.gallery .wide {
grid-column: span 2;
}If a .wide item doesn't fit at the end of a row, a later single-cell item moves up to fill the hole.
Mixing placed and auto-placed items
The browser places items with explicit positions first. It then auto-places the rest around them. An item with only a row set, or only a column set, is placed in a second pass, before fully automatic items.
Common errors
- Expecting
denseto reorder the DOM. It doesn't. - Setting
grid-auto-rowswhen the flow iscolumn. In column flow, the added tracks are columns, so usegrid-auto-columns. - Forgetting a height. Implicit rows default to
auto, so empty-looking cells may collapse.
Exercise
A grid has grid-template-columns: repeat(3, 1fr) and grid-auto-flow: row. Item 3 has grid-column: span 2 and starts with the cursor at column 2 of the first row. Item 4 is one cell wide. Without dense, where does item 4 go, and what changes if you add dense?
Show answer
Item 3 needs two columns but only columns 2 and 3 remain in row 1, so it fits there. Using a different layout, if it did not fit, the cursor would move to the next row and leave a gap. Without dense, item 4 goes into the next free cell after item 3, never into an earlier hole. With dense, the browser searches from the start for each item, so item 4 fills the first free cell, even one left empty earlier. The DOM order does not change.