Faragiri

Learn Grid CSS

One declaration that replaces a set of breakpoints, the difference between auto-fit and auto-fill on a short row, and placing children by name instead of number.

4Sections
3Min read
4Depth

Recommended Before This

0 / 2
  • CSS
  • Flexbox

One declaration replaces a set of breakpoints.
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) in a
700px container, measured on 2026-09-02, computed to
228px 228px 228px: three columns, chosen by the browser from the
available width and the minimum you named, with a fourth item wrapping to the
next row. Narrow the container and it becomes two columns, then one, with no
media query anywhere.

The two halves each do one job. minmax(220px, 1fr) says never
narrower than 220 and otherwise share the space equally.
auto-fit says fit as many of those as will go. Together they are a
responsive card layout in one line, and it is the single most useful thing grid
does.

auto-fit against auto-fill, and it matters when the row is short

The two are identical until there are fewer items than columns.
auto-fit collapses the empty tracks, so two cards in a three column
grid stretch to fill the row. auto-fill keeps the empty tracks, so
the same two cards stay at one third each and the rest of the row is blank.

700px, four items

Three tracks the container chose, and the item that wrapped

The columns are decided by the container from a minimum width, and the items fall into them in source order with nothing placing them.

  1. Track 1, 228px2
    • firstCard 1
    • wrappedCard 4
  2. Track 2, 228px1
    • Card 2
  3. Track 3, 228px1
    • Card 3

The tracks are ordered and the cards in them are not placed by anything: card 4 is in the first column because it is fourth, not because it was assigned. Naming areas is what turns that around.

Interactive Practice Lab

Edit and run this snippet directly in your browser (zero setup)

CSS
.demo {  display: flex;  flex-wrap: wrap;  justify-content: space-between;  gap: 12px;}.box {  flex: 1 1 80px;  text-align: center;  background: #0ea5e9;}

Live Big-O Complexity & Binary Search Race Lab

Compare complexity curves at scale and watch Linear vs Binary Search race live.

Neither is correct in general. A gallery of cards usually wants
auto-fit, so a partial last row does not look broken; a calendar or
anything aligning to a fixed rhythm wants auto-fill, because the
columns are the point. Choosing the wrong one produces a layout that is right
until the day a filter leaves two results.

Naming the areas, and placing by name

  • grid-template-areas takes strings of names, one per row, so
    the layout is legible in the stylesheet as a picture of itself
  • A child then says grid-area: sidebar and lands there,
    regardless of its position in the markup
  • A dot in the template leaves a cell deliberately empty
  • Rearranging the layout at a breakpoint is a new
    grid-template-areas and no changes to any child

That last property is the argument for areas over line numbers. Placing by
line number means every change renumbers the children; placing by name means the
container owns the layout entirely, which is also what keeps the source order
correct for a screen reader while the visual order changes.

CSS container ownership

A stylesheet map that places children by name

Declaring grid-template-areas on the container draws the page structure in text so each child only names where it belongs.

  1. 01grid-template-areas: 'header header' 'sidebar main' '. footer';
  2. 02grid-area: sidebar;
  3. 03Redefine grid-template-areas on the container only
  4. 04HTML source order stays untouched
  1. 01Container template rowsEach quoted string is a row; a dot leaves a cell deliberately empty
  2. 02Named child placementThe child moves to its named region regardless of its markup position
  3. 03Breakpoint rearrangementNo child selector changes and no line numbers need renumbering
  4. 04Screen reader reading orderVisual regions can shift across breakpoints while logical DOM order stays intact

Line numbers tie every child rule to a track count that breaks when a column is added. Named areas keep the layout map in one declaration.

What to run in 2026

The browser's grid inspector is the tool, free in every browser, and it draws
the track lines with their numbers and names over the page. Grid is the one
layout system where the structure is genuinely invisible without it, because the
tracks are not elements and nothing in the DOM corresponds to them. It replaced
adding temporary borders, which changes the layout being measured.

2026 platform tools

Two built-in mechanisms that replace layout hacks

Because grid tracks have no DOM nodes, debugging and nested card alignment depend on native browser tools rather than extra CSS or measuring scripts.

  • Browser grid inspector
  • CSS subgrid (2026 baseline)
  • Browser grid inspectorDraws track lines, numbers, and area names directly over the page, replacing temporary borders that alter sizing
  • CSS subgrid (2026 baseline)Lets nested child tracks align to parent rows and columns across browsers, replacing fixed heights and JavaScript measuring

Both features ship inside every major browser in 2026 with zero dependencies.

subgrid is the second, free with the platform and available
across browsers as of 2026. It lets a child's tracks align to its parent's, which
is what makes a row of cards line up their headings and buttons even when the
text lengths differ. It replaced fixed heights and JavaScript measuring, both of
which break when the content changes.

Where this stalls people

The stall is reaching for grid where flex was the answer, and the confusion
is genuine because grid can do one dimension too. The distinction that holds:
grid places items into a structure the container defines, and flex distributes
items along a line according to their own content. A navigation bar is flex; a
page layout is grid; a row of cards that must line up their internals is grid.

Practical choice

Whether the container or the items own the sizing

Grid can arrange items along a single row too, so the real dividing line is whether widths come from the container structure or from the items.

What are you catching yourself writing in the stylesheet?
  • Setting widths on children so rows line up
    Switch to CSS GridLet the container define shared tracks
    • Full page layouts
    • Card rows with aligned headings and buttons
  • Defining tracks only to hold one item each
    Switch to FlexboxLet items distribute along one axis by content size
    • Navigation bars
    • Single-axis rows without cross-row alignment

Reaching for grid where flex belongs or forcing flex children into artificial widths is where most layout stylesheets get stuck.

The practical test: if you find yourself setting widths on the children to
make them line up, the container should have been a grid. If you find yourself
defining tracks that exist only to hold one item each, it should have been flex.
The one dimensional case is
laying out along one axis, what the
tracks are made of is
margin, border, padding, and
where widths go wrong
, and doing without breakpoints entirely is
one document, every screen,
inside how a page is laid out and how it
looks
.

Alongside this

Above this