Start with the space the cards need
A card needs enough room for its title, description, and link. Start with a minimum track size of 16rem. Use gap for the spacing between cards so the first and last cards can line up with the container edges.
Let the grid choose the column count
Set display to grid and try a grid-template-columns value of repeat(auto-fit, minmax(min(100%, 16rem), 1fr)). The min() caps the track minimum at the container width when space is tight. The 1fr maximum shares the available space across the tracks.
Check an incomplete row
Load the layout with one card and again with four. Where extra tracks are entirely empty, auto-fit collapses them. If a single card looks too wide, constrain the grid or choose auto-fill to keep room for empty tracks.
Test real content at small widths
Use a long title, a short title, and one card with a long unbroken string. Try overflow-wrap: anywhere on the text if the string runs outside its card. Narrow the container until it fits only one column, then check the card order with Tab. Keep the source order aligned with the reading order.
Try the grid in a narrow sidebar
Wrap your grid in a container with a maximum width of 28rem and put it next to the main content. Repeat the exercise at the same browser width with the grid in the main column. This lets you see the grid respond to the space its parent provides. A page-wide breakpoint can miss this difference.
Choose what empty tracks should do
Compare two cards in a container wide enough for three tracks. With auto-fit, the available tracks for the two cards can grow after the empty track collapses. With auto-fill, the empty track still takes up space. Use this comparison to decide how the last few cards should look in a collection with a variable item count.
Keep card actions aligned
If cards in the same row share a height, use a column flex layout inside each card and apply margin-block-start: auto to its action area. Give the action area padding or a gap so long descriptions still have space above the link. Check the result with titles that wrap to two or three lines.
Comments