Use grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) for a responsive four-column-style layout that wraps as the viewport narrows without a breakpoint for every screen size. minmax() protects the minimum card width, auto-fit expands the tracks that exist, and the same pattern works well for product cards, galleries, and blog grids.
Responsive CSS Grid 4-Column Layout
A step-by-step guide to a responsive CSS Grid layout that collapses from four columns down to one, using auto-fit and minmax with zero media queries.
// The short answer
In this guide · 5 sections
Hardcoding a 12-column grid and writing endless media queries just to make four columns stack on mobile kills your development speed. CSS Grid has a built-in mathematical engine that handles the viewport calculations for you, turning a screen full of breakpoints into a single, elegant line of code.
- The Magic Formula:
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); - Media Queries Needed: Zero (automatic wrapping)
- Browser Support: 97%+ (all modern browsers)
- Best Use Case: Product cards, portfolio galleries, blog post grids
The Modern Approach: Using auto-fit and minmax()
Traditional grid systems force you to calculate fractions like span 3 for a fourth of a 12-column layout. That approach breaks the moment the screen shrinks, requiring heavy CSS adjustments to fix the overflow. Relying on grid-template-columns: repeat(4, 1fr) might look fine on a 1920px monitor, but it will brutally squish your content or cause horizontal scrolling on an iPhone.
The modern CSS Grid approach flips this logic entirely. You define the minimum acceptable width for a card, and the browser figures out exactly how many columns can safely fit into the current viewport.
Step-by-Step: Creating the 4-Column Grid
Building a fluid grid requires almost no markup. You only need a parent container and the child elements you want to align.
1. Setting Up the HTML Structure
Keep your DOM clean. Wrap your content blocks inside a main grid container.
<div class="grid-container">
<div class="grid-item">Card 1</div>
<div class="grid-item">Card 2</div>
<div class="grid-item">Card 3</div>
<div class="grid-item">Card 4</div>
</div>2. Applying the Base CSS Grid
Activate the grid environment on the parent container. Adding a gap replaces the old, messy margin hacks we used to rely on with Flexbox or floats.
.grid-container {
display: grid;
gap: 20px; /* Handles both row and column spacing evenly */
}
.grid-item {
background-color: #f4f4f4;
padding: 20px;
border-radius: 8px;
}3. Making It Fluid (No Media Queries Required)
Here is where the real power of CSS Grid takes over. By combining repeat(), auto-fit, and minmax(), you instruct the browser to make the columns intelligent.
.grid-container {
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}Let's break down exactly what happens under the hood here:
minmax(250px, 1fr): this tells the browser that no column should ever shrink below 250 pixels. If extra space is available, the1fr(one fractional unit) kicks in, allowing the column to stretch and fill the void.auto-fit: this calculates the container width and divides it by 250px. If your container is 1100px wide, the browser easily fits 4 columns. When a user resizes the window down to 700px, 4 columns of 250px can no longer fit. The browser automatically drops the layout down to 2 columns, stretching them slightly to fill the screen perfectly.
auto-fit vs. auto-fill: Which One Do You Need?
Developers constantly swap these two keywords, but they behave very differently when you have fewer items than your maximum column count (see this full breakdown of auto-fit vs. auto-fill for edge cases beyond a simple 4-column grid).
Use auto-fit when you want your items to stretch and fill the entire row. If you only have two items in a four-column grid, auto-fit will expand those two items to take up 50% of the screen each.
Use auto-fill when you want to lock the items to their base size. If you only have two items, auto-fill leaves a noticeable blank space where the third and fourth columns would normally sit.
For standard responsive blog grids or product listings, auto-fit is almost always the right choice.
Fallback Method: Handling Specific Breakpoints
Sometimes a designer hands you a strict layout requirement: exactly 4 columns on desktop, exactly 2 on tablet, and exactly 1 on mobile, the same rigid structure that grid-template-areas is built to handle more explicitly. The fluid nature of minmax() might trigger a 3-column layout at weird in-between widths, which violates strict design rules.
When you need rigid control over the column count, abandon auto-fit and use manual media queries.
.grid-container {
display: grid;
gap: 20px;
/* Default to mobile-first single column */
grid-template-columns: 1fr;
}
/* Tablet: Force exactly 2 columns */
@media (min-width: 600px) {
.grid-container {
grid-template-columns: repeat(2, 1fr);
}
}
/* Desktop: Force exactly 4 columns */
@media (min-width: 1024px) {
.grid-container {
grid-template-columns: repeat(4, 1fr);
}
}Common CSS Grid Layout Mistakes
Even with a layout this simple, a few minor slip-ups can break the entire grid structure. The same fundamentals matter once you move on to trickier patterns like CSS Grid masonry or subgrid.
- Setting explicit widths on grid items: never apply
width: 100%orwidth: 250pxto your.grid-itemclasses. The parent grid container controls the sizing. Setting widths on the children will cause overflows and break theminmax()calculations. - Forgetting box-sizing: if your grid items have heavy padding or borders, they might blow past the available viewport width. Always ensure
box-sizing: border-boxis applied globally in your CSS reset. - Misusing margin for spacing: do not use
margin-rightormargin-bottomon grid items to create space. Always use thegapproperty on the parent container to keep the spacing mathematically perfectly aligned.
Quick answers
Frequently asked questions
How do you make a 4-column CSS grid collapse on mobile screens?
Use grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); on the container element. The browser calculates the available viewport width automatically and wraps items into fewer columns or a single column once they hit the 250px boundary.
What is the difference between auto-fit and auto-fill in CSS Grid?
auto-fit stretches existing grid items to take up the entire available width of the row when there are fewer items than columns. In contrast, auto-fill keeps items at their base size and leaves empty ghost columns at the end of the row.
Can you build a responsive 4-column grid without using media queries?
Yes, combining the repeat(), auto-fit, and minmax() functions removes the need for media queries entirely. The layout calculates column counts dynamically based on the container width rather than arbitrary viewport breakpoints.
Why is repeat(4, 1fr) bad for responsive web design?
The repeat(4, 1fr) property locks the grid into four equal columns regardless of screen size. On mobile viewports, this causes extreme text squishing, broken child elements, and horizontal scrollbars unless you manually override it with media queries.
What does the 1fr unit mean in CSS Grid layouts?
The 1fr unit represents one fractional unit of the remaining space inside the grid container. When paired with minmax(250px, 1fr), it tells the column to scale up freely beyond 250px to absorb any leftover space evenly.
Should you use CSS Grid or Flexbox for a 4-column layout?
CSS Grid is the better choice because it controls two-dimensional layouts along both rows and columns from the parent container. Flexbox works along a single axis, requiring explicit child widths and negative margins to prevent unevenly wrapped trailing cards.
Why are my CSS Grid columns overflowing on small viewports?
This almost always happens because child elements have explicit widths like width: 100% or fixed margins applied. Remove fixed widths on your grid items and apply box-sizing: border-box globally to allow the parent grid rules to handle calculations cleanly.
How do you control the spacing between columns and rows in CSS Grid?
Use the gap property directly on the parent grid container (e.g., gap: 20px;). This applies uniform gutters between both rows and columns without causing margin-collapse bugs or layout shifts on child elements.
Something wrong or out of date?
Tools change fast. Tell the editor what no longer works.