Understand automatic placement and implicit tracks.
Grid can add rows or columns when more items need space. These are implicit tracks, and they let your layout grow without a set row count.
.list {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
grid-template-rows: 4rem;
grid-auto-rows: minmax(6rem, auto);
gap: 1rem;
}For these items, default row-wise placement fills two cells in the first row, two in the next, then one in a third row.
4rem.minmax(6rem, auto).grid-auto-rows sizes automatically created rows; it does not replace the explicit row template. An auto maximum lets these implicit rows grow when their content needs more than the minimum.
In the example above, a sixth article is added. Does it need a fourth row, and what sizes its row?
It fills the second cell of the existing third row. That row is implicit and uses grid-auto-rows: minmax(6rem, auto).
If a seventh article is added, identify the new row and the property that sizes it.
It creates a fourth row, also sized by grid-auto-rows. The first row alone uses the explicit template.
The initial grid-auto-flow value is row. It fills rows in turn and creates more rows as needed. For ordinary items without placement or order overrides, the sequence follows source order.
You can instead fill columns:
.columns {
display: grid;
grid-template-rows: repeat(2, 5rem);
grid-auto-flow: column;
grid-auto-columns: 10rem;
gap: 1rem;
}Two items occupy the first column before placement advances to the next. grid-auto-columns gives the implicit columns a 10rem size. A long collection can therefore become wider than its container; column flow is not automatic horizontal scrolling.
Suppose there are three columns and only the last cell of a row is free. An item spanning two columns must move to the following row. By default, the placement cursor continues forward, so a later single-cell item leaves the earlier hole open.
.packed {
grid-auto-flow: row dense;
}dense lets the browser fill earlier gaps with later items that fit. This can change their visual order while leaving the HTML and usual Tab order unchanged. Check that the result still makes sense.
An item placed beyond the rows or columns you defined can create implicit tracks too. When extra columns appear unexpectedly, inspect item line numbers and spans as well as the template.
Use a grid overlay to distinguish explicit boundaries from added tracks.
grid-auto-rows and grid-auto-columns size those added tracks.Try it yourself
HTML + CSS