Lesson Content
CSS Grid is the most powerful layout system in CSS. Unlike Flexbox (which works in one direction), Grid works in two dimensions — rows and columns simultaneously.
Enabling Grid: Add `display: grid` to a container. Its direct children become grid items.
Defining columns: Use `grid-template-columns` to set column widths. The `fr` unit means "fraction of available space". `repeat(3, 1fr)` creates three equal columns.
The `auto-fill` and `auto-fit` trick: Combine with `minmax()` to create responsive grids that automatically adjust the number of columns based on available space — no media queries needed.
Placing items: By default, items flow into the grid automatically. You can also explicitly place items using `grid-column` and `grid-row` to span multiple cells.
Grid vs Flexbox: Use Grid for page-level layouts and complex two-dimensional arrangements. Use Flexbox for components and one-dimensional alignment. In practice, you'll use both together.
/* Three-column grid */
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 24px;
}
/* Responsive grid (no media queries!) */
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 24px;
}
/* Item spanning multiple columns */
.featured-card {
grid-column: span 2;
}
/* Named areas layout */
.page-layout {
display: grid;
grid-template-areas:
"header header" "sidebar main" "footer footer";
grid-template-columns: 280px 1fr;
gap: 24px;
}Finished this lesson?
Mark it complete to track your progress through the course.
Learning Objectives
- 1Enable CSS Grid with display: grid
- 2Define columns and rows with grid-template-columns
- 3Place items in specific grid cells
- 4Build a responsive card grid
0 of 31 lessons done