01Let content set the limit
A card grid needs a rule for when another column stops being useful. Start with the content: can a heading, a short summary, and a link still breathe at this width? Pick a minimum from that test. The viewport size alone does not tell you how much room the grid actually has inside a sidebar or a constrained article.
In this example, 18rem is a starting point for readable cards, not a universal breakpoint. Let the browser fit as many columns as the container can support, and share the remaining space between them.
02One rule for the columns
<ul class="cards">
<li class="card">
<h2><a href="/blog/the-bow/">Bending pictures with a shader</a></h2>
<p>A closer look at the geometry behind a scrolling effect.</p>
<span class="category">Creative Development</span>
</li>
<!-- Repeat the list item for each article. -->
</ul>.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr));
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}
.card {
min-width: 0;
display: flex;
flex-direction: column;
gap: 1rem;
padding: 1.5rem;
border: 1px solid currentColor;
overflow-wrap: anywhere;
}
.card h2, .card p { margin: 0; }
.card .category { margin-top: auto; }auto-fit collapses unused tracks so existing cards can grow. minmax() gives each column a lower bound and a flexible upper bound. The inner min(100%, 18rem) allows that lower bound to shrink when the whole container is narrower than 18rem.
03Protect the layout from real content
A layout that works with three short headings may still fail with a long URL or an unbroken identifier. Allow the card to shrink, and give long text a wrapping policy. Avoid hiding overflow on the whole card merely to conceal a bug; doing so can also clip focus outlines or useful content.
When cards include images, reserve their space before the download finishes. Supply intrinsic width and height attributes, or a deliberate aspect ratio. Use object-fit only when cropping is acceptable for that image. For code samples inside a card, a horizontally scrollable code block is usually easier to read than breaking every line arbitrarily.
Preserve document order. Visually moving cards around without changing their reading order can make keyboard navigation difficult to follow. The same article sequence should make sense in one column and in three.
04Check between the comfortable widths
Resize slowly through the point where a column disappears. Try one card, two cards, a full row, and a final row with one item. Then increase the text size and replace your shortest heading with your longest. These cases reveal whether the layout follows a useful rule or merely matches one screenshot.
You may still need a media query when the composition itself changes, such as moving filters above the results. Keep that decision separate from how many cards fit. The grid can handle column count while your design handles hierarchy.
Reference: MDN — grid-template-columns.
