Skip to main content
$cat ~/articles/web/tutorial/css-grid-complete-guide.md

CSS Grid: A Complete Guide

Master CSS Grid layout with practical examples — from basic grids to complex responsive layouts.

K
Kartik Gupta
Author
April 10, 2026
Published
1 min read
Read time

Basic Grid

.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}

This creates a 3-column grid with equal columns and 1rem gap.

Auto-fit vs Auto-fill

/* Expands to fill available space */
.grid-auto-fit {
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

/* Maintains fixed sizes even with extra space */
.grid-auto-fill {
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
}

Named Areas

Grid areas make layouts declarative:

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

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

Responsive Grids

With Grid, responsive layouts become trivial:

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

No media queries needed — the grid figures it out automatically.

When to Use Grid vs Flexbox

  • Grid: When you need two-dimensional control (rows AND columns)
  • Flexbox: When layout is essentially one-dimensional

Most layouts benefit from both — use them together.