Responsive card grid without media queries
A CSS grid snippet that fits as many columns as the space allows and wraps on its own, using auto-fit and minmax.
Updated
This grid fits as many equal columns as will fit in the available width and drops to fewer columns as the space shrinks, with no media queries.
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}
Change 16rem to the narrowest width a card should be allowed to reach.
How it works
Read the grid-template-columns value from the inside out:
minmax(16rem, 1fr)says each column is at least16remwide and can grow to an equal share of any leftover space.repeat(auto-fit, ...)tells the browser to create as many of those columns as fit in the row.min(100%, 16rem)guards against overflow. If the container is narrower than16rem, the minimum becomes100%and you get one full-width column in place of a horizontal scrollbar.gapsets the space between rows and columns. Unlike margins, it adds nothing at the outer edges.
So a container 70rem wide gets four columns, since four 16rem columns plus three gaps fit and five do not. As the container narrows, the count falls to three, two and then one.
auto-fit or auto-fill?
The two behave the same until there are fewer items than columns that would fit.
| Keyword | With two cards in a wide container |
|---|---|
auto-fit |
Empty tracks collapse, and the two cards stretch to fill the row |
auto-fill |
Empty tracks are kept, and the two cards stay at column width, aligned to the start |
Use auto-fit when a short row should stretch. Use auto-fill when cards should keep a consistent width however many there are.
Equal-height cards
Grid items in the same row stretch to the same height by default. To pin a button or footer to the bottom of each card, make the card a flex column:
.card {
display: flex;
flex-direction: column;
}
.card .actions {
margin-top: auto;
}
Capping the number of columns
To stop at three columns, raise the minimum so that a fourth can never fit:
.card-grid {
--gap: 1.5rem;
--max-columns: 3;
--min-width: 16rem;
display: grid;
gap: var(--gap);
grid-template-columns: repeat(
auto-fit,
minmax(
min(100%, max(var(--min-width), (100% - (var(--max-columns) - 1) * var(--gap)) / var(--max-columns))),
1fr
)
);
}
The max() chooses the larger of your minimum width and one third of the row after gaps. In a wide container the second value wins, which makes each column a third of the row, so a fourth cannot fit.
Spanning columns
A featured card can take two columns:
.card.featured {
grid-column: span 2;
}
In a one-column layout a span of two forces a second column and can cause overflow. Apply the span only when there is room, for instance inside a container query.
The same idea with flexbox
Flexbox can wrap too, but items in the last row stretch to fill it, so a leftover card ends up wider than the rest:
.card-row {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.card-row > * {
flex: 1 1 16rem;
}
Use grid when the columns must line up from row to row, and flexbox when a stretched last row is acceptable.
More layout: how to center an element.