Sub topic 11
Card grids that adjust themselves with auto-fit and minmax()
This chapter assumes you know basic HTML and CSS, the box model, and the repeat() and fr syntax from the earlier chapters on building a 12-column grid. Here you will build a card grid that changes its column count without media queries.
The problem with fixed column counts
A 12-column grid with cards that span 4 columns always shows three cards per row at wide widths. To change that, you write media queries at chosen breakpoints. That works, but it ties the layout to screen sizes instead of to the space the cards actually have. A card grid inside a narrow sidebar needs different breakpoints than the same grid in a full-width section.
A better question is: "How narrow can a card get before it is unreadable?" If you answer that once, the browser can work out the column count itself.
The pattern
<div class="cards">
<article>Card one</article>
<article>Card two</article>
<article>Card three</article>
<article>Card four</article>
</div>.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}The browser fits as many columns of at least 16rem as the container allows and shares the leftover space equally. Each part of the declaration has a job.
| Part | Meaning |
|---|---|
| `repeat(auto-fit, ...)` | Create as many columns as fit in the container. |
| `minmax(16rem, 1fr)` | Each column is at least 16rem wide and at most one share of the free space. |
| `min(100%, 16rem)` | The minimum never exceeds the container's width. |
| `gap: 1.5rem` | The gutter between rows and columns. |
How the browser decides
The browser divides the container width by the minimum column width plus the gap, rounds down, and creates that many columns. It then stretches each column with 1fr so no space is left over.
For example, take a 60rem container with a 1.5rem gap. Three columns need 3 × 16rem + 2 × 1.5rem = 51rem. Four need 4 × 16rem + 3 × 1.5rem = 67.5rem, which is too wide. The browser creates three columns, and each grows to about 18.3rem.
Why min(100%, 16rem)
Without min(), a plain minmax(16rem, 1fr) forces each column to be at least 16rem. In a container narrower than 16rem, such as a small phone with side margins, the column overflows and causes horizontal scrolling. Wrapping the minimum in min(100%, 16rem) caps it at the container's width, so a single column fits instead.
auto-fit versus auto-fill
Both keywords create as many columns as fit. They differ when there are fewer cards than columns.
auto-fillkeeps the empty columns. Three cards in a space for five leave two empty tracks, and the cards stay at their minimum-based size.auto-fitcollapses the empty tracks to zero width, so the1frcolumns share all the space and three cards stretch across the row.
Use auto-fit when you want cards to fill the row. Use auto-fill when you want a consistent card width regardless of how many cards there are.
Common mistakes
- Using
pxfor the minimum, which ignores the user's font-size settings. Preferrem. - Leaving out
min()and getting overflow on narrow screens. - Expecting
auto-fitto behave likeauto-fillwhen the last row has fewer cards. Cards in an incomplete last row still use the same column width. - Setting a fixed
widthon cards, which fights the column sizing. Let the grid size them.
Exercise
A container is 40rem wide. The grid uses repeat(auto-fit, minmax(min(100%, 12rem), 1fr)) with gap: 1rem. How many columns does the browser create, and what happens to the column width?
Show answer
Three columns fit, because 3 × 12rem + 2 × 1rem = 38rem, which is at most 40rem. Four would need 4 × 12rem + 3 × 1rem = 51rem. The 1fr maximum then grows each column to (40rem − 2rem) ÷ 3, about 12.67rem.
Exercise
You have two cards in a grid that could hold four columns. Which keyword makes the two cards stretch across the full row, auto-fit or auto-fill?
Show answer
auto-fit. It collapses the two empty tracks to zero width, so the 1fr columns of the two cards share the whole row. With auto-fill the empty tracks stay and the cards keep a narrower width.