Grid basics
Overview
Flexbox lays items out in one direction. CSS grid works in two: you define rows and columns, and items line up in both. It’s the right tool for page layouts, card grids, galleries, dashboards, forms — anything where things should align across rows and down columns.
Your first grid
<div class="grid">
<div>1</div><div>2</div><div>3</div>
<div>4</div><div>5</div><div>6</div>
</div>.grid {
display: grid;
grid-template-columns: 200px 200px 200px;
gap: 1rem;
}The element with display: grid is the grid container; its direct children are grid items. With three columns defined, the six items fill the first row, then wrap into a second — auto-placed one per cell, in source order. Rows are created automatically, as tall as their content.
Defining tracks
Tracks are the columns and rows of the grid:
grid-template-columns: 200px 1fr 2fr; /* three columns */
grid-template-rows: 80px auto 60px; /* three rows */Track sizes can be lengths (200px, 15rem), percentages, auto (sized by content), the flexible fr unit, and functions like minmax() — covered in the next lesson.
repeat()
grid-template-columns: repeat(3, 1fr); /* 1fr 1fr 1fr */
grid-template-columns: repeat(2, 100px 1fr); /* 100px 1fr 100px 1fr */
grid-template-columns: 15rem repeat(4, 1fr); /* mixed */gap
gap adds space between tracks — never at the outer edges:
gap: 1rem; /* rows and columns */
gap: 2rem 1rem; /* row-gap column-gap */
row-gap: 2rem;
column-gap: 1rem;Gaps are gutters, not cells — you can’t place items in them.
The vocabulary
| Term | Meaning |
|---|---|
| Grid line | the dividing lines between tracks, numbered from 1 (and from −1 at the far end) |
| Track | a single column or row |
| Cell | the intersection of one row and one column |
| Area | any rectangle of one or more cells |
| Gap | the gutter between tracks |
A grid with three columns has four column lines. You’ll place items by line number in Placing grid items.
Spanning cells
Items take one cell each by default. Make one span more:
.featured {
grid-column: span 2; /* two columns wide */
grid-row: span 2; /* two rows tall */
}Auto-placement flows the other items around it, filling cells in order. If an item doesn’t fit in the remaining space on a row, it moves to the next row — sometimes leaving gaps. (grid-auto-flow: dense can backfill them; see Implicit grid and auto-flow.)
Items fill their cells
By default, grid items stretch to fill their cell in both directions (justify-items and align-items both default to stretch). That’s what makes every card in a row the same height. Images are an exception — they keep their aspect ratio — which is why a gallery usually sets width: 100%; height: 100%; object-fit: cover on them.
Grid vs flexbox in one example
/* Flexbox: each row decides its own sizes */
.flex { display: flex; flex-wrap: wrap; gap: 1rem; }
.flex > * { flex: 1 1 200px; }
/* Grid: the container decides the columns; every row lines up */
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }With flexbox, a last row of two items stretches to fill the line, so they’re wider than the items above. With grid, the last row’s items sit in the same columns as everything else. That’s the core difference: flexbox is content-out, grid is layout-in. Flexbox or grid? explores when to use each.
Inspecting grids
Click the grid badge next to the container in DevTools’ Elements panel. The overlay shows track sizes, line numbers, gaps and area names. Keep it on while you work through this chapter — grid is much easier when you can see the lines.
Remember: only direct children of the grid container are grid items. A nested element can’t be placed on the outer grid — unless its parent becomes a grid too (or uses subgrid, in a later lesson).
A photo gallery grid
Turn .gallery into a grid with three equal columns and a 1rem gap between items. Each image should fill its cell and keep a square shape (crop it rather than squash it). Then add a .featured class that makes an item span two columns and two rows, and see how the other items flow around it.
<div class="gallery">
<img class="featured" src="https://picsum.photos/id/10/600/600" alt="Forest" />
<img src="https://picsum.photos/id/11/400/300" alt="Lake" />
<img src="https://picsum.photos/id/12/300/400" alt="Beach" />
<img src="https://picsum.photos/id/13/400/400" alt="Field" />
<img src="https://picsum.photos/id/14/500/300" alt="Coast" />
<img src="https://picsum.photos/id/15/400/500" alt="River" />
</div>
<style>
.gallery { max-width: 48rem; }
/* your code here */
</style><div class="gallery">
<img class="featured" src="https://picsum.photos/id/10/600/600" alt="Forest" />
<img src="https://picsum.photos/id/11/400/300" alt="Lake" />
<img src="https://picsum.photos/id/12/300/400" alt="Beach" />
<img src="https://picsum.photos/id/13/400/400" alt="Field" />
<img src="https://picsum.photos/id/14/500/300" alt="Coast" />
<img src="https://picsum.photos/id/15/400/500" alt="River" />
</div>
<style>
.gallery { max-width: 48rem; }
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.gallery img {
display: block;
width: 100%;
height: 100%;
aspect-ratio: 1;
object-fit: cover; /* crop instead of squashing */
}
.gallery .featured {
grid-column: span 2;
grid-row: span 2;
}
</style>Summary
display: gridmakes a grid container; its direct children become grid items placed into cells.grid-template-columnsandgrid-template-rowsdefine tracks;gapadds space between them.- Grid lines are numbered from 1; tracks are the columns and rows between lines; areas are rectangles of cells.
- Items are auto-placed into the next empty cell, one per cell, unless you place them yourself.
- Grid controls rows and columns together, so items line up in both directions.