Skip to content
Velloreth
Web Development 7 min read

CSS Grid: Two-Dimensional Layout Explained

CSS Grid lays out content in rows and columns at once. Learn the track syntax, named areas, auto-fit, and the patterns that replace old hacks.

By

The model

Grid defines rows and columns together, so it controls layout in two dimensions.

.container {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 1rem;
}

The container defines a grid of tracks. Children are placed into cells, either automatically or explicitly.

Defining tracks

Fixed and fractional units

grid-template-columns: 200px 1fr 1fr;

The first column is 200px. The remaining space is split into two equal fractions.

The fr unit is a fraction of the available space — after fixed sizes and gaps are subtracted.

grid-template-columns: 1fr 2fr; /* second column is twice as wide */

repeat()

grid-template-columns: repeat(3, 1fr);      /* three equal columns */
grid-template-columns: repeat(4, 100px);    /* four 100px columns */
grid-template-columns: repeat(2, 1fr 2fr);  /* pattern repeated twice */

minmax()

grid-template-columns: minmax(200px, 1fr) 1fr;

The first column is at least 200px and at most one fraction. This is how you make a column that shrinks to a minimum and then stops.

auto-fit and auto-fill

The responsive grid without media queries.

grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));

The browser fits as many 300px columns as possible, then stretches them to fill the row. At 1000px wide, you get three columns. At 600px, two. At 400px, one.

The difference between the two:

ValueBehaviour
auto-fillKeeps empty tracks, items stay at minimum size
auto-fitCollapses empty tracks, items stretch to fill

auto-fit is usually what you want for a card grid, because the cards stretch to fill the row rather than leaving a gap.

Placing items

Line-based placement

Grid lines are one-indexed. A three-column grid has four vertical lines.

.item {
  grid-column: 1 / 3;  /* from line 1 to line 3, spanning 2 columns */
  grid-row: 2 / 4;     /* from line 2 to line 4, spanning 2 rows */
}

Shorthand with span:

.item {
  grid-column: span 2; /* span two columns from its auto position */
}

Negative line numbers count from the end:

.item {
  grid-column: 1 / -1; /* full width, from first line to last */
}

Named lines

.container {
  grid-template-columns: [start] 1fr [middle] 1fr [end];
}

.item {
  grid-column: start / middle;
}

Named lines make placement readable when the layout is complex.

grid-template-areas

The most readable way to define a page layout.

.layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 1rem;
}

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

The CSS literally draws the layout. A . means an empty cell.

grid-template-areas:
  "header header"
  "sidebar main"
  ". footer";

This is the clearest way to express a page shell, and it is easy to change.

Alignment

justify-items and align-items

Align items within their cells.

justify-items: start | center | end | stretch; /* horizontal */
align-items: start | center | end | stretch;   /* vertical */

justify-content and align-content

Align the whole grid within the container when the tracks do not fill it.

justify-content: center; /* center the grid horizontally */
align-content: center;   /* center the grid vertically */

place-items

Shorthand for both.

.container {
  display: grid;
  place-items: center; /* centers every item in its cell */
}

This is the shortest way to center content in a grid.

Common patterns

Responsive card grid

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}

No media queries. The grid adapts to any width.

Holy grail layout

.page {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header header"
    "nav    main   aside"
    "footer footer footer";
  min-height: 100vh;
  gap: 1rem;
}

The classic three-column layout with header and footer, in eight lines.

Centering

