Web Services

CSS Grid Layout in Practice: From Two Columns to Fully Responsive

The hard part of CSS Grid is not syntax but how fr units distribute space, when auto and minmax are resolved, and how the implicit grid is filled. Starting from the coordinate system of a grid container, this guide explains the precise meaning of fr remainder distribution, why 1fr can overflow, the difference between minmax and auto-fill versus auto-fit, how grid-auto-flow changes placement, and closes with a copy-ready responsive recipe, canonical centering solutions, and a rule for choosing between Grid and Flexbox.

By LaoHand Team·8 min read·Updated 2026-10-02

Understand the coordinate system: tracks, gutters, and content boxes

The first principle of Grid is that the container is divided into row tracks and column tracks, and children are placed into grid areas formed by the intersections of row and column lines. All sizing is computed on tracks, not on the children themselves. The counter-intuitive part is that 1fr describes how wide the track is, not how wide the child is; the child ends up as the track width minus its own padding, border, and margin.

Tracks can be separated by a gutter, defined with gap. The gutter is not part of any track — it is empty space between them and is never occupied by content. That is why gap is preferable to per-item margins: margin-based spacing also appears at the start and end of the container unless you cancel it with negative margins, while gap has no such artifact and produces nothing at all when only one child is present.

Track sizes have exactly two sources: explicit definitions through grid-template-columns and grid-template-rows, and implicit definitions created by grid-auto-rows and grid-auto-columns for anything beyond the explicit tracks. Explicit tracks come first and implicit ones are appended after, and that explicit-then-implicit ordering determines where the auto-placement algorithm puts surplus items.

Two more concepts must be kept distinct: grid lines and grid areas. Column lines are numbered starting at 1, and n lines define n-1 column tracks. That is why grid-column: 1 / 3 means "from the first line to the third line", spanning tracks 1 and 2 — the numbers refer to lines, not to cells. Getting this off-by-one wrong is the single most common Grid mistake and is worth double-checking every time.

.layout {
  display: grid;
  /* Explicit column tracks + track gaps */
  grid-template-columns: 240px 1fr;
  gap: 24px;
  /* Implicit row tracks: undefined rows are generated at this size */
  grid-auto-rows: minmax(120px, auto);
}

/* Grid lines are numbered from 1; spanning two tracks is written 1 / 3 */
.sidebar {
  grid-column: 1;
  grid-row: 1 / 4; /* Spans rows 1, 2, and 3 */
}
.content {
  grid-column: 2;
}

/* You can also use named areas, declared first and used later */
.layout {
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
  grid-template-columns: 240px 1fr;
}
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }

How fr is actually distributed, and why layouts overflow

fr expresses a proportion of the remaining space, not the width of an element. The resolution order is: every non-fr track (fixed lengths, percentages, auto, the fixed part of minmax) claims its space first, and whatever is left of the container is then divided in proportion to the fr values. When all tracks are fr, the base is the container width divided by the sum of the coefficients.

The direct consequence is that 1fr does not mean "half". In grid-template-columns: 1fr 1fr the columns are indeed equal, but in 1fr 200px the 1fr track receives whatever remains after 200px and the gap are taken. At the same time, the automatic minimum size — min-width: auto — gives an fr track a content-derived minimum. When intrinsic content is wider than the proportionally computed track, the track simply refuses to shrink. This is the real cause of most unexplained Grid horizontal overflow.

There are two fixes. Either give the track an explicit floor with minmax(0, 1fr), telling the browser that minimum sizing must not be driven by content; or set min-width: 0 (or overflow: hidden) on the child to override the min-width: auto default. In practice, writing minmax(0, 1fr) instead of a bare 1fr is a cheap habit that eliminates an entire class of overflow bugs.

