Responsive card grid without media queries
Let CSS Grid choose how many columns fit and use auto-fit to collapse columns that have no cards.
You have a grid of cards. You wrote three columns, then a media query for two, then another for one, and the sidebar now needs a fourth. Each breakpoint is a guess about a width the card never needed to know.
Give the grid a minimum card width and let the browser count the columns.
.cards {
display: grid;
gap: 16px;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}Every column is at least 200px wide. The browser fits as many as it can in the container, then shares the leftover space between them through 1fr. Resize the window and the grid goes from four columns to three to two to one with no media query.
The column count
The rule is arithmetic. The browser takes the largest column count where the 200px minimums and the 16px gaps still fit inside the container.
In a 900px container that is four columns (4 × 200 + 3 × 16 = 848). The leftover 52px is split four ways, so each card is 213px wide. At 360px one column fits, so the card is 360px.
auto-fit or auto-fill
Both keywords start with the same number of tracks. They differ when entire columns are empty, as with two cards in that 900px container. A filter that leaves two results hits this case.
/* two cards, 900px container */
.fit { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); } /* 442px each */
.fill { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); } /* 213px each */With auto-fit the empty columns collapse to zero width, so the two cards stretch and share the full row: 442px each. With auto-fill the empty columns stay, so the cards keep their 213px and two empty slots sit on the right.
Use auto-fit to stretch two results across the container, or auto-fill to keep room for a third and fourth result. A short last row still lines up with a full row above it because those columns already contain cards.
A narrow container
If the container can be narrower than your minimum, a 200px column overflows it. Wrap the minimum in min() so it never exceeds the container: minmax(min(200px, 100%), 1fr). In a 160px container the column is then 160px wide, and the grid stays one column.