.center {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

Overlapping items

Grid allows items to occupy the same cell, which is how you layer content.

.stack {
  display: grid;
}

.stack > * {
  grid-area: 1 / 1; /* all items in the same cell */
}

Useful for image captions, badges, and overlays.

.layout {
  display: grid;
  grid-template-columns: minmax(150px, 25%) 1fr;
}

The sidebar is at least 150px and at most a quarter of the width.

The gotchas

Implicit tracks

Items placed outside the defined tracks create implicit tracks, which are sized by grid-auto-rows and grid-auto-columns.

.container {
  grid-auto-rows: minmax(100px, auto); /* control implicit row height */
}

Without this, implicit rows size to content, which can produce uneven layouts.

auto-fit versus auto-fill

auto-fill keeps empty tracks, so a row with two cards in a four-track grid leaves two empty columns. auto-fit collapses them, so the cards stretch. Choose deliberately.

minmax with a fixed maximum

/* Cards never grow beyond 400px, leaving space at the end */
grid-template-columns: repeat(auto-fit, minmax(280px, 400px));

If you want cards to stretch, use 1fr as the maximum.

Grid lines are one-indexed

grid-column: 1 / 3 spans two columns, not three. This trips people up coming from zero-indexed languages.

gap is not margin

gap applies between tracks, not around the container. The outer edges have no gap, which is usually what you want.

Subgrid

grid-template-columns: subgrid lets a nested grid align to its parent’s tracks. Support is good in modern browsers but not universal, so check your targets.

Grid versus flexbox

GridFlexbox
DimensionsTwoOne
SizingFrom tracksFrom content
Best forPage layout, galleriesToolbars, card rows
AlignmentBoth axesOne axis

The rule: grid for two dimensions, flexbox for one. They compose — a grid cell can hold a flex container.

Grid and accessibility

Grid changes visual order, and that has consequences for keyboard navigation.

Visual order can diverge from DOM order. With grid-template-areas, you can place an element visually before another while it comes later in the markup. Keyboard focus follows DOM order, so a user tabbing through the page may jump around unexpectedly.

/* The sidebar is visually first but comes second in the DOM */
grid-template-areas:
  "sidebar main"
  "footer  footer";

If the visual order matters for comprehension, reorder the DOM instead of the grid. Screen readers and keyboard users follow the markup, not the layout.

Do not use grid to fake a table. A grid of divs has no row or column semantics, so a screen reader cannot announce “row 2, column 3”. For tabular data, use a real table and style it with grid if needed.

Keep the reading order sensible. A grid that places content in a visually pleasing but logically jumbled order is harder to follow with a screen reader. Test by reading the page with CSS disabled — the DOM order is what assistive technology sees.

Practical guidance

  • Use repeat(auto-fit, minmax(280px, 1fr)) for responsive grids.
  • Use grid-template-areas for page layouts — it is the most readable.
  • Use place-items: center to center content.
  • Set grid-auto-rows to control implicit tracks.
  • Remember lines are one-indexed.
  • Choose auto-fit or auto-fill deliberately.
  • Use grid for structure and flexbox inside cells.
  • Use gap, not margins.

For the related guides, see css-flexbox-complete-guide and responsive-design-breakpoints. For the broader context, css-custom-properties and mobile-first-css.

Quotable summary

CSS Grid defines rows and columns together, so it handles two-dimensional layout that flexbox cannot express in a single container. grid-template-columns with fr units and repeat() is the core syntax, and repeat(auto-fit, minmax(280px, 1fr)) gives a responsive grid with no media queries. grid-template-areas lets you describe a layout visually in CSS, which is the most readable way to build a page shell. Grid and flexbox compose: use grid for the page structure and flexbox inside the cells. The most common gotchas are implicit tracks sized by grid-auto-rows, the difference between auto-fit and auto-fill, and grid lines being one-indexed rather than zero-indexed.

Frequently asked questions

What is the difference between grid and flexbox?
Grid defines rows and columns together, so it controls layout in two dimensions. Flexbox lays out items along one axis. Use grid for page structure and galleries, and flexbox for toolbars and card contents.
What does the fr unit mean?
fr is a fraction of the available space. In grid-template-columns: 1fr 2fr, the second column gets twice the width of the first, after fixed sizes and gaps are subtracted.
What is the difference between auto-fit and auto-fill?
Both create as many tracks as fit. auto-fill keeps empty tracks, so items stay their minimum size. auto-fit collapses empty tracks, so items stretch to fill the row. auto-fit is usually what you want for a responsive card grid.
How do I make a responsive grid without media queries?
Use repeat(auto-fit, minmax(300px, 1fr)). The browser fits as many 300px columns as it can and stretches them to fill the row, so the layout adapts to any width.
What is grid-template-areas?
It lets you name regions of the grid and lay them out visually in CSS, using a string per row. It is the most readable way to define a page layout.
Are grid lines zero-indexed?
No, they are one-indexed. A three-column grid has four vertical lines, numbered 1 to 4. Negative numbers count from the end, so -1 is the last line.