Complete Guide

CSS Grid layout: the complete guide

This guide covers every part of CSS Grid layout with visual examples. Includes responsive patterns, 4-language code and a free interactive generator.

What is CSS Grid layout?

CSS Grid is a two-dimensional layout system built into CSS. It lets you define rows, columns and gaps, then place items anywhere within that structure.

Grid handles two dimensions at once: rows and columns. Flexbox handles one dimension at a time. They work well together, so you can use both.

CSS Grid is supported in all modern browsers. No polyfills needed.

Setting up a CSS Grid container

Apply display: grid to any element. All direct children become grid items automatically.

CSS
.container {
  display: grid;
}

One line to activate the grid.

→ grid-template-columns defines how many columns your grid has and how wide each one is. Use fixed widths, fr units for flexible space, or repeat() to avoid writing the same value many times.

Fixed widths
grid-template-columns:
200px 200px 200px;
fr units
grid-template-columns:
1fr 1fr 1fr;
repeat()
grid-template-columns:
repeat(3, 1fr);

→ grid-template-rows defines row heights the same way. Without it, rows size automatically to fit their content.

→ gap sets the spacing between rows and columns in one property.

Placing items in CSS Grid

By default, CSS Grid places items in sequence from left to right, row by row.

You can place any item precisely using grid-column and grid-row with line numbers or span.

By line numbers
grid-column: 1 / 3;
By span
grid-column: span 2;

→ grid-template-areas lets you name regions and assign items to them - the most visual way to define a layout.

CSS
.grid {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-areas:
    'header header header'
    'left main right'
    'footer footer footer';
}
.left   { grid-area: left; }
.main   { grid-area: main; }
.right  { grid-area: right; }

New to CSS Grid? Don’t worry if this looks technical. Start with the basics above or head to our free step by step learning centre and come back to this section later.

Responsive CSS Grid layout

→ repeat(auto-fill, minmax(min, max)) creates responsive card grids without media queries. The browser fits as many columns as will comfortably hold the minimum width, then stretches them to fill the row.

CSS
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));

The responsive card grid pattern.

→ auto-fit collapses empty tracks, while auto-fill preserves them. In most layouts, auto-fill is the safer default.

For more complex breakpoint control, redefine grid-template-columns at each breakpoint.

Common CSS Grid layout patterns

Card Grid

Three-column responsive card grid.

Open in generator →

Page Shell

Header, sidebar, main content, footer. The standard web page shell.

Open in generator →

Dashboard

Top bar, left nav, main content area. Common for app dashboards.

Open in generator →

Magazine

Asymmetric multi-column editorial layout. Mix of wide and narrow areas.

Open in generator →

CSS Grid layout in 4 languages

The same two-column, two-row layout in CSS, Tailwind, JavaScript and SCSS.

CSS
.grid {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr;
  gap: 1rem;
}
Tailwind
<div class="grid grid-cols-[200px_1fr]
  grid-rows-[auto_1fr] gap-4">
JavaScript
const g = document.querySelector('.grid');
g.style.display = 'grid';
g.style.gridTemplateColumns = '200px 1fr';
g.style.gap = '1rem';
SCSS
$sidebar: 200px;
.grid {
  display: grid;
  grid-template-columns: $sidebar 1fr;
  gap: 1rem;
}

Frequently asked questions

What is CSS Grid layout?
CSS Grid layout is a two-dimensional layout system built into CSS, letting you define rows and columns and place items within them.
CSS Grid vs Flexbox - which should I use?
Use Grid for two-dimensional layouts and Flexbox for one-dimensional alignment. Most real projects use both together.
Is CSS Grid supported in all browsers?
Yes, CSS Grid is supported in all modern browsers.
How do I make a responsive CSS Grid layout?
Use repeat(auto-fill, minmax(min, max)) on grid-template-columns to create a responsive layout without media queries.
What are CSS Grid named areas?
Named areas are a way to define a layout by naming regions with grid-template-areas and assigning items to them with grid-area.
Is there a visual CSS Grid layout generator?
Yes, Gridlock Holmes offers a free visual CSS Grid layout generator.
What does fr mean in CSS Grid?
fr stands for fraction - it represents a share of the available space in a grid track.

Build your CSS Grid layout now

Visual generator. Free, no signup. CSS, Tailwind, JS and SCSS.

Open the CSS Grid Generator Start the Learning Path CSS Grid Cheat Sheet