More CSS answers

by Danny
Published 2 min read

Use CSS Grid with repeat(auto-fit, minmax()) to create a card grid that adds and removes columns as space allows, with no breakpoints. Learn auto-fit vs auto-fill too.

The short answer

#

Let the grid work out the number of columns for you. repeat(auto-fit, minmax(...)) fits as many columns as the container allows, each at least a minimum width, and stretches them to fill the row.


_10
.grid {
_10
display: grid;
_10
grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
_10
gap: 1.5rem;
_10
}

Why it happens

#

Media queries respond to the width of the viewport, not the width of the space your component sits in. A card grid in a sidebar and the same grid in the main column need different column counts, but the viewport is the same, so breakpoints end up wrong in one of them.

CSS Grid can decide the column count itself. Three pieces make it work:

  • minmax(16rem, 1fr) says each column must be at least 16rem wide, and can grow to share the leftover space equally.
  • repeat(auto-fit, ...) tells the browser to create as many of those columns as fit in the container.
  • When there is not enough room for another 16rem column, the items wrap to a new row.

The result is a layout that adapts to its container, wherever you put it.

How to fix it

#

Use auto-fit with minmax

This is the pattern most people want: cards that fill the row.


_10
<ul class="grid">
_10
<li class="card">One</li>
_10
<li class="card">Two</li>
_10
<li class="card">Three</li>
_10
</ul>


_10
.grid {
_10
display: grid;
_10
grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
_10
gap: 1.5rem;
_10
padding: 0;
_10
list-style: none;
_10
}

The min(16rem, 100%) part matters. With a plain 16rem minimum, a container narrower than 16rem (a small phone, or a narrow sidebar) would overflow sideways. Wrapping it in min() lets the column shrink to the container width in that case.

Choose between auto-fit and auto-fill

The two keywords only differ when there are fewer items than columns would fit.


_10
/* Items stretch to fill the whole row */
_10
.fit {
_10
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
_10
}
_10
_10
/* Empty column tracks are kept, so items stay at their natural size */
_10
.fill {
_10
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
_10
}

With two items on a wide screen, auto-fit collapses the empty tracks and the two cards grow very wide. auto-fill keeps the empty tracks, so the two cards stay about the same width as they would in a full row. Use auto-fill when a lone card stretching across the page would look odd.

Cap the number of columns

Sometimes you want the automatic behaviour but never more than, say, four columns. Make the minimum width depend on the container width as well.


_18
.grid {
_18
--max-columns: 4;
_18
--min-width: 14rem;
_18
--gap: 1.5rem;
_18
_18
display: grid;
_18
gap: var(--gap);
_18
grid-template-columns: repeat(
_18
auto-fill,
_18
minmax(
_18
max(
_18
var(--min-width),
_18
(100% - (var(--max-columns) - 1) * var(--gap)) / var(--max-columns)
_18
),
_18
1fr
_18
)
_18
);
_18
}

The max() picks whichever is larger: your minimum width, or the width a column would have if there were exactly four. So the grid never fits a fifth column. For very narrow containers, wrap var(--min-width) in min(..., 100%) as before.

Use flexbox when rows should not line up

If you want items in the last row to grow and share the space, rather than lining up with the columns above, flexbox does that without media queries too.


_10
.tags {
_10
display: flex;
_10
flex-wrap: wrap;
_10
gap: 0.5rem;
_10
}
_10
_10
.tags > * {
_10
flex: 1 1 10rem;
_10
}

Common pitfalls

#
  • Minimum wider than the container. A fixed minimum like 20rem causes horizontal scrolling on small screens. Use min(20rem, 100%).
  • Mixing up auto-fit and auto-fill. If a single item fills the whole row and you did not want that, switch to auto-fill.
  • Using percentages for gaps with fr. Keep gap in rem or px; it is subtracted before fr space is shared out.
  • Expecting it to change the layout of the card itself. This pattern only sets the column count. To change what is inside a card based on its width, use container queries.
#

Was this resource helpful?