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 the implicit grid is where tracks created beyond your explicit definition go. It also assumes you can place an item with line numbers, as in grid-column: 1 / 3. Here you will learn how the browser places items you did not position, and how to change that behavior.
What auto-placement is
Most grid items have no grid-column or grid-row value. The browser places them for you. This is auto-placement. It is why a grid with repeat(3, 1fr) and nine children fills three rows with no extra CSS.
Auto-placement follows a defined algorithm. If you know it, you can predict where items land. You also know when a layout needs explicit placement instead.
The placement algorithm
The browser works through items in three passes. A simplified version looks like this:
- Items with a definite position on both axes are placed first, for example
grid-area: headerorgrid-column: 1; grid-row: 2. - Items with a definite position on only one axis are placed next, for example an item with
grid-row: 2but no column. - All remaining items are placed in source order, one at a time, using a cursor that moves through the grid.
One consequence is that an item with explicit placement can appear before an auto-placed item in the layout even if it comes later in the HTML. Explicit items are placed first, so auto-placed items flow around them.
Controlling direction with grid-auto-flow
grid-auto-flow sets the direction in which the cursor moves. It accepts these values:
| Value | Behavior |
|---|---|
| `row` | Fill each row left to right, then add new rows. This is the default. |
| `column` | Fill each column top to bottom, then add new columns. |
| `row dense` | Fill rows, and let later items back-fill earlier gaps. |
| `column dense` | Fill columns, with back-filling. |
With row flow, the number of columns comes from grid-template-columns. New rows are added to the implicit grid as needed, sized by grid-auto-rows. With column flow, the roles swap. You must define rows, and the browser adds columns sized by grid-auto-columns.
Here is a complete page that fills a vertical list into columns:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Column flow</title>
<style>
.list {
display: grid;
grid-template-rows: repeat(3, auto);
grid-auto-flow: column;
grid-auto-columns: 10rem;
gap: 0.5rem;
}
.list div { background: #dde; padding: 0.5rem; }
</style>
</head>
<body>
<div class="list">
<div>1</div><div>2</div><div>3</div>
<div>4</div><div>5</div><div>6</div>
<div>7</div>
</div>
</body>
</html>Items 1 to 3 fill the first column from top to bottom. Items 4 to 6 fill a second column. Item 7 starts a third column. All columns come from the implicit grid, so each is 10rem wide.
Sparse and dense packing
By default the cursor only moves forward. This is sparse packing. If an item does not fit in the space left on the current row, the cursor moves on. The skipped space stays empty, even if a later, smaller item would fit.
Suppose a grid has three columns, and the first two items span two columns each:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.wide { grid-column: span 2; }Take items in the order wide, wide, small. The first wide item takes columns 1 and 2 of row 1. The second wide item needs two columns, but only column 3 is free in row 1. It moves to row 2. In sparse mode, the small item is placed after it, in column 3 of row 2. Column 3 of row 1 stays empty.
Adding dense changes the rule. For each item, the cursor goes back to the start of the grid and takes the first gap that fits. Now small moves into column 3 of row 1.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 120px;
grid-auto-flow: row dense;
gap: 8px;
}Dense packing suits galleries, tile layouts, and dashboards, where the visual order of items does not matter and holes look worse than reordering.
Mixing explicit and auto-placed items
Explicit and auto-placed items can share a grid. The algorithm places explicit items first, and auto-placed items fill the remaining cells:
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}
.featured {
grid-column: 1 / 3;
grid-row: 1 / span 2;
}Every other child is auto-placed. They skip over the 2-by-2 block that .featured occupies, even if .featured is last in the HTML.
An item with only one axis set, for example grid-row: 2, is a common source of surprises. In row flow, the browser places it in row 2 and picks the first column where it fits. Items placed after it start from the beginning of the grid again, so they can fill row 1 and then wrap past it.
Changing order with the order property
Grid items honor the order property, which sorts them before auto-placement. The default is 0, and lower values come first. Items with equal values keep source order. order has the same accessibility cost as dense, because it affects only visual order. Prefer fixing the HTML order when you can.
Sizing implicit tracks
Auto-placed items often create implicit tracks. Without configuration, these tracks are sized auto, meaning they fit their content. You can set a size with grid-auto-rows or grid-auto-columns:
grid-auto-rows: 120pxgives every implicit row a fixed height.grid-auto-rows: minmax(100px, auto)gives rows a minimum height and lets them grow with their content.grid-auto-rows: 100px 200pxalternates between the two sizes, repeating the pattern.
The minmax(100px, auto) form is usually the safest. A fixed height can clip text when a user enlarges their font size.
Common errors
- Setting
grid-auto-flow: columnwithout rows. If you do not definegrid-template-rows, every row is implicit and the grid ends up with one row. All items then form one long horizontal strip. - Expecting
denseto reorder the DOM. It changes only where items are drawn. - Forgetting that
grid-auto-rowsdoes not affect explicit rows. Rows defined bygrid-template-rowsuse that property's sizes only. - Using
grid-row: 2on a single item and expecting later items to follow it. Only the item with an explicit position is moved. The others start from the top again.
Exercise
A grid has grid-template-columns: repeat(3, 1fr) and the default grid-auto-flow: row. It holds four children in this order: A with grid-column: span 2, B with grid-column: span 2, C with no placement, D with no placement. Where does C land in sparse mode, and what changes if you add dense?
Show answer
In sparse mode, A takes columns 1 to 2 of row 1. B needs two columns and cannot fit in column 3, so it goes to columns 1 to 2 of row 2. The cursor continues from B, so C goes to column 3 of row 2 and D to row 3, column 1. With dense, C goes back to the first gap that fits, which is column 3 of row 1. D then takes column 3 of row 2. The DOM order stays A, B, C, D, but C is now drawn above B's row.
Exercise
You want a list of 12 short items to read down each column first, with three rows per column and as many columns as needed. Write the CSS for the container.
Show answer
Define the rows and set column flow:
.list {
display: grid;
grid-template-rows: repeat(3, auto);
grid-auto-flow: column;
grid-auto-columns: max-content;
}
The three explicit rows are filled top to bottom. The browser then adds implicit columns, sized by grid-auto-columns, until all 12 items are placed.