The second trap is mixing fr with auto. In grid-template-columns: auto 1fr, the auto track sizes to the largest intrinsic width of its contents and 1fr takes the rest. That looks natural, but when the content width is unbounded — a long URL, an unbreakable token — the auto column consumes everything and the 1fr column is squeezed to nearly zero. The predictable approach is to bound the auto track too, as in minmax(0, auto) 1fr, or to compose an explicit bound from fit-content() and clamp().

.app {
  display: grid;
  /* The auto column sets an upper bound, keeping long content from squeezing out the 1fr */
  grid-template-columns: minmax(0, auto) minmax(0, 1fr);
  gap: 16px;
}

/* Safe pattern: write every 1fr as minmax(0, 1fr) */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

/* Classic overflow case: Grid items default to min-width: auto, so long words burst the container */
.grid-item {
  min-width: 0;      /* Key: release the content-driven minimum width */
  overflow-wrap: anywhere;
}

/* The same pitfall and fix in classic Flex (for comparison) */
.flex-item {
  min-width: 0;      /* Flex items also need this manual reset */
}
.flex-item img {
  max-width: 100%;
  height: auto;
}

minmax, auto-fill, and auto-fit: the responsive trio

minmax(min, max) defines a track as a range between a floor and a ceiling. In minmax(0, 1fr) the zero floor overrides the automatic minimum size while the 1fr ceiling participates in the remainder distribution. minmax(200px, 1fr) expresses both "never narrower than 200px" and "absorb as much of the rest as possible". Use minmax(200px, 400px) when you need a hard cap, and minmax(min-content, 1fr) when the track should grow with its content before it shrinks.

repeat(auto-fill, <pattern>) counts how many copies of the pattern fit the container width and then fills the row with that many explicit tracks, even if some end up empty. auto-fit differs by collapsing those empty tracks and letting the populated ones expand to fill the container. Visually, when the number of items is below the row capacity, auto-fill leaves a gap while auto-fit stretches the items.

One counter-intuitive but important detail: the difference is most visible when the item count is less than or equal to the container capacity, and the two behave almost identically once items must wrap. A second trap is that when the container has zero width, or when items are display: none, auto-fit can compute zero tracks and stack everything on top of everything else. In collapsible sidebars and modals, always give the container a non-zero minimum width or switch to explicit column counts via media queries.

The most dependable combination in practice is to let media queries own breakpoints and let Grid own column arithmetic: a single column on narrow screens, and something like repeat(auto-fill, minmax(280px, 1fr)) on desktop. But when the design specifies exact column counts per breakpoint, write them out explicitly instead. Design-derived column widths are rarely equal divisions, so auto-fill equal division will simply not match the mockup.

/* (1) Fluid card grid: wraps columns automatically, no media queries needed */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

/* (2) Same pattern but stretches when there are few items: auto-fit collapses empty tracks */
.toolbar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}

/* (3) Explicit breakpoints: more reliable when the design specifies column counts */
.page-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}
@media (min-width: 768px)  { .page-grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1200px) { .page-grid { grid-template-columns: repeat(3, 1fr); } }

/* (4) Sidebar + main content, minmax(0, 1fr) prevents content overflow */
.shell {
  display: grid;
  grid-template-columns: 0 minmax(0, 1fr);
  transition: grid-template-columns 200ms ease;
}
.shell.is-open { grid-template-columns: 240px minmax(0, 1fr); }
@media (prefers-reduced-motion: reduce) {
  .shell { transition: none; }
}

Placement: grid-auto-flow and explicit item positioning

The default auto-placement strategy is grid-auto-flow: row — fill each row left to right, then wrap. Setting column fills top to bottom before moving to the next column. Either works for a feed, but the rhythm differs: row flow suits horizontally scanned lists, column flow suits vertically scrolling boards and column-oriented categories.

grid-auto-flow: dense makes later items backfill holes left by taller earlier ones. For a masonry gallery of uneven cards it noticeably reduces ragged gaps at the end of rows, at the cost of breaking the correspondence between visual order and DOM order — and Tab order follows suit. Use dense only for decorative tiles where sequence is irrelevant, and never on anything interactive.

