CSS Grid for Responsive: minmax, auto-fit, subgrid

CSS Grid for Responsive: minmax, auto-fit, subgrid

Many responsive layouts no longer need media queries. auto-fit plus minmax handles card grids, subgrid aligns nested rows, container queries replace viewport breakpoints inside components.

Core CSS Grid reached Baseline in 2017 (gap in grid layout followed in 2018, and full repeat() support in 2020), and subgrid reached Baseline in 2023. With them, many responsive layouts no longer need media queries. The single pattern grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)) produces a card grid that adapts from one column to many without a single breakpoint. A Holy Grail layout's container rule fits in eight lines of declarations, built around grid-template-areas. Subgrid aligns nested content rows across the cards in each grid row regardless of content length, which used to require display:contents hacks. Pair Grid with size container queries (Baseline 2023) for component-local adaptation inside grid items, and many viewport-width media queries can retire.

Why Grid changed responsive layout

Until CSS Grid, responsive layouts — an approach Ethan Marcotte named in 2010 — were built from one-dimensional tools. Floats, then Flexbox, both work along a single axis — you align items in a row, or you align items in a column. Two-dimensional layouts required nested containers, with the row layout owning the horizontal axis and individual items owning their vertical content. The mental model was always "stack rows of columns" or "stack columns of rows."

CSS Grid is a browser layout system designed for two-dimensional layouts that adapt to the available space. A grid container declares tracks on both axes simultaneously, items can span across rows and columns, and the same declaration that handles desktop-wide layouts can adapt smoothly to narrow viewports without nested containers. The W3C CSS Grid Layout Module Level 2 spec is the authoritative reference, and the MDN CSS Grid Layout reference is the standard practitioner documentation.

The practical shift is that many layouts no longer need media queries. Grid with minmax() and auto-fit adapts to container width automatically; pairing Grid with size container queries (Baseline 2023) handles the rest at component level. A codebase that still ships dozens of viewport media queries may be carrying technical debt from a pre-Grid mental model.

The auto-fit + minmax trick

The single most useful Grid pattern fits in one declaration:

css
.card-grid {
  display: grid;
  gap: 1.5rem;
  grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
}

What that reads as: create as many columns as fit, each at least 15rem wide and growing to fill remaining space evenly. In a 320 CSS pixel container, one column. At 768 pixels, three — exactly three 15rem tracks plus two 1.5rem gaps, so any page padding drops it to two. At 1200 pixels, four. Zero media queries. The browser computes the column count from container width and the minimum track size.

minmax(15rem, 1fr) is the load-bearing piece. The first value (15rem) sets the minimum track width; the second (1fr) lets each track stretch to consume remaining space evenly. Without the 1fr, columns stay at exactly 15rem and the rightmost space sits empty. With it, the layout looks intentional at every width.

The choice between auto-fit and auto-fill matters when there are fewer items than columns that fit. auto-fit collapses empty tracks when there are fewer items than would fit, so two cards on a wide viewport stretch to fill the space. auto-fill keeps the empty tracks reserved, so two cards stay one track wide — the same width they would have in a full row — with empty slots beside them. auto-fit is the right default for card grids; auto-fill is for cases where the empty slot is meaningful (a placeholder for future content, or a layout that should not stretch).

Holy Grail layout with grid-template-areas

The Holy Grail layout — header, footer, main content with two sidebars — used to require either tables (in the 2000s), floats with negative margins (the A List Apart approach from 2006), or Flexbox with three nested containers. In Grid, the container rule holds eight lines of declarations, plus one grid-area line per region:

css
.page {
  display: grid;
  grid-template-columns: minmax(12rem, 15%) 1fr minmax(12rem, 15%);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header  header  header"
    "sidebar main    aside"
    "footer  footer  footer";
  min-block-size: 100svh;
}

.page > header { grid-area: header; }
.page > nav.sidebar { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > aside { grid-area: aside; }
.page > footer { grid-area: footer; }

The grid-template-areas syntax is visually self-documenting: the strings describe the layout the way you would sketch it on a whiteboard. Reordering the layout at a breakpoint means redefining the areas, not changing the DOM:

css
@media (max-width: 48rem) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "aside"
      "footer";
  }
}

The DOM order stays the same — header, sidebar, main, aside, footer — and only the visual order changes by area reassignment. Screen readers and keyboard navigation still follow the DOM order: the CSS Grid specification says the order property and grid placement do not affect ordering in non-visual media (such as speech) or the default traversal order of sequential navigation (such as cycling through links), and that authors must use them only for visual, not logical, reordering — style sheets that reorder content logically this way are non-conforming. In the narrow layout the visual order puts main before the sidebar while keyboard focus reaches the sidebar's links first, so keep the source order logical and check that this mismatch does not confuse keyboard users. Pair the page-level layout with min-block-size: 100svh (the small viewport height unit, Baseline 2022) to handle iOS Safari toolbar resize correctly.

