Sub topic 11
Grids that adapt without media queries
This chapter assumes you know basic CSS Grid (display: grid, gap, grid-template-columns, the fr unit). It also assumes you know how min() behaves from the earlier chapters. Here you will build a card grid that changes its column count by itself, learn why it sometimes overflows, and fix that.
The problem with fixed column counts
A common first approach is to set a column count and change it at breakpoints:
.cards { display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 40rem) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 64rem) {
.cards { grid-template-columns: repeat(3, 1fr); }
}This ties the layout to the viewport width. If the grid sits in a narrow sidebar on a wide screen, the viewport query still asks for three columns and the cards are squeezed. An intrinsic grid decides from the width of the grid container itself, so it needs no breakpoints and works wherever you place it.
The core pattern
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Each part does one job:
minmax(16rem, 1fr)sets the range for one column. It is never narrower than 16rem and can grow to an equal share of the free space.repeat(auto-fit, ...)asks the browser to create as many such columns as fit in the container.1fras the maximum makes the columns absorb leftover space, so the row is filled.
The browser computes the column count as the largest n for which n * min + (n - 1) * gap is at most the container width. Written as a formula: n = floor((width + gap) / (min + gap)), with a minimum of 1.
For example, take a 16px root font size, so min is 256px, and a gap of 16px.
| Container width | Calculation | Columns | Column width |
|---|---|---|---|
| 250px | `min` limited to the container, see below | 1 | 250px |
| 520px | floor(536 / 272) = 1 | 1 | 520px |
| 700px | floor(716 / 272) = 2 | 2 | 342px |
| 800px | floor(816 / 272) = 3 | 3 | 256px |
The column count moves in whole steps, but the column width changes continuously between steps.
auto-fit versus auto-fill
Both keywords create as many tracks as fit. They differ when there are fewer items than tracks.
| Keyword | Empty tracks | Visible result with 2 items in a wide container |
|---|---|---|
| `auto-fill` | Kept, and they still take up space | Two cards of the minimum size, with blank space on the right |
| `auto-fit` | Collapsed to zero width | Two cards stretched across the whole row |
Use auto-fit when you want items to fill the row. Use auto-fill when you want items to keep a consistent column width regardless of how many there are, for example when you want a lone card to match the width of cards in other grids on the page.
The overflow bug and the min() guard
Consider minmax(16rem, 1fr) in a container only 15rem wide, such as a narrow sidebar or a phone at high zoom. The browser cannot make a column smaller than its minimum, so the single column is 16rem wide and sticks out of the container by 1rem. The result is a horizontal scrollbar or clipped content.
The fix is to cap the minimum with min():
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}The percentage in a grid track size resolves against the grid container's content width. So min(100%, 16rem) is 16rem in any container wider than that. In a narrower container it becomes the container's width, and the single column fits exactly. This guard costs nothing in wide containers, so treat it as the default form of the pattern.
Here is a complete page to try. Resize the window, or drop the grid into a narrow parent, and watch the column count change.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Intrinsic card grid</title>
<style>
body { font-family: system-ui, sans-serif; margin: 0; }
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}
.card {
padding: clamp(1rem, 3vw, 1.5rem);
border: 1px solid #ccc;
border-radius: 0.5rem;
}
</style>
</head>
<body>
<main class="container">
<h1>Cards</h1>
<div class="cards">
<article class="card">One</article>
<article class="card">Two</article>
<article class="card">Three</article>
<article class="card">Four</article>
<article class="card">Five</article>
</div>
</main>
</body>
</html>The page combines three tools from this tutorial. min() sets the container width, clamp() sets the card padding, and minmax(min(...), 1fr) sets the grid columns.
Content that refuses to shrink
A bare 1fr track is shorthand for minmax(auto, 1fr). The auto minimum means the track will not shrink below the min-content size of its items, so a long unbreakable word or a wide image can push the track wider than you expect.
In the card pattern the minimum is a definite length (min(100%, 16rem)), so tracks cannot grow past it because of content. The content can still overflow its own track, so add overflow-wrap: anywhere for long strings. For images, add max-width: 100%.
If you write a fixed-count grid, use minmax(0, 1fr) instead of 1fr to avoid the same problem:
.fixed-three {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
}Limiting the number of columns
The pattern has no maximum column count. On a very wide container you might get seven thin cards. You can set a ceiling by making the minimum width depend on the container, using max():
.cards {
--col-min: 16rem;
--gap: 1rem;
--max-cols: 4;
display: grid;
gap: var(--gap);
grid-template-columns: repeat(
auto-fit,
minmax(
min(100%, max(var(--col-min), (100% - (var(--max-cols) - 1) * var(--gap)) / var(--max-cols))),
1fr
)
);
}Read it from the inside out:
(100% - (var(--max-cols) - 1) * var(--gap)) / var(--max-cols)is the width of one column if exactly four columns filled the row.max(var(--col-min), ...)raises the minimum to that width when the container is wide. Four columns then fit, and a fifth cannot.min(100%, ...)keeps the overflow guard.
--max-cols must be a plain number without a unit, because the expression multiplies and divides by it. Browsers do their layout arithmetic in floating point, so in rare cases rounding can make four columns fail to fit by a fraction of a pixel and you get three. If you see that, reduce the computed width slightly, for example by subtracting 0.1px in the numerator.
What this pattern cannot do
- Exact breakpoints. You cannot say "three columns between 900px and 1200px". The count follows from the arithmetic above. If you need exact counts, use media queries or container queries.
- Balanced last rows. With five items and three columns you get a row of three and a row of two. The grid does not rebalance the rows.
- Different spans per width. A featured card that spans two columns on wide layouts needs
grid-column: span 2. Withauto-fitthat span can exceed the column count in a one-column grid, which creates an implicit extra column and overflow. Apply spans only inside a media or container query, or keep the layout single-span.
Putting it together
A practical checklist for an intrinsic grid:
- Pick a minimum column width in
rem, so it follows the user's font-size setting. - Use
repeat(auto-fit, minmax(min(100%, <width>), 1fr)). - Set
gapinremor withclamp()so spacing scales with the layout. - Choose
auto-fillonly if you want empty tracks to be kept. - Add the
max()ceiling only if a column limit is a real design requirement.
Exercise
A grid uses grid-template-columns: repeat(auto-fit, minmax(min(100%, 12rem), 1fr)) with gap: 1rem and a 16px root font size. How many columns does it have in a container 600px wide, and how wide is each column? What happens in a container 150px wide?
Show answer
The minimum is 12rem = 192px and the gap is 16px. The column count is floor((600 + 16) / (192 + 16)) = floor(616 / 208) = 2. Two columns use 600 - 16 = 584px, so each column is 292px wide. A container 150px wide is narrower than 192px, so min(100%, 12rem) resolves to 150px. The grid has one column that is 150px wide and does not overflow.
Exercise
You have two items in an auto-fit grid inside a very wide container, and the cards stretch across the whole row. You want them to stay at normal card width instead. What single change fixes this, and what is the trade-off?
Show answer
Change auto-fit to auto-fill. The empty tracks are kept instead of collapsing, so the two cards stay at the width of the other columns and blank space remains at the end of the row. The trade-off is that the cards no longer fill the row.