Sub topic 11
Card grids that adjust themselves with auto-fit and minmax()
This chapter assumes you can write a basic CSS Grid layout with display: grid, grid-template-columns, repeat(), fr units and gap. It also assumes you are comfortable with rem and % units. Earlier chapters built a fixed 12-column grid and changed it at breakpoints. Here you will build a grid that picks its own column count from the space it has, without any media queries.
The problem with breakpoint-driven card grids
A 12-column grid with media queries works well for page structure. It is clumsy for a collection of similar cards, such as products, articles or team members. You have to decide in advance that cards span 12, 6 or 4 columns, and you must pick breakpoints that match the content. If the card sits in a narrow sidebar one day and a wide main area the next, the viewport width tells you nothing about how much room the card's container really has.
What you want is a rule that says: "Each card needs at least this much width. Fit as many as you can, and share the leftover space." CSS Grid can express that rule directly.
The building blocks
repeat() with a keyword
You have used repeat(12, 1fr), where the first argument is a number. The first argument can also be the keyword auto-fill or auto-fit. The browser then works out how many tracks fit in the container and creates that many.
minmax()
minmax(min, max) sets a track's size range. The track is never smaller than min and never larger than max. In minmax(16rem, 1fr), each column is at least 16rem wide. If there is spare room, the column grows up to an equal share of it, which is what 1fr means.
Putting them together
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}Read it from the inside out:
min(100%, 16rem)is the smallest a column may be. It is16rem, unless the container is narrower than that, in which case it is the container's full width.minmax(..., 1fr)lets each column grow to share any leftover space equally.repeat(auto-fit, ...)creates as many such columns as fit, counting thegapbetween them.
Here is the markup to try it on:
<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>How the browser decides the column count
With a gap of 1.5rem and a minimum column of 16rem, the browser asks how many columns of at least 16rem fit, including the gaps between them. For n columns the needed width is n × 16rem + (n − 1) × 1.5rem.
| Container width | Columns that fit | Needed width | Result |
|---|---|---|---|
| 20rem | 1 | 16rem | One column, each card 20rem wide |
| 35rem | 2 | 33.5rem | Two columns, each about 16.75rem |
| 52rem | 3 | 51 rem | Three columns, each about 16.67rem |
| 70rem | 4 | 70.5rem would be needed for 4, so 3 fit | Three columns, each about 22.3rem |
The last row shows a common surprise. At 70rem, four columns would need 4 × 16 + 3 × 1.5 = 68.5rem, which does fit, so the result there is four columns of about 17.4rem each. Always do the arithmetic with the gap included. It is easy to forget that gaps take space and to predict one column too many.
The column count only changes when the container crosses one of these thresholds. Between thresholds, the columns stretch smoothly. That is the "fluid" behavior from the earlier chapter on fixed and fluid grids, combined with a stepped column count.
Why min(100%, 16rem) matters
Many tutorials write minmax(16rem, 1fr) without the min() wrapper. That works until the container is narrower than 16rem, for example on a very small phone or in a narrow sidebar with padding. A column cannot shrink below its minmax() minimum, so the grid overflows the container and causes a horizontal scrollbar.
Wrapping the minimum in min(100%, 16rem) caps it at the container's width. The column then shrinks to fit, and the overflow never happens. The 100% here refers to the grid container's width, so the rule stays correct wherever the grid is placed.
auto-fit versus auto-fill
Both keywords create as many columns as fit. They differ only when there are fewer items than columns.
auto-fillkeeps the empty columns. They take up space but hold nothing, so the cards stay at a consistent width and the row looks left-aligned with blank space at the end.auto-fitcollapses the empty columns to zero width. The1frcolumns that remain then grow to absorb the free space, so the existing cards stretch across the row.
Try this with two cards in a wide container:
.fill {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}
.fit {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}In .fill, two cards sit at roughly 16rem to 20rem each, with empty tracks to their right. In .fit, the two cards stretch to share the full width.
| Keyword | Empty tracks | Card widths with few items | Best for |
|---|---|---|---|
| `auto-fill` | Kept at full size | Stay close to the minimum | Lists where item count varies and you want a stable card size |
| `auto-fit` | Collapsed to zero | Stretch to fill the row | Short sets of cards that should fill the width |
When there are enough items to fill every row, the two keywords behave identically. The difference shows only on the last row, or with a short list.
Fitting the card grid into the page grid
A card grid does not replace the 12-column page grid. It lives inside it. A typical page uses the 12-column grid for large regions, and an auto-fit grid inside one region for its cards.
<div class="page">
<div class="grid">
<main class="main">
<div class="cards">
<article class="card">One</article>
<article class="card">Two</article>
<article class="card">Three</article>
</div>
</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}The .cards grid sizes itself from the width of .main, not from the viewport. If the sidebar is removed and .main spans all twelve columns, the card grid gains columns on its own. That is the main advantage over media queries: the component responds to its own space.
Keeping gutters consistent
Use the same 1.5rem value for the page grid's column-gap and the card grid's gap. Cards then feel like part of the same system, and the horizontal spacing is uniform. The cards will not line up with the 12 page columns, though, because their columns are calculated separately. If cards must align with the outer columns, use subgrid (covered in a later chapter) or a fixed column count with span.
Trade-offs and limits
An auto-fit grid is not always the right tool.
- Column count is not under your control. You cannot say "always three columns on wide screens". A container wide enough for four columns gets four. If design requires an exact count at set widths, use
repeat(3, 1fr)and media queries. - Orphans on the last row. Seven cards in a four-column grid leave three cards and a gap on the last row. This is normal for a grid of equal items, and it is usually acceptable.
- No alignment with the page columns. As noted above, the cards follow their own tracks.
- Content must tolerate stretching. With
1frcolumns, a card can be much wider than its minimum. Images should usemax-width: 100%orobject-fitso they scale properly.
Common mistakes
- Forgetting the gap in your arithmetic and expecting one more column than appears.
- Writing
minmax(16rem, 1fr)with nomin()guard, and getting overflow on narrow containers. - Using
auto-fitand being surprised when one card becomes enormous. Switch toauto-fillor cap the maximum. - Putting a percentage minimum such as
minmax(25%, 1fr)and expecting four columns. With a gap, four 25% columns do not fit, so you get three. - Setting a fixed
widthon cards. Cards in a grid should fill their track, so leave their width asauto.
Exercise
A container is 50rem wide. The grid uses repeat(auto-fit, minmax(min(100%, 14rem), 1fr)) with gap: 2rem. How many columns does the browser create, and roughly how wide is each column?
Show answer
Check n columns: n × 14 + (n − 1) × 2 must be at most 50. For n = 3: 42 + 4 = 46, which fits. For n = 4: 56 + 6 = 62, which does not. So there are 3 columns. The free space is 50 − 4 (gaps) = 46rem shared by three columns, so each column is about 15.33rem wide.
Exercise
You have two cards in a wide container. With auto-fit they stretch across the whole row, which looks too wide. Name two ways to keep them near the minimum width.
Show answer
Switch from auto-fit to auto-fill, so the empty tracks keep their size and the two cards stay narrow. Alternatively, cap the maximum track size, for example minmax(min(100%, 16rem), 20rem), and set justify-content: start.
Exercise
Why does repeat(auto-fit, minmax(16rem, 1fr)) cause a horizontal scrollbar in a 15rem-wide container, and what change fixes it?
Show answer
A track cannot be narrower than its minmax() minimum, so the 16rem column is wider than the 15rem container and overflows. Replace the minimum with min(100%, 16rem) so the minimum never exceeds the container's width.