CSS Grid & Mobile-First: Layouts That Don't Collapse on Phones
Build two-dimensional card grids with CSS Grid, understand fr units and minmax(), and make layouts adapt cleanly from 375px phones to wide monitors.
You'll learn how CSS Grid arranges items into rows and columns at the same time, how to build a responsive card grid in one line of CSS, and how mobile-first media queries keep phone layouts from breaking. Assumes basic HTML and CSS familiarity.
1. Flexbox is a line; Grid is a spreadsheet
Flexbox is great when you want items in a single row (like a navigation bar) or a single column. The moment you try to build a 3-column gallery of cards where every row lines up neatly, Flexbox starts fighting you—especially on the last row when there are only two cards left.
CSS Grid thinks in two dimensions at once: rows and columns, like a spreadsheet. You put display: grid on the parent container, tell it how many columns you want, and the browser slots your cards into the cells automatically.
.gallery {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
}1fr stands for one fraction of the available space. Writing 1fr 1fr 1fr means: "Take whatever width .gallery has, subtract the 20px gaps, and split what's left into three equal columns." No percentage math, no 33.3333% rounding bugs.
2. The one-line responsive card grid
Three fixed columns (1fr 1fr 1fr) look great on a laptop and terrible on a phone, where each column squishes to 100 pixels wide and every word wraps onto its own line.
Instead of hardcoding 3 columns, let CSS Grid count how many fit:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
gap: 20px;
}That looks intimidating, so read it from the inside out:
min(100%, 260px)— "Be at least260pxwide, unless the whole screen is narrower than260px, in which case use100%."minmax(..., 1fr)— "Each card is between260pxand1fr(an equal share of the row)."repeat(auto-fill, ...)— "Fit as many of those columns on a row as you can, and wrap the rest onto new rows."
With one line of CSS, you get 4 columns on a wide monitor, 2 on a tablet, and 1 on a phone—zero media queries required.
Put 5 <div class="card">Card</div> items inside <div class="gallery"> and resize your browser window from wide to narrow. Watch the columns collapse cleanly from 3 or 4 down to 1.
3. The viewport tag phones secretly require
If you open your page on a real phone without this tag inside <head>, the phone assumes your site was built in 2006, pretends the screen is 980px wide, and zooms everything out to microscopic ant-text:
<meta name="viewport" content="width=device-width, initial-scale=1" />Always include it. It tells the phone: "A 375px phone is 375px wide. Stop pretending."
4. Mobile-first media queries
When you do want a completely different layout on desktop—like a sidebar next to your main article—start with the phone layout as your default, and layer the desktop layout on top with @media (min-width: ...):
.page-layout {
display: grid;
grid-template-columns: 1fr;
gap: 32px;
}
@media (min-width: 768px) {
.page-layout {
grid-template-columns: 2fr 1fr;
}
}On screens narrower than 768px, .page-layout is one column (main content on top, sidebar underneath). At 768px and wider, it becomes two columns (2fr main, 1fr sidebar).
Also add min-width: 0 on grid children that contain long code blocks or URLs—by default, grid tracks refuse to shrink smaller than their longest unbroken word, which is how one long URL pushes an entire mobile page sideways.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Missing <meta name="viewport"> | Mobile browsers zoom out to 980px | Add the viewport meta tag in <head> |
Fixed width: 600px on a card | Causes horizontal scrollbars on phones | Use width: 100%; max-width: 600px; |
Long <pre> or URL stretches a grid column | Grid overflows the viewport | Add min-width: 0 on the grid child |
Used minmax(320px, 1fr) without min() | Breaks screens narrower than 320px | Use minmax(min(100%, 320px), 1fr) |
Practice
- In
grid-template-columns: 200px 1fr 2fr, how wide are the second and third columns if the container is840pxwide withgap: 20px? - Why is mobile-first (
min-width) easier to maintain than desktop-first (max-width)?
Answers:
- Two gaps take
40px, the fixed column takes200px, leaving600pxsplit into 3 fractions (1fr + 2fr): column 2 gets200px, column 3 gets400px. - Mobile is almost always a single vertical stack. Starting simple and adding columns for larger screens means you write overrides only where things get more complex, instead of undoing complex desktop rules on small screens.
What you learned
- CSS Grid arranges rows and columns simultaneously using
frfractions andgap. repeat(auto-fill, minmax(min(100%, 260px), 1fr))builds a responsive card grid in one line.- Always include
<meta name="viewport" content="width=device-width, initial-scale=1" />and usemin-widthmedia queries for mobile-first layouts.