The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Create a grid containerdisplay: grid;View examples
Define explicit columnsgrid-template-columns: 16rem 1fr;View examples
Repeat equal columnsgrid-template-columns: repeat(3, 1fr);View examples
Space grid tracksgap: 1rem;View examples
Create responsive columnsgrid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));View examples
Protect flexible tracksgrid-template-columns: minmax(0, 1fr) 20rem;View examples
Size implicit rowsgrid-auto-rows: minmax(8rem, auto);View examples
Place by column linesgrid-column: 1 / 3;View examples
Span multiple columnsgrid-column: span 2;View examples
Place by row linesgrid-row: 1 / -1;View examples
Declare named areasgrid-template-areas: 'header header' 'sidebar main' 'footer footer';View examples
Assign an item to an areagrid-area: main;View examples
Align items verticallyalign-items: center;View examples
Align items horizontallyjustify-items: stretch;View examples
Set both item alignmentsplace-items: center;View examples
Inherit parent columnsgrid-template-columns: subgrid;View examples

CSS Grid controls rows and columns together. Define the tracks the design requires, let auto-placement handle ordinary items, and reserve explicit placement for elements whose position carries layout meaning.

Step by step

Detailed examples

01

Define explicit tracks and gaps

Setting display to grid creates a one-column grid until tracks are defined. Use fixed sizes only where the design requires them, use fr units to distribute remaining space, and use gap instead of item margins for consistent gutters.

Sidebar and flexible content
.layout {
  display: grid;
  grid-template-columns: 16rem 1fr;
  gap: 1rem;
}
Three equal feature columns
.features {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}
Back to quick reference ↑
02

Build grids that adapt to available space

Combine auto-fit with minmax for card grids that add and remove columns automatically. The min(100%, size) pattern prevents a minimum track from overflowing very narrow containers, while minmax(0, 1fr) lets long content shrink inside a flexible track.

Responsive cards without a media query
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
  grid-auto-rows: minmax(8rem, auto);
  gap: 1rem;
}
Flexible content beside a fixed panel
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 20rem;
  gap: 1.5rem;
}

.workspace pre {
  overflow: auto;
}
Back to quick reference ↑
03

Place exceptional items with grid lines

Grid lines number the boundaries between tracks. Explicit placement is useful for headers, featured items, and controlled spans, but ordinary collections are usually simpler when auto-placement determines their cells.

A featured card spanning two columns
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.card--featured {
  grid-column: 1 / 3;
}

.card--wide {
  grid-column: span 2;
}
A sidebar spanning every explicit row
.sidebar {
  grid-column: 1;
  grid-row: 1 / -1;
}
Back to quick reference ↑
04

Describe page regions with named areas

Template areas make large layouts readable by naming rectangular regions. Every row must have the same number of cells, repeated names must form rectangles, and each placed item uses the matching grid-area name.

Named application shell
.page {
  display: grid;
  grid-template-columns: 14rem 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 1rem;
}

.page-header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main-content { grid-area: main; }
.page-footer { grid-area: footer; }
Back to quick reference ↑
05

Align grid items within their areas

Align-items controls the block axis and justify-items controls the inline axis. Place-items sets both in that order. Stretch is often useful for cards, while centering both axes is appropriate for compact icons or empty states.

Stretch cards and center their icons
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: center;
  justify-items: stretch;
  gap: 1rem;
}

.empty-state {
  display: grid;
  min-height: 12rem;
  place-items: center;
}
Back to quick reference ↑
06

Align nested content with subgrid

Subgrid lets a nested grid use tracks defined by its parent, which is useful when card headings, bodies, and actions should align across rows. The nested item must span the parent tracks it intends to inherit.

Cards sharing parent row tracks
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto 1fr auto;
  gap: 1rem;
}

.card {
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;
}

Note: Check the browser support requirements for subgrid when targeting older environments.

Back to quick reference ↑

Local code tester

Experiment with CSS Grid

Change the track definitions, placement, and alignment, then update the sandboxed preview.

Runs in your browser
Preview

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. MDN Web DocsCSS grid layoutdeveloper.mozilla.org
  2. MDN Web DocsBasic concepts of grid layoutdeveloper.mozilla.org
  3. W3CCSS Grid Layout Module Level 2w3.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback