CSS Grid is the most powerful layout system in CSS. Let’s master it once and for all.

The Basics

1
2
3
4
5
6
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 1rem;
}

Grid Template Areas

One of Grid’s most elegant features:

1
2
3
4
5
6
7
8
9
10
11
12
.layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}

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

Responsive Without Media Queries

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

This single line creates a fully responsive grid layout. No media queries needed.

Grid vs Flexbox

  • Grid — Two-dimensional layouts (rows AND columns)
  • Flexbox — One-dimensional layouts (row OR column)

They’re not competitors. Use both — they complement each other perfectly.