CSS Grid Layout: A Working Developer's Guide

By Krishna Basnet Published Last reviewed 14 min read

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.

Read this if

  • You can already write CSS and want to stop guessing
  • You're building a page shell, gallery or dashboard
  • You've hit an overflow bug and need the cause

Skip this if

  • You need a property reference — use MDN
  • You're targeting Outlook desktop email
  • You want masonry; that's still flagged

Decide whether Grid is the right tool

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.

  1. Do rows and columns both carry meaning in this layout?

    No → it's a single axis. Use Flexbox or normal flow. Stop here.
  2. Should the container decide the track sizes, or the content?

    Content decides → Flexbox, with flex-basis and gap. Container decides → Grid. Keep going.
  3. 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 simple flex-wrap row gets you there with less code.
  4. 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.

The four primitives you actually need

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.

1. Lines are the coordinate system

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.

grid-column: 1 / 3
1 / 3
3
4
1
2
3
.wide { grid-column: 1 / 3; }   /* two tracks, not three */

2. Track sizing is where the control lives

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.

1fr 2fr 1fr
1fr
2fr
1fr
.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: 8px;
}

3. Named areas make the CSS readable

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.

named areas · page shell
header
sidebar
main
footer
.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; }

4. Gap replaces margin on items

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.

gap: 18px
a
b
c
.container { gap: 18px; }          /* row and column */
.container { gap: 18px 30px; }     /* different per axis */

Build-along: one real layout

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.

row dense
span 2
2
3
4
5
.gallery { grid-auto-flow: row dense; }
.tall    { grid-row: span 2; }

The five bugs, and the fix for each

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.

The minmax cliff

repeat(auto-fit, minmax(280px, 1fr)) on a 320px phone. The minimum is fixed, the track can't shrink, the page scrolls sideways.

Fix: wrap the minimum — minmax(min(280px, 100%), 1fr). The minimum now falls back to the container width instead of overflowing it.
The fr trap

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.

Fix: 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.
The silent area drop

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.

Fix: every named area must be a rectangle. Split the L into two names, or pad with . cells. The spec's named areas rules are strict here.
place-items on the wrong element

place-items: center on a page container centred every child — including header text that needed to stay left-aligned.

Fix: put place-items on the specific cell's wrapper, or reset per item with justify-self: start.
dense that reorders the keyboard

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.

Fix: restrict dense to non-interactive galleries, or reorder the DOM to match the packed result and drop 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.

Tested at 320px viewport, Chrome 141 / Safari 26 / Firefox 143, November 2026
DeclarationChromeSafariFirefox
minmax(280px, 1fr)overflowsoverflowsoverflows
minmax(min(280px, 100%), 1fr)fitsfitsfits
1fr + 60-char tokenoverflowsoverflowsoverflows
minmax(0, 1fr) + same tokenfitsfitsfits
L-shaped named areadroppeddroppeddropped

Three habits to ban from a codebase

These are opinions, and they come from maintaining other people's grid CSS after they left.

  1. Numeric placement without names. grid-column: 4 / 7 is meaningless six months later, and it breaks the moment a column is added. Name the lines or use areas.
  2. Grid for a single row of items. If every child sits on the same row and nothing spans, flex with gap is shorter, safer and reads better. Grid there is a costume.
  3. Grid for vertical spacing. Using grid-template-rows with fixed gaps to space paragraphs fights the content. Let normal flow handle prose; reserve Grid for boxes.

Source order is the real accessibility risk

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.

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.

Support, and what this guide leaves out

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:

Questions that come up in review

Why does a bare 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.

Does a broken 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.

Is 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.

Should I use 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 is Grid the wrong choice?

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.

Is grid-gap still valid?

As a legacy alias, yes. Prefer gap, which also works on Flexbox and multi-column.

How this guide was made

So you can judge how much weight to put on it:

Changelog

Sources: W3C CSS Grid Layout Module Level 2 · MDN: CSS grid layout · web.dev: Grid · Can I use: subgrid

Krishna Basnet — student developer from Nepal and founder of Coodeverse. Built the platform in December 2025 after finding that most free programming resources were shallow or paywalled. Writes the entire Coodeverse CSS curriculum.

About · GitHub · X · All CSS lessons