CSS Grid
CSS Grid is a two-dimensional layout system: it controls rows and columns at the same time. It's the best tool for whole page layouts, photo galleries and dashboards, and it often needs surprisingly little code.
Creating a grid
Set display: grid on a container, then define the columns (and optionally rows). Direct children become grid items and fill the cells in order.
.gallery {
display: grid;
grid-template-columns: 200px 200px 200px;
gap: 1rem;
}
This makes three 200px columns with 1rem gaps. Extra items automatically start a new row; rows that you haven't defined are created as needed and sized to their content.
The fr unit and repeat()
Fixed widths don't adapt to the screen. The fr unit means "a fraction of the free space", and repeat() saves typing.
.layout {
display: grid;
grid-template-columns: repeat(3, 1fr); /* three equal columns */
}
.sidebar-layout {
display: grid;
grid-template-columns: 250px 1fr; /* fixed sidebar, flexible main */
}
.thirds {
display: grid;
grid-template-columns: 2fr 1fr; /* main is twice the side */
}
minmax(min, max) sets a size range for a track. grid-template-rows: auto 1fr auto is a common page pattern: a header and footer sized to their content and a main area that takes the rest.
Lines, tracks, cells and areas
Grid has its own vocabulary, and it matters because you place items by referring to it:
- Grid lines: the dividing lines, numbered from 1. Three columns have four column lines. Negative numbers count from the end, so
-1is always the last line. - Track: a single row or column (the space between two adjacent lines).
- Cell: where one row and one column cross.
- Area: a rectangle of one or more cells.
- Gap: the gutter between tracks.
repeat(3, 1fr) with two rows creates column lines 1 to 4 and row lines 1 to 3 (negative numbers count back from the end). The yellow item runs from column line 1 to line 3, forming a two-cell area.Placing items
Items fill cells automatically, but you can place them by line number:
.featured {
grid-column: 1 / 3; /* from line 1 to line 3: two columns */
}
.banner {
grid-column: 1 / -1; /* first line to last line: full width */
}
.tall {
grid-row: span 2; /* cover two rows from wherever it lands */
}
Named areas
grid-template-areas lets you draw the layout with words. Each string is a row; repeat a name to make an area span several cells, and use a dot for an empty cell.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.page header { grid-area: header; }
.page aside { grid-area: sidebar; }
.page main { grid-area: main; }
.page footer { grid-area: footer; }
Every area must be a rectangle, and every row string must have the same number of cells.
Responsive grids without media queries
This one line is worth memorising:
.products {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
It says: fit in as many columns as possible, each at least 200px wide, then stretch them to share any leftover space. On a phone you get one column, on a tablet two or three, on a laptop four or more. (auto-fill is similar but keeps empty tracks when there are few items, so the items don't stretch.)
Grid or Flexbox? Use Grid when you're designing the layout in two dimensions (rows and columns that line up). Use Flexbox when content flows in one direction, such as a nav bar or a row of buttons. They work well together: a grid of cards whose insides use Flexbox. For nested grids that line up with their parent's tracks, look up subgrid, now supported in all major browsers.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid</title>
<style>
body { font-family: system-ui, sans-serif; margin: 0; }
.page {
display: grid;
grid-template-columns: 160px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 0.5rem;
min-height: 100vh;
}
header, aside, main, footer { padding: 1rem; border-radius: 6px; }
header { grid-area: header; background: #1e3a8a; color: white; }
aside { grid-area: sidebar; background: #fde68a; }
main { grid-area: main; background: #f1f5f9; }
footer { grid-area: footer; background: #334155; color: white; }
.tiles {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
gap: 0.5rem;
}
.tiles div { background: #bfdbfe; padding: 1.5rem 0.5rem; text-align: center; }
</style>
</head>
<body>
<div class="page">
<header>County Dashboard</header>
<aside>Menu</aside>
<main>
<div class="tiles">
<div>Clinics</div><div>Schools</div><div>Roads</div><div>Water</div><div>Markets</div>
</div>
</main>
<footer>Updated weekly</footer>
</div>
</body>
</html>
A full-height page appears with a dark blue header across the top, a yellow 160px menu on the left, a light grey main area and a slate footer across the bottom. Inside the main area, five light blue tiles arrange themselves into as many columns as fit; drag the preview narrower and they reflow into fewer columns.
Key points
display: gridplusgrid-template-columnsdefines a two-dimensional layout.frshares free space;repeat()andminmax()keep definitions short and flexible.- Place items by line numbers (
grid-column: 1 / -1) or by named areas. repeat(auto-fit, minmax(200px, 1fr))makes a responsive grid with no media queries.
Tip: use the ← and → keys to move between pages.