Explicit placement uses grid-row-start, grid-row-end, grid-column-start, and grid-column-end, with the grid-row and grid-column shorthands. Values can be line numbers or span N. Negative numbers count backwards from the end of the explicit grid, which is the convenient way to build sticky headers and footers because you never need to know the total row count in advance.

Finally, grid-template-areas draws the layout with strings. It looks closest to the mockup and is the most readable to debug, at the price of strict column-count alignment per row (a single missing space is a parse error) and less flexibility when columns are generated in a loop. The practical rule: fixed layouts use areas, dynamic or variable-column regions use line numbers with span.

/* (1) Kanban: column-first + horizontal scroll */
.board {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 280px;
  grid-template-rows: repeat(3, auto);
  gap: 16px;
  overflow-x: auto;
}

/* (2) Masonry: row-first + dense backfill of gaps (decorative tiles only) */
.masonry {
  display: grid;
  grid-auto-flow: row dense;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

/* (3) Negative grid lines: pin the first and last without knowing the total row count */
.report {
  display: grid;
  grid-template-rows: auto 1fr auto;
}
.report > .title { grid-row: 1; }
.report > .body  { grid-row: 2; }
.report > .footer { grid-row: -1; }   /* First from the end of the explicit grid */

/* (4) Named areas: the layout reads at a glance */
.page {
  display: grid;
  grid-template-areas:
    "nav    nav"
    "side   main"
    "side   foot";
  grid-template-columns: 200px minmax(0, 1fr);
  grid-template-rows: 56px 1fr auto;
  min-height: 100vh;
}
.nav  { grid-area: nav; }
.side { grid-area: side; }
.main { grid-area: main; }
.foot { grid-area: foot; }

Centering recipes and when to choose Grid over Flexbox

The cleanest centering in the Grid era is to occupy one cell and center within it: display: grid with place-items: center centers horizontally and vertically in one declaration. It replaces the long-standing position: absolute plus top/left 50% plus translate(-50%, -50%) sequence, but without leaving the document flow, without requiring position: relative on the parent, and while still filling the available space.

When you need equal space on all sides, use place-content: center, which centers the entire track group; place-items: center centers each item within its own cell. Conflating these is the most common semantic mistake in Grid: to center content while letting the background fill the container, use place-items; to shrink the whole grid into a centered cluster, use place-content.

The division of labor between Grid and Flexbox has a simple test: Grid is two-dimensional and describes row and column structure, while Flexbox is one-dimensional and handles alignment and distribution along a single axis. In practice, if you are writing justify-content, the item in question is almost certainly in a Flexbox row. If you are writing grid-template-columns, it is almost certainly a Grid layout. Mixing the two for the same row is where most layout bugs come from.

The selection rule reduces to one sentence: use Grid when you are describing "how many rows and columns, and how big each cell is"; use Flexbox when you are describing "how this run of items is distributed along the main axis". The most common real combination is Grid on the outside to carve the page into regions — sidebar, main, footer — with Flexbox inside each region for button groups, tag rows, and forms.

/* (1) General centering: one line with Grid, without leaving the document flow */
.center-box {
  display: grid;
  place-items: center;      /* Center the grid item horizontally and vertically within its cell */
  min-height: 320px;
}

/* (2) Center the whole grid as a small cluster: use place-content, not place-items */
.login-page {
  display: grid;
  place-content: center;    /* Center the track group as a whole */
  grid-template-columns: 320px;
  min-height: 100vh;
}

/* (3) The classic split: outer Grid divides regions, inner Flex lays out one direction */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.toolbar__spacer { margin-left: auto; }  /* Push the right-side actions to the far right */

/* (4) Mobile-first two-column layout, minimum card width 260px */
.split {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 20px;
}

/* (5) Respect the reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.