Skip to content

CSS Grid

Master CSS Grid for two-dimensional layouts with rows, columns, and template areas.

What you'll learn

  • What CSS Grid is and how it differs from Flexbox
  • Defining rows and columns with grid-template-rows and grid-template-columns
  • Using grid-template-areas for visual layout naming
  • The fr unit and responsive grid patterns with auto-fit and minmax

Concept

CSS Grid

CSS Grid is a two-dimensional layout system. Where Flexbox excels at arranging items in a single row or column, Grid handles both rows and columns simultaneously. This makes it the right choice for page-level layouts (header, sidebar, main, footer), photo galleries, and any design where you need precise control over both axes. Grid and Flexbox are complementary — use Grid for the page structure and Flexbox for components within it.

Defining the Grid

Apply display: grid to a container, then define its tracks with grid-template-columns and grid-template-rows. The fr (fraction) unit distributes available space — 1fr 2fr gives the second column twice the space of the first:

.layout {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
}

This creates a sidebar (250px) and a main area (fills the rest), with three rows.

Grid Template Areas

The most readable way to define a grid layout is grid-template-areas. You name areas and draw the layout with ASCII-art-like strings:

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

Then assign elements to areas with grid-area: header;, grid-area: main;, etc. The layout reads like a picture of the page — easy to understand and modify.

Responsive Grids with auto-fit

The combination of repeat(), auto-fit, and minmax() creates grids that respond to container size without media queries:

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

This creates as many columns as fit, each at least 250px wide, stretching to fill remaining space. It is the pattern behind responsive card grids and photo galleries that reflow automatically.

Grid vs. Flexbox

Use Grid when you need control over both rows and columns (page layouts, galleries). Use Flexbox when you are laying out items in one direction (navbars, toolbars, card rows). They work together: a Grid cell can contain a Flex container, and vice versa. You do not have to pick one — you use both.

Example

              /* A classic page layout with grid-template-areas */
.page {
  display: grid;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  grid-template-columns: 220px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  gap: 16px;
}

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

/* A responsive photo grid that reflows without media queries */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}
            

The page layout uses named areas for a header, sidebar, main content, and footer. The sidebar is fixed at 220px; main fills the rest. The gallery uses auto-fit with minmax to create responsive columns that reflow automatically as the container changes width.

Try it

  • SVG Optimizer

    SVG icons in a grid layout — optimize them and inspect their sizing.

  • Image Color Picker

    Pick colors for grid cell backgrounds and borders.

Common mistakes

The mistake

Using Grid for a simple row of items where Flexbox would be simpler.

The fix

Grid shines for two-dimensional layouts (rows AND columns). For a single row or column of items, Flexbox is simpler and more appropriate. Use Grid for page structure and galleries; Flexbox for navbars and component rows.

The mistake

Forgetting gap and using margins on grid items, causing uneven spacing.

The fix

Use the gap property on the grid container for spacing between tracks. It handles both row and column gaps cleanly (gap: 16px; or row-gap/column-gap separately) without margin collapse issues.

Related Snippets

Related Cheatsheets

Practice CSS Grid

6 exercises

Practice CSS Grid

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors