CSS Grid
Two-dimensional layouts with rows, columns, and named areas
Overview
CSS Grid divides a container into rows and columns. Place items explicitly or let auto-placement flow. Grid excels at page layouts, dashboards, and complex responsive designs.
Syntax / Usage
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 1fr auto;
gap: 1.5rem;
}
/* Named areas */
.layout {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 240px 1fr;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
/* Span columns */
.featured {
grid-column: span 2;
}
Examples
Responsive auto-fit cards:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.25rem;
}
Holy grail layout:
.app {
display: grid;
min-height: 100vh;
grid-template-rows: auto 1fr auto;
grid-template-columns: 1fr;
}
Common Mistakes
- Mixing flex and grid without clear responsibility per section
- Fixed pixel columns that break on mobile—use
fr,minmax, or media queries - Overlapping
grid-areanames causing placement bugs - Forgetting
gapreplaces manual margins between cells
See Also
flexbox responsive-design box-model custom-properties