Implicit grid and auto-flow
Overview
Most grids don’t define every row. You set the columns, and rows appear as content arrives — three items or three hundred. Those automatic tracks form the implicit grid. Controlling them, and the algorithm that places items into them, is the key to grids that work with any amount of content.
Explicit and implicit tracks
- The explicit grid is made of the tracks you define with
grid-template-columns,grid-template-rowsandgrid-template-areas. - When there are more items than explicit cells — or you place an item outside the explicit grid — the browser creates implicit tracks to hold them.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr); /* explicit columns */
/* no rows defined: every row is implicit */
}Sizing implicit tracks
By default, implicit tracks are auto — sized by their content. Set their size with grid-auto-rows (and grid-auto-columns):
.grid {
grid-auto-rows: 10rem; /* every implicit row is 10rem */
grid-auto-rows: minmax(10rem, auto); /* at least 10rem, taller if content needs it */
grid-auto-rows: 8rem 12rem; /* alternating pattern */
}minmax(10rem, auto) is the safe choice for content: a consistent minimum height that still grows instead of cutting off text.
grid-auto-flow
grid-auto-flow controls how auto-placement fills the grid:
| Value | Behaviour |
|---|---|
row (default) |
fill each row left to right, then add rows |
column |
fill each column top to bottom, then add columns |
row dense / dense |
like row, but backfill earlier holes |
column dense |
like column, with backfilling |
Column flow
With grid-auto-flow: column, items fill downwards and new items create new columns — the basis of horizontal scrollers and timelines:
.scroller {
display: grid;
grid-auto-flow: column;
grid-auto-columns: minmax(14rem, 1fr);
gap: 1rem;
overflow-x: auto;
}Add scroll-snap-type: x mandatory (from Overflow and scrolling) for a carousel feel. Unlike a flex row, every column is the same width without setting widths on items.
If you also define rows, column flow fills those rows before adding a column — handy for lists that should read down columns:
.index {
display: grid;
grid-template-rows: repeat(5, auto);
grid-auto-flow: column; /* items 1–5 in column 1, 6–10 in column 2… */
}Dense packing
With spanning items, auto-placement leaves holes: if an item spanning two columns doesn’t fit in the remaining space on a row, it moves to the next row, leaving the cell behind it empty. By default, the algorithm never goes back — it only moves forward.
dense tells it to backfill: for each item, start searching from the beginning of the grid and use the first hole that fits.
.wall {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 8rem;
grid-auto-flow: dense;
}
.wall .wide { grid-column: span 2; }
.wall .tall { grid-row: span 2; }It produces tightly packed photo walls and dashboards. The cost: items appear out of source order. Item 6 might show up before item 4. For pictures in a gallery that’s usually fine; for anything with a reading order — articles, steps, lists sorted by date — it’s confusing for everyone and a problem for keyboard and screen-reader users. Use dense only when order doesn’t matter.
Placing outside the explicit grid
Place an item beyond the defined lines and the grid grows to include it:
.grid { grid-template-columns: repeat(3, 1fr); }
.extra { grid-column: 5; } /* creates implicit columns 4 and 5 */Implicit columns are sized by grid-auto-columns. Negative line numbers only count the explicit grid, so -1 is still the end of column 3 here — a subtle reason to avoid relying on implicit columns for layout.
Masonry: grid lanes
A true masonry layout — items of different heights packed into columns like a Pinterest board, with no row alignment — is being added to CSS as grid lanes (display: grid-lanes). In December 2026 it’s shipped in Safari 26.4 and is in development in Firefox and Chrome, so it isn’t ready for production yet. Until it is, use CSS multi-column (columns: 16rem) for simple masonry-style flows, or a grid with dense and row spans.
Design with unknown amounts of content: define the columns, size implicit rows with
minmax(…, auto), and let the grid grow. It’s what makes grids work for real data.
A dense photo wall and a horizontal timeline
Make .wall a 4-column grid where every implicit row is 8rem tall; .wide items span 2 columns and .tall items span 2 rows. Turn on dense packing so small items fill the holes left by big ones. Then make .timeline a single row of 12rem-wide columns that keeps adding columns as you add items (scrolling sideways), using auto-flow in the column direction.
<div class="wall">
<div class="wide">1</div><div>2</div><div class="tall">3</div><div class="wide">4</div>
<div>5</div><div>6</div><div class="wide">7</div><div>8</div><div>9</div>
</div>
<ol class="timeline">
<li>2019</li><li>2020</li><li>2021</li><li>2022</li><li>2023</li><li>2024</li><li>2025</li><li>2026</li>
</ol>
<style>
* { box-sizing: border-box; }
.wall > div, .timeline li { background: #eaf0fc; padding: 0.5rem; list-style: none; }
/* your code here */
</style><div class="wall">
<div class="wide">1</div><div>2</div><div class="tall">3</div><div class="wide">4</div>
<div>5</div><div>6</div><div class="wide">7</div><div>8</div><div>9</div>
</div>
<ol class="timeline">
<li>2019</li><li>2020</li><li>2021</li><li>2022</li><li>2023</li><li>2024</li><li>2025</li><li>2026</li>
</ol>
<style>
* { box-sizing: border-box; }
.wall > div, .timeline li { background: #eaf0fc; padding: 0.5rem; list-style: none; }
.wall {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 8rem;
grid-auto-flow: dense; /* backfill holes with later items */
gap: 0.5rem;
}
.wall .wide { grid-column: span 2; }
.wall .tall { grid-row: span 2; }
.timeline {
display: grid;
grid-auto-flow: column; /* new items create new columns */
grid-auto-columns: 12rem;
gap: 1rem;
overflow-x: auto;
padding: 0 0 0.5rem;
margin: 0;
}
</style>Summary
- The explicit grid is what you define; when items don’t fit, grid adds implicit tracks automatically.
grid-auto-rowsandgrid-auto-columnssize implicit tracks;minmax(8rem, auto)gives a minimum that can grow.grid-auto-flow: row(default) fills rows first;columnfills columns first and adds new columns.grid-auto-flow: densebackfills earlier gaps with later items — at the cost of visual order.- Placing an item on a line outside the explicit grid also creates implicit tracks.