Sub topic 11
Grids that adapt without media queries
This chapter assumes you know CSS Grid basics (display: grid, gap, grid-template-columns), common length units, and how min() works. You will build a card grid that changes its column count by itself, with no breakpoints.
The working model
Two built-in grid features do most of the work:
repeat(auto-fit, ...)creates as many columns as fit in the container.minmax(MIN, MAX)gives each column a smallest and a largest width.
Combined, they tell the browser: "Make each column at least 16rem wide. Fit as many as you can. Share the leftover space equally."
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}The 1fr maximum lets each column grow to absorb free space. The browser decides how many columns fit by dividing the container width (minus gaps) by the 16rem minimum. A wide container gets four columns, a medium one gets two, and so on.
The overflow problem
This version has a flaw. If the container is narrower than 16rem, for example on a small phone with side padding, the single column cannot shrink below its minimum. The grid overflows and causes horizontal scrolling.
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));
}min(100%, 16rem) resolves to whichever value is smaller. In a wide container it is 16rem. In a container narrower than 16rem it is 100%, so the column fills the container and stops there. Percentages inside minmax() here refer to the grid container's width.
auto-fit versus auto-fill
Both keywords create columns as space allows. They differ when there are fewer items than possible columns.
| Keyword | Empty columns | Effect with few items |
|---|---|---|
| `auto-fit` | Collapsed to zero width | Items stretch to fill the row |
| `auto-fill` | Kept at their size | Items stay at their column width, leaving blank space |
Use auto-fit when items should fill the row. Use auto-fill when you want a consistent column width regardless of item count.
Common errors
- Using
auto-fitwith a fixed maximum.minmax(16rem, 20rem)stops columns from growing, so leftover space appears at the end of the row. Use1fras the maximum if you want a filled row. - Forgetting the
min()guard. The layout looks fine on desktop and breaks only on narrow screens, so it is easy to miss. Test at 320px wide. - Writing
min(100%, 16rem)outsideminmax()and expecting columns.min()only limits a size. The column count still comes fromrepeat(). - Mixing up the arguments.
max(100%, 16rem)would force columns to be at least 16rem and cause the overflow again.
Exercise
A grid uses repeat(auto-fit, minmax(min(100%, 12rem), 1fr)) and a gap of 0. The container is 30rem wide. How many columns does it create, and how wide is each?
Show answer
Two columns need 24rem and three need 36rem, so only two fit (30 / 12 = 2.5, rounded down). The 1fr maximum shares the leftover space, so each column is 15rem wide.
Exercise
Your grid has 2 cards but the container could fit 4 columns. With auto-fit, how do the cards look? What changes with auto-fill?
Show answer
With auto-fit the two empty columns collapse, so the two cards stretch to fill the whole row. With auto-fill the empty columns keep their width, so the cards stay column-sized and blank space remains on the right.