CSS Snippet
CSS auto-fit + minmax
The one-line CSS trick that creates fully responsive grids without media queries.
Last updated: October 2026
Quick answer: Use grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) to create a responsive grid. The grid automatically fits as many 250px-minimum columns as possible, and each one stretches evenly to fill the row. No media queries required.
🎬 Interactive Live Demo
Resize your browser window to see the grid respond. Drag the slider to change the minimum column width.
💡 Drag the slider and resize your browser window to see the grid react automatically.
The One-Line Responsive Grid
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
That's it. No media queries, no JavaScript, no breakpoints. The grid figures out how many columns fit in the available space:
- Wide container: 4 or 5 columns.
- Medium container: 2 or 3 columns.
- Narrow container (mobile): 1 column.
How Each Part Works
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
│ │ │
│ │ └── Max width: 1 fraction of available space
│ └─── Function: distribute tracks automatically
└─── Keywords: create as many tracks as fit
Breaking it down:
repeat(...)— repeats the track pattern.auto-fit— fills the row with as many tracks as will fit.minmax(250px, 1fr)— each track is at least 250px wide, but grows to fill extra space evenly.
auto-fit vs auto-fill — The Critical Difference
These two keywords look identical but behave differently when there are fewer items than available columns:
/* auto-fit — collapses empty columns, items stretch */
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
/* Result: 3 items → 3 evenly-sized stretched cards */
/* auto-fill — keeps empty columns, items stay sized */
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
/* Result: 3 items → 3 cards + empty space on the right */
💡 Rule of thumb: Use auto-fit for card grids (you want items to fill the row). Use auto-fill when you want items to stay a fixed size regardless of container width.
Practical Examples
Responsive card grid:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.5rem;
}
Responsive photo gallery:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 8px;
}
Responsive dashboard with different min widths:
.dashboard {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
gap: 1rem;
}
Tailwind CSS Version
Tailwind supports arbitrary values with square brackets:
<div class="grid grid-cols-[repeat(auto-fit,minmax(250px,1fr))] gap-4">
<div>Card 1</div>
<div>Card 2</div>
<div>Card 3</div>
<div>Card 4</div>
</div>
Or for a more reusable setup, define a custom utility in your CSS:
@utility grid-auto-cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
👉 Read our Tailwind Grid Guide and Custom Utilities Guide.
🎯 auto-fit vs auto-fill — Side by Side
| Keyword | Empty columns | Best for |
|---|---|---|
auto-fit |
Collapsed | Card grids, galleries, dashboards |
auto-fill |
Kept as empty space | Fixed-size items, alignment scenarios |
⚠️ Common Mistakes
- Using
1fras the minimum.minmax(1fr, 1fr)doesn't work. The minimum needs a fixed unit (px, rem, or ch). - Forgetting
gap. Without a gap, cards touch each other. Always set a gap. - Setting the minimum too high. A 500px minimum forces single-column layout on mobile. Keep it between 180px and 300px for most grids.
- Confusing
auto-fitwithauto-fill. Try both if the layout doesn't look right. - Expecting this to work on flex containers.
auto-fitis Grid-only. For Flexbox, useflex-wrapwithflex: 1 1 250px.
❓ Frequently Asked Questions
What does auto-fit do in CSS Grid?
auto-fit tells the grid to fit as many columns as possible into the container. Empty columns are collapsed, so remaining columns stretch to fill the space. This makes grids automatically responsive.
What is the difference between auto-fit and auto-fill?
auto-fit collapses empty columns and stretches items to fill the row. auto-fill keeps empty columns, leaving whitespace. For responsive card grids, auto-fit is usually what you want.
What is minmax() in CSS Grid?
minmax() defines a minimum and maximum size for a grid track. In minmax(250px, 1fr), each column is at least 250px wide but can grow to fill available space up to 1 fractional unit.
Do I still need media queries for responsive grids?
For most card grids, no. auto-fit + minmax handles responsiveness automatically. You still need media queries for layout changes (like switching a sidebar from horizontal to vertical) that aren't about item count.
Can I use auto-fit with Tailwind CSS?
Yes. Tailwind supports arbitrary values: grid-cols-[repeat(auto-fit,minmax(250px,1fr))] creates the same responsive grid without writing custom CSS.