Subgrid: aligned nesting

Subgrid finally arrived at Baseline 2023 across all evergreen browsers. Before subgrid, a card grid with a title row and an action row that needed to align across cards regardless of content length required hacks: fixed heights that broke long titles, display: contents that broke accessibility tree expectations, or JavaScript to measure and equalize.

Subgrid lets a child grid inherit its parent's track definitions. The pattern looks like this:

css
.card-grid {
  display: grid;
  gap: 1.5rem;
  grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));
  grid-auto-rows: auto 1fr auto;
}

.card {
  display: grid;
  /* Inherit row tracks from the parent grid */
  grid-template-rows: subgrid;
  grid-row: span 3;
}

The result is that, in each row of cards, every title row aligns with its neighbors, the body rows stretch to the same height, and the action rows line up. The visual rhythm holds even when one card's title is one line and another's is three.

The W3C CSS Grid Level 2 specification is the authoritative source for subgrid syntax and behavior. Rachel Andrew's Grid by Example collects CSS Grid examples, including a set of subgrid examples. Use subgrid wherever alignment across siblings is part of the design intent; skip it when each card is genuinely independent.

Grid + container queries — the modern stack

The pre-2023 reflex was to write component CSS conditional on viewport width. A card component would have one layout under @media (min-width: 768px) and another below. This breaks the moment the same card appears in multiple slots — sidebar, main column, modal — because the viewport width does not tell you anything about the card's actual width.

Size container queries (Baseline 2023) replace the viewport-width media query with a query on the component's container width:

css
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
}

.card {
  container-type: inline-size;
}

@container (min-width: 25rem) {
  .card .card-body {
    display: grid;
    grid-template-columns: 8rem 1fr;
    align-items: start;
  }
}

The outer Grid handles the page-level responsiveness — how many cards fit, how they wrap. The container query handles the component-level adaptation — when a card is at least 25rem wide, it switches to a two-column body, with the image on the left when it comes first in the card body; narrower than that, it stacks. The same card component works in a sidebar slot and a main-column slot without any viewport-width awareness.

Sprinkling @media (min-width: ...) inside grid items is the legacy pattern. The replacement is @container for the in-component layout and grid-template-columns: repeat(auto-fit, minmax(...)) for the outer grid. Together they can handle many layouts without a viewport-width media query.

Common Grid responsive patterns

A handful of patterns cover most everyday work.

PatternDeclaration
Card grid (most common)`grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr))`
Sidebar + main`grid-template-columns: minmax(12rem, 25%) 1fr`
Holy Grail layout`grid-template-areas` with 3 rows × 3 columns
Magazine layoutnamed `grid-template-areas` with breakpoint redefinitions
Pinterest-style asymmetric`grid-auto-flow: dense` + variable row spans

The sidebar pattern with minmax(12rem, 25%) 1fr produces a sidebar that is at least 12rem wide, grows to 25 percent of container width at wider sizes, and lets the main column take the rest. At narrow viewports, the same pattern wrapped in a container query collapses to a single column.

The Pinterest-style asymmetric layout — cards of varying heights packing efficiently — uses grid-auto-flow: dense so the placement algorithm tries to fill earlier gaps with later, smaller items, combined with grid-row: span N on items that should occupy more vertical space. Dense packing can show items out of source order, so check reading and focus order. The web.dev Learn CSS Grid chapter covers the techniques behind these patterns with interactive demos.

Avoid fixed-pixel widths in grid-template-columns. grid-template-columns: 300px 1fr looks safe and falls apart at 320px viewports where the 300px column leaves only 20px for main content. minmax(...) is almost always the right substitute.

Phase 1 deliverables and acceptance criteria

The migration can be done one component at a time.

  • Pick one component grid in the codebase — cards, dashboard tiles, image gallery — and rewrite it with repeat(auto-fit, minmax(...)). Verify it adapts fluidly without any media queries by resizing the browser developer tools viewport.
  • Find a layout where nested flex containers were used to align rows across siblings (card title rows, action footers). Replace with subgrid. The CSS can get shorter, and the alignment can hold in edge cases that previously broke.
  • Audit viewport-width media queries. Many of those inside grid items can become @container queries — the layout adapts to the slot the component lives in, not the viewport.
  • For any Holy Grail or magazine layout, replace nested containers with grid-template-areas. The named areas make the layout readable straight from the source.
  • Remove fixed-pixel column widths in grid-template-columns wherever possible. Replace with minmax(...) so the layout survives the narrow viewport the original author did not test.

Grid does not replace Flexbox — Flexbox is still the right tool for one-dimensional rows of items. But for two-dimensional adaptive layouts, Grid is the layout model designed for them, and the patterns above can replace much of the responsive plumbing that used to live in media queries.

Project Inquiry