Card Grid with Auto-Fit
What you'll learn: How to create a responsive card layout that automatically adds or removes columns as the screen resizes, without media queries.
The Core Idea
Imagine you have a collection of product cards, blog posts, or image thumbnails. You want them to fill the available space efficiently—showing more columns on wide screens and fewer on narrow ones—without writing breakpoint logic for every possible screen size.
auto-fit combined with minmax() solves this beautifully. The grid automatically calculates how many columns fit based on your minimum card width, and collapses empty tracks so cards expand to fill all available space.
How It Works
The pattern looks like this:
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
This tells the grid: "Create as many columns as will fit (auto-fit), where each column is at least 250px wide but can grow to fill available space (1fr)."
On a wide screen, you might get 4 columns. On a tablet, maybe 2. On mobile, just 1. The grid figures it out automatically based on the 250px minimum and available container width.
Typical HTML Structure
<div class="card-grid">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
<div class="card">Card 4</div>
<!-- More cards... -->
</div>
Each card becomes a grid item that flows into the auto-calculated columns. No JavaScript, no media queries—just one line of CSS doing all the responsive work.
Key Takeaway: Use repeat(auto-fit, minmax(min-size, 1fr)) to create responsive card grids that automatically adjust column count based on available space, eliminating the need for breakpoint-specific media queries.