CSS Grid
CSS Grid is a two-dimensional layout system that divides a container into rows and columns, letting you place items precisely in any cell or span across multiple cells, making complex page layouts straightforward.
Explanation
CSS Grid excels at two-dimensional layouts: rows and columns simultaneously. Flexbox handles one dimension at a time; Grid handles both. For a dashboard with a header, sidebar, main content area, and footer — Grid is the correct tool. For a horizontal navigation bar — Flexbox. The key Grid concepts: define the container with display: grid, then specify the column and row structure with grid-template-columns and grid-template-rows. The repeat() function and fr (fractional unit) make responsive grids easy: grid-template-columns: repeat(3, 1fr) creates three equal-width columns that share available space. grid-template-columns: 250px 1fr creates a fixed sidebar and a flexible main area. Items are placed in the grid by default (auto-placement fills rows left to right), or explicitly with grid-column and grid-row. Named areas with grid-template-areas provide a visual ASCII-art layout: define the area names in the container, then assign items to areas with grid-area. This is one of CSS's most elegant features for complex page layouts. gap (or column-gap / row-gap) sets spacing between grid cells. minmax(min, max) sets minimum and maximum sizes for tracks. auto-fill and auto-fit with repeat() create responsive grids that automatically adjust the number of columns — the single most common responsive grid pattern.
Code Example
css/* CSS Grid patterns */
/* Classic page layout with named areas */
.page {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr 60px;
grid-template-areas:
"header header"
"sidebar main "
"footer footer";
min-height: 100vh;
gap: 0;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
/* Responsive card grid — no media queries needed */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}
/* Cards automatically wrap: 4 columns on large screens,
2 on medium, 1 on mobile — all from one rule */
/* Item spanning multiple cells */
.featured {
grid-column: 1 / 3; /* span columns 1-2 */
grid-row: 1 / 3; /* span rows 1-2 */
}
Why It Matters for Engineers
CSS Grid is the most powerful layout tool in CSS and the correct solution for any two-dimensional layout. Before Grid, developers used floats, tables, and complex flexbox hacks to build page layouts — all fragile and hard to maintain. Grid makes page layout code as readable and intentional as the design it implements. The auto-fill/minmax pattern for responsive grids is one of the most useful CSS one-liners ever written: it creates a fully responsive grid with no media queries. Knowing this pattern and understanding why it works (fr units + minmax()) saves significant CSS complexity.
Related Terms
Learn This In Practice
Go deeper with the full module on Beyond Vibe Code.
Web Development Fundamentals → →