Auto-Placement and Responsive Grids

Intermediate · 11 min

How grid auto-placement picks cells for items you did not position, what dense packing changes, and how auto-fit with minmax makes a grid responsive.

Before thisGrid Areas and Named Lines

Kevin Powell · 11 min

Auto-Placement and Responsive Grids

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Items with no explicit position are auto-placed in source order, each into the next free cell that fits, scanning row by row (`grid-auto-flow: row`, the default).
  • `grid-auto-flow: dense` goes back and fills earlier holes with later, smaller items. It improves packing but breaks the link between source order and visual order, which matters for keyboard and screen-reader users.
  • `repeat(auto-fill, minmax(200px, 1fr))` makes as many 200px-minimum columns as fit and lets them grow. `auto-fit` does the same, then collapses empty tracks so the items stretch to fill the row.
  • With 2 items in an 800px grid and `minmax(150px, 1fr)`, `auto-fill` leaves 5 tracks and the items are 160px wide, while `auto-fit` leaves 2 and the items are 400px wide.
  • `minmax(min(100%, 200px), 1fr)` stops a minimum larger than the container from causing horizontal overflow on narrow screens.

Most items are never positioned

Anything you don’t place by line number or area name is placed automatically. The algorithm is simple, which makes it easy to predict. It walks through the items in source order, and for each one it finds the first free spot that fits, scanning left to right, then down. When the row is full, it starts a new row, and it creates that row if the grid doesn’t have it yet. That is the implicit grid from the fundamentals lesson.

.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 120px;
  gap: 1rem;
}

Nine children make three rows, and you never wrote a row template. Think of it as a person seating guests at a table: each guest takes the next free seat from the left, moving to a new row when a row fills up.

Spans and holes

Auto-placement respects spans. If an item needs two columns and only one is left on the current row, it moves to the next row, and the leftover cell stays empty:

.a, .b { grid-column: span 2; }   /* in a 3-column grid */

In a test with three 100px columns, a took columns 1 and 2 of row one, b could not fit in column 3 and moved to row two, and a one-column item c then landed at row two, column three. The cell beside a stayed empty.

grid-auto-flow: dense changes that. After placing an item it can go back and fill earlier holes with later items that fit:

.gallery { grid-auto-flow: row dense; }

In the same test, c jumped back up to the hole in row one. This is good for galleries of mixed-size tiles. The cost is that the visual order no longer matches the source order, and keyboard focus and screen readers follow the source. Use dense where the order of items doesn’t carry meaning.

Responsive columns with no media query

You can have a grid that works out its own column count. The trick is repeat() with auto-fill or auto-fit and a minmax():

.cards {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
}

Read it inside out. Each column is at least 220px and at most 1fr. repeat(auto-fit, ...) asks the browser to fit as many of those columns as the container allows. Narrow container, one column. Wider container, more columns, and the spare space is shared, because the maximum is 1fr. No breakpoint ever appears in the CSS.

auto-fit against auto-fill

They’re identical until there are too few items to fill the row. Say the container is 800px, the minimum is 150px and there are only two items:

  • auto-fill creates 5 tracks (800 / 150). Two are filled, three stay empty and keep their width. The items measured 160px each.
  • auto-fit creates the same 5 tracks, then collapses the empty ones to zero. The two remaining tracks share the row, and the items measured 400px each.

Choose auto-fill when you want cards to stay a consistent size even when few exist, and auto-fit when you want them to stretch. When there are enough items to fill every row, you can’t tell them apart.

The overflow guard

A minimum that is bigger than the container breaks the layout. A minmax(200px, 1fr) track in a 150px container measured 200px wide in a test, overflowing it. Replacing the minimum with min(100%, 200px) capped it, and the track measured 150px. It costs a few characters and avoids a horizontal scrollbar on narrow phones.

Sizing the implicit tracks

Auto-created rows default to auto, which means as tall as their content. To give them a consistent height, set grid-auto-rows, usually with minmax() so taller content isn’t clipped:

.cards { grid-auto-rows: minmax(120px, auto); }

grid-auto-flow: column makes the algorithm fill columns first and create new columns, with grid-auto-columns sizing them. That is how a horizontally scrolling strip is built.

What the video covers

Kevin Powell’s “Breakpoint-Free CSS Grid Layouts” (February 2025, about eleven minutes) lists these chapters: using auto-fit, adding in minmax(), the full build, making sure we never have an overflow, auto-fit versus auto-fill, and making the CSS more readable. His description says the full build is part of his Frontend Masters course. It’s centred on the responsive-columns half of this lesson. I could not read a transcript, and the video has no chapter on the placement algorithm or dense, so those sections are the prose’s own. The Optimistic Web runner-up covers auto-placement and the explicit/implicit distinction.

Where this fits

This lesson uses the tracks and lines from grid fundamentals and named areas. The idea of layouts that adapt without breakpoints continues in intrinsic and fluid layouts, and the next lesson, subgrid, solves the one thing these auto grids can’t: aligning the insides of cards.

Check yourself

  1. 01You place one item with `grid-column: span 2` in a three-column grid, and the next item also spans 2. What happens to the second row's first cell?

    With the default flow the second item doesn't fit in the one cell left at the end of row one, so it moves to row two and the hole stays empty. With `grid-auto-flow: dense`, later one-column items can be backfilled into that hole, which changes visual order.

  2. 02What is the visible difference between `auto-fit` and `auto-fill` when there are fewer items than columns would fit?

    `auto-fill` keeps the empty tracks it created, so the items stay at their normal size and leave blank space at the end of the row. `auto-fit` collapses those empty tracks to zero width, so the `1fr` maximum lets the remaining items grow to fill the row.

  3. 03Why write `minmax(min(100%, 200px), 1fr)` instead of `minmax(200px, 1fr)`?

    If the container is narrower than 200px, a plain 200px minimum forces a column wider than its container. In a test with a 150px container the track was 200px wide. With `min(100%, 200px)` the minimum can't exceed the container, so the track was 150px.

  4. 04How do you set the size of rows the grid creates on its own?

    `grid-auto-rows`, for example `grid-auto-rows: minmax(100px, auto)`. These implicit rows appear when there are more items than the explicit `grid-template-rows` covers. `grid-auto-columns` does the same when `grid-auto-flow: column` is in use.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark