Sub topic 7
Responsive columns with auto-fit
This chapter assumes you can write a grid container with grid-template-columns, and that you know fr units, gap, repeat() and minmax() from earlier chapters. It shows how to let the browser choose the column count so a card grid adapts to its container without media queries.
The one-line pattern
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}Each part has a job:
minmax(200px, 1fr)describes one column. It is never narrower than 200px and may grow to an equal share of the free space.repeat(auto-fit, ...)tells the browser to repeat that column as many times as fit, instead of using a number you wrote.gapis included in the calculation. The browser counts it when deciding how many columns fit.
How the browser decides
The browser takes the container's width and asks how many columns, at their minimum size plus the gaps between them, fit on one row. It creates that many columns. Then it divides the leftover space among them, because each maximum is 1fr.
With a 200px minimum and a 1rem (16px) gap, the results look like this:
| Container width | Columns | Width of each column |
|---|---|---|
| 400px | 1 | 400px |
| 700px | 3 | about 222.7px |
| 1000px | 4 | 238px |
At 700px, three columns need 3 × 200 + 2 × 16 = 632px. Four would need 848px, which does not fit. The remaining 68px is split three ways, so each column grows from 200px to about 222.7px.
A complete example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Auto-fit cards</title>
<style>
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
padding: 1rem;
}
.cards > article {
padding: 1rem;
background: #eef3f8;
border: 1px solid #b8c7d9;
}
</style>
</head>
<body>
<section class="cards">
<article>One</article>
<article>Two</article>
<article>Three</article>
<article>Four</article>
<article>Five</article>
</section>
</body>
</html>Resize the window and watch the cards. The column count drops from four to three to two to one as the window narrows. No media query is involved.
Common errors
Using a fixed maximum. Writing minmax(200px, 300px) stops columns from growing past 300px. The leftover space then stays empty at the end of each row. Use 1fr as the maximum when you want columns to fill the row.
Using auto-fit with a plain length. repeat(auto-fit, 200px) is valid, but columns never stretch, so the grid will not fill the row. Pair it with minmax().
Expecting a fixed count. The count follows the container, not the viewport. A grid nested in a narrow sidebar gets fewer columns than the same grid in a wide main area.
Forgetting the rows. auto-fit only affects columns. Rows are created implicitly as items wrap, and their height follows the content unless you set grid-auto-rows.
Overflow on tiny screens. If the container is narrower than 200px, a 200px minimum overflows it. A later chapter covers the fix with min().
Try it
Exercise
A grid container is 900px wide and uses grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;. How many columns does the browser create, and how wide is each one?
Show answer
Three columns, each 286.67px wide. Three columns need 3 × 250 + 2 × 20 = 790px, which fits. Four need 4 × 250 + 3 × 20 = 1060px, which does not. The leftover width after removing the two gaps is 900 − 40 = 860px, and 860 / 3 is about 286.67px.
Exercise
Your cards stay at exactly 300px wide and leave a blank strip at the right edge of the container. The rule is repeat(auto-fit, minmax(300px, 300px)). What is the cause, and what is the fix?
Show answer
The maximum of minmax() is a fixed 300px, so columns cannot grow into the leftover space. Change it to minmax(300px, 1fr).