"Define what you can. Let the grid figure out the rest. The auto-flow rules make the difference between a grid that scales and a grid that breaks."
Explicit Grid
The explicit grid is what you declare with grid-template-rows and grid-template-columns. grid-template-columns: repeat(3, 1fr) creates 3 explicit column tracks. grid-template-rows: 100px 100px creates 2 explicit row tracks.
Implicit Grid
If you have more items than the explicit grid can hold, the grid adds new tracks automatically — these are the implicit tracks. A grid with grid-template-columns: repeat(3, 1fr) and 7 items will produce three columns (explicit) and three rows (one implicit for the seventh item — row 3 has only one item).
Implicit track sizing is controlled by:
grid-auto-rows— size of implicit rows. Defaultauto(content-sized).grid-auto-columns— size of implicit columns. Defaultauto.
Most layouts only need grid-auto-rows: 200px or minmax(120px, auto) to size new rows uniformly when items overflow the explicit grid.
grid-auto-flow: How Items Place Themselves
Auto-placement decides where items land. grid-auto-flow sets the strategy:
row(default) — fill rows left-to-right, top-to-bottom. When a row is full, start a new row.column— fill columns top-to-bottom, left-to-right. When a column is full, start a new column.row denseorcolumn dense— same as above, but the grid backfills holes. If item 5 spans 2 columns and won't fit in the current row, default flow leaves a hole;densepacks a smaller later item into that hole.
Dense Packing For Masonry-Like Layouts
If you have cards of varying sizes (some span 1 column, some 2 columns), grid-auto-flow: dense creates a tight, masonry-like packing where smaller items fill the gaps left by larger ones. Trade-off: items may not appear in source order visually.
dense reorders the visual output without changing source order. This is fine when items don't have a meaningful sequence (a photo grid, a card list with no narrative order). It's harmful when source order is the reading order (a list of search results, a timeline). Use dense for visual completeness, not for general layouts.Combining With span
Auto-placement respects span on individual items. grid-column: span 2 on a card makes that card 2 columns wide; auto-flow places it accordingly:
The subgrid Feature
subgrid lets a nested grid inherit its parent's track definitions. Useful when you have a card with a header, body, footer that should align with sibling cards' headers, bodies, footers — without each card carrying its own grid template.
subgrid is widely supported in modern browsers (baseline 2023). Use when you need cross-card alignment; otherwise nested grids with their own templates are simpler.
Pippa's Note
grid-auto-flow: dense with a mix of 1x1 and 2x1 photos. The dense packing means the gallery has no visible holes regardless of photo order — the smaller photos automatically slot into the gaps left by larger ones. Photos are still authored in upload order; only their visual placement reorders. Reading order doesn't matter for a gallery, so dense is the right tradeoff.