Reviewed by the Coodeverse team · Tested in Chrome 141, Safari 26 and Firefox 143
The first time Grid cost me real money, the declaration was nine words long
and the bug report said "horizontal scroll on iPhone SE".
It was repeat(auto-fit, minmax(280px, 1fr)). On a 320px screen
with 16px of padding, the track was wider than the viewport, so the page
scrolled sideways and the sticky header detached. Nothing in the CSS looked
wrong. Everything in it was.
Grid is a layout system where the failure modes are silent — no console error, no broken build, just a phone screen sliding where it shouldn't. This guide is organised around that reality: a framework for deciding whether Grid is even the right tool, a build-along of one non-trivial layout, and the five bugs that actually turn up in code review, with fixes.
Most Grid problems are decision problems. Someone reaches for a two-dimensional system to lay out a single row of buttons, adds twelve declarations, and then fights the consequences. Answer these four questions in order. Stop at the first one that gives you an answer.
Do rows and columns both carry meaning in this layout?
No → it's a single axis. Use Flexbox or normal flow. Stop here.Should the container decide the track sizes, or the content?
Content decides → Flexbox, withflex-basis and gap. Container decides → Grid. Keep going.
Do items ever need to occupy the same cell, or span in two directions?
Yes → Grid is the only sane option. No → Grid is still fine, but check whether a simpleflex-wrap row gets you there with less code.
Is the layout order different from the DOM order?
Yes → Grid, and then read section 6 before you ship. No → you're in the happy path.The advice "use both, they're complementary" is usually true and almost always too vague to act on. The concrete split is: Grid owns the page, Flexbox owns the things inside a cell. A nav bar inside a grid header cell is Flexbox work.
Grid has around forty properties. Production code uses four, and everything else is a modifier on one of them. Learn these and you can build anything the specification allows.
A grid container is divided by numbered lines starting at 1 in the
top-left. The space between two lines is a track; where two tracks cross is a
cell. Placement uses a start line and an exclusive end line, so
grid-column: 1 / 3 covers tracks one and two. The specification
defines this in its
line-based placement
section.
.wide { grid-column: 1 / 3; } /* two tracks, not three */
grid-template-columns and grid-template-rows define
the explicit grid. fr is one fraction of the leftover space after
fixed tracks and gaps, so proportions survive resizing — which is exactly why
it also hides overflow problems until a narrow screen finds them.
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
gap: 8px;
}
grid-template-areas draws the layout as text. Every named area
must be rectangular or the whole declaration is dropped without warning —
which is the subject of bug 3.
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 8px;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
gap only exists between tracks, never at the outer edges, so it
never collapses against a parent's padding. Margins on items do, which is why
margin-based spacing needs first- and last-child corrections that
gap simply doesn't.
.container { gap: 18px; } /* row and column */
.container { gap: 18px 30px; } /* different per axis */
The layout is a documentation page: sticky header, sidebar, article column, and a card gallery at the end that must reflow from four columns to one. It is the shape you'll actually be asked for.
Start with areas, because the structure is the part worth naming. Then make the gallery fluid and give the article column a readable measure.
.shell {
display: grid;
grid-template-columns: 240px minmax(0, 1fr);
grid-template-areas:
"header header"
"sidebar article"
"sidebar gallery";
gap: 24px;
}
.shell > .header { grid-area: header; }
.shell > .sidebar { grid-area: sidebar; }
.shell > .article { grid-area: article; }
.gallery {
grid-area: gallery;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr));
gap: 16px;
}
Two deliberate choices. minmax(0, 1fr) on the article column
rather than 1fr, because article bodies contain long code
identifiers that would otherwise widen the track. And min()
around the gallery minimum, because 200px plus padding exceeds a 320px
viewport.
At 320px the gallery becomes one column, the sidebar stacks above the article, and nothing scrolls sideways. No media query was written.
Now the part that is easy to get wrong when the design gets ambitious:
grid-auto-flow: dense to pack uneven cards.
.gallery { grid-auto-flow: row dense; }
.tall { grid-row: span 2; }
Every one of these came from real review, not a contrived example. Each is silent: no error, no warning, a plausible-looking stylesheet, and a broken screen.
repeat(auto-fit, minmax(280px, 1fr)) on a 320px phone. The
minimum is fixed, the track can't shrink, the page scrolls sideways.
minmax(min(280px, 100%), 1fr). The minimum now falls back to the container width instead of overflowing it.
A card grid on repeat(3, 1fr) blew out the viewport when one
card held a long unbroken URL. 1fr is shorthand for
minmax(auto, 1fr), and that auto minimum refuses
to shrink below the item's min-content width.
repeat(3, minmax(0, 1fr)). The minimum is now zero and the track collapses. Add overflow-wrap: anywhere to the card text for good measure.
A grid-template-areas block had an L-shaped
"main" region. The whole declaration was discarded and the
page fell back to auto-placement — with no error anywhere.
. cells. The spec's named areas rules are strict here.
place-items: center on a page container centred every child —
including header text that needed to stay left-aligned.
place-items on the specific cell's wrapper, or reset per item with justify-self: start.
A gallery used grid-auto-flow: dense with focusable card
links. Tab order jumped backwards through the grid because dense packs
later DOM items into earlier holes.
dense.
All five share a shape: a fixed or implicit minimum meeting a container that
can be narrower than it. When you hit an unexplained Grid overflow, check
minimums before anything else — minmax() floors,
auto minimums on fr, and min-content
widths.
| Declaration | Chrome | Safari | Firefox |
|---|---|---|---|
minmax(280px, 1fr) | overflows | overflows | overflows |
minmax(min(280px, 100%), 1fr) | fits | fits | fits |
1fr + 60-char token | overflows | overflows | overflows |
minmax(0, 1fr) + same token | fits | fits | fits |
| L-shaped named area | dropped | dropped | dropped |
These are opinions, and they come from maintaining other people's grid CSS after they left.
grid-column: 4 / 7
is meaningless six months later, and it breaks the moment a column is added.
Name the lines or use areas.
flex with gap is
shorter, safer and reads better. Grid there is a costume.
grid-template-rows
with fixed gaps to space paragraphs fights the content. Let normal flow
handle prose; reserve Grid for boxes.
Grid separates visual position from DOM position, and assistive technology follows the DOM. Three mechanisms break the match, and only one of them announces itself.
grid-auto-flow: dense paints later items in earlier holes.order on grid items reorders paint without touching source.
The rule I apply: if a keyboard user has to traverse it, visual order and DOM
order must agree. If they don't, either reorder the DOM or patch focus with
tabindex. Decorative galleries can diverge; forms and navigation
cannot.
order is the one that surprises people. It reads like a
harmless reordering and it is — for sighted mouse users. Screen reader users
get the original sequence, so the two experiences drift apart.
The core module has been Baseline widely available since 2017. Subgrid reached
Baseline in 2026 at roughly 97% global support. Masonry is the remaining gap
and is still behind a flag; dense packing is the interim answer.
Deliberately not covered here:
1fr track overflow when content is long?1fr is shorthand for minmax(auto, 1fr). The auto minimum won't shrink below the item's min-content width, so a long token widens the track past its container. minmax(0, 1fr) sets the minimum to zero.
grid-template-areas throw an error?No — it fails silently. A non-rectangular area discards the whole declaration and the grid falls back to auto-placement. This is why it passes review and breaks in the browser.
grid-auto-flow: dense safe to use?On non-interactive galleries, yes. On anything with focusable children, only if you've checked tab order afterwards.
auto-fit or auto-fill?auto-fit when a stretched final row looks right, which is most card galleries. auto-fill when item width must stay constant regardless of count — thumbnails, fixed-size tiles.
When only one axis matters, when the content should size the container, or when the layout is a single row of variable-width items. Those are Flexbox or normal flow.
grid-gap still valid?As a legacy alias, yes. Prefer gap, which also works on Flexbox and multi-column.
So you can judge how much weight to put on it:
Sources: W3C CSS Grid Layout Module Level 2 · MDN: CSS grid layout · web.dev: Grid · Can I use: subgrid