How Does CSS Grid Work? A Visual Guide
Disclosure: this page contains Amazon affiliate links. As an Amazon Associate we earn from qualifying purchases, at no extra cost to you.
Welcome, layout architect!
CSS Grid explained the short way: you draw the rows and columns first, then drop your content into the slots, like a spreadsheet that finally does what you asked.
Page layouts, photo galleries, dashboards, a pricing table where every card lines up, that’s CSS Grid’s home turf.
If you like a book open next to your editor, the CSS Visual Dictionary paperback on Amazon draws properties the same way we do, but the diagram below plus four words will get you building grids today.
Short answer: CSS Grid is a two-dimensional layout system: you turn a parent into a grid with display: grid, define rows and columns with grid-template-columns and grid-template-rows, and its children drop into the cells automatically. To place an item yourself, point it at numbered grid lines (grid-column: 1 / 3) or a named area.
What the diagram shows, in plain text:
- Grid lines (pink, dashed): 3 columns make 4 column lines, numbered 1, 2, 3, 4 from the left (and -4, -3, -2, -1 counting back from the right). 3 rows make 4 row lines, numbered 1 to 4 from the top.
- A track (orange): column track 2 — the whole column between column lines 2 and 3.
- A cell (blue): the single cell at row 2, column 3, where one row meets one column.
- Areas (green):
headerspans all three columns of row 1,sidebarsits in column 1 of row 2,maincovers columns 2 and 3 of row 2, andfooterspans all three columns of row 3.
What is CSS Grid?
CSS Grid (the CSS Grid Layout Module) is a layout system that arranges a parent’s children in rows and columns at the same time. The W3C CSS Grid Layout Module Level 1, written by the CSS Working Group, opens with this:
This CSS module defines a two-dimensional grid-based layout system, optimized for user interface design. In the grid layout model, the children of a grid container can be positioned into arbitrary slots in a predefined flexible or fixed-size layout grid.
That “two-dimensional” bit is the big difference from flexbox, which lines things up along one axis.
With CSS Grid you draw the whole table of rows and columns first, then drop content into it — and every column lines up from top to bottom, no matter what’s inside.
How do you turn on CSS Grid?
You turn CSS Grid on by giving the parent element display: grid and telling it what columns you want. The parent becomes the grid container; its direct children become grid items and fill the cells in order, left to right, row by row.
<div class="gallery">
<img src="a.jpg" alt=""> <img src="b.jpg" alt=""> <img src="c.jpg" alt="">
<img src="d.jpg" alt=""> <img src="e.jpg" alt=""> <img src="f.jpg" alt="">
</div>
.gallery {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* three equal columns */
gap: 16px; /* 16px between rows and columns */
}
Six images, three columns: CSS Grid makes two rows on its own. As with flexbox, only direct children become grid items, and there’s an inline version too — display: inline-grid — for a grid that sits in a line of text.
What are grid lines, tracks, cells and areas?
CSS Grid has four words, and every grid property uses one of them: lines are the dividers, tracks are the rows and columns between them, a cell is one row meeting one column, and an area is any rectangle of cells.
Learn these four from the diagram and the rest of the grid syntax reads like plain English.
How are CSS grid lines numbered?
CSS grid lines are numbered from 1, starting at the start edge, and there is always one more line than tracks. Google’s web.dev Learn CSS course puts it simply: If your grid has four columns, it will have five column lines including the one after the last column.
The W3C spec adds negative numbers: If a negative integer is given, it instead counts in reverse, starting from the end edge of the explicit grid.
So -1 is always the last line, which makes grid-column: 1 / -1 the go-to way to say stretch across the whole grid
.
What is the difference between a track, a cell and an area?
A grid track is one whole row or one whole column; the W3C spec calls it the space between two adjacent grid lines
. A grid cell is the smallest piece, where one row track crosses one column track.
A grid area is a rectangle of one or more cells bounded by four lines — the header in the diagram is an area three cells wide. Items are placed into areas; tracks are what you size.
How do you define rows and columns?
Columns come from grid-template-columns and rows from grid-template-rows, both set on the grid container: each value you list is one track. Mix units freely — pixels, percentages, auto (fit the content) and the grid-only fr unit.
.layout {
display: grid;
grid-template-columns: 200px 1fr 2fr; /* fixed sidebar, then a 1:2 split */
grid-template-rows: auto 1fr auto; /* header, stretchy middle, footer */
}
You often don’t need grid-template-rows at all: CSS Grid adds rows as items arrive (more on that in the implicit grid section below).
What does 1fr mean in CSS Grid?
1fr means one share of the space left over after fixed tracks and gaps are taken out. The W3C spec defines the fr unit as one that represents a fraction of the leftover space in the grid container
.
So the recipe is: leftover = container width minus fixed tracks minus gaps, then one fr = leftover divided by the total number of fr.
One catch from the spec: on its own, 1fr really means minmax(auto, 1fr), so a column won’t shrink below its longest word or widest image. Write minmax(0, 1fr) if you want columns that stay truly equal.
How wide is each column? Worked examples
Here’s that fr recipe run on five real setups, so you can check your own numbers. (Each assumes the items’ content is narrow enough not to force a column wider.)
| Container width | grid-template-columns | Gap | Math | Column widths |
|---|---|---|---|---|
| 1000px | 200px 1fr 2fr | 20px | 1000 — 200 — 2 gaps of 20 = 760px leftover; 760 / 3 fr = 253.33px per fr | 200px, 253.33px, 506.67px |
| 1000px | repeat(3, 1fr) | 20px | (1000 — 40) / 3 | 320px each |
| 1000px | repeat(3, 1fr) | 0 | 1000 / 3 | 333.33px each |
| 1000px | repeat(auto-fit, minmax(200px, 1fr)) | 16px | 4 columns fit (4 x 200 + 3 x 16 = 848px), 5 don’t (1064px); (1000 — 48) / 4 | 4 columns, 238px each |
| 360px (a phone) | repeat(auto-fit, minmax(200px, 1fr)) | 16px | 2 columns need 416px, more than 360px, so 1 column | 1 column, 360px |
The surprise most people hit is row one: 2fr is twice 1fr, but it’s not twice the fixed 200px column — fr only splits what’s left after the fixed track and the gaps.
What do repeat() and minmax() do?
repeat() saves typing — repeat(4, 1fr) is the same as 1fr 1fr 1fr 1fr — and minmax(min, max) gives a track a size range.
Put them together with auto-fit and you get the most famous line in CSS Grid, a responsive card grid with no media queries:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
}
The browser fits as many 200px-or-wider columns as the row can hold, then stretches them to fill it — 4 columns at 1000px, 1 column at 360px, as the table above shows.
auto-fill works the same way, except, per the W3C spec, auto-fit collapses any empty repeated tracks
after placing items; with only two cards in a wide row, auto-fit stretches the two cards and auto-fill keeps empty columns beside them.
How do you place an item on specific grid lines?
You place a grid item by naming the lines it starts and ends on: grid-column: 2 / 4 means from column line 2 to column line 4
, so the item covers columns 2 and 3.
grid-row works the same way for rows. You can also say how many tracks to cover with span.
.feature { grid-column: 1 / -1; } /* full width: first line to last line */
.wide { grid-column: span 2; } /* two columns wide, wherever it lands */
.tall { grid-row: 2 / 4; } /* rows 2 and 3 */
What is grid-template-areas?
grid-template-areas lets you name grid areas and draw the layout as text: each quoted string is one row, and each word is one cell.
Can I confess something? The line numbers tripped me up for ages until I stopped counting boxes and started counting the dashed lines. Three columns, four lines. Every time. Once that sticks, grid-column: 1 / -1 reads like a sentence.
Then each item says which area it goes into with grid-area. This is exactly the header, sidebar, main and footer layout in the diagram:
.page {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
gap: 16px;
}
.page > header { grid-area: header; }
.page > nav { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
Every named area must be a rectangle — an L-shape is invalid and the whole declaration is ignored. Use a dot (.) for an empty cell.
The best part: to rearrange the page on a phone, you only rewrite the strings inside a media query. Our grid-template-areas guide covers empty cells, the valid and invalid shapes, responsive layouts and the line names each area creates.
What happens when there are more items than cells?
CSS Grid makes new tracks on its own, called the implicit grid. The rows and columns you define are the explicit grid; when items run past them, the browser adds rows (by default, since grid-auto-flow is row).
The W3C spec says grid-auto-rows and grid-auto-columns size these implicit grid tracks
, and both default to auto.
.feed {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 180px; /* every extra row is 180px tall */
}
Add grid-auto-flow: dense and CSS Grid backfills holes left by wide items with smaller ones later in the list — good for galleries, but the visual order may then differ from the HTML order, which matters for keyboard users.
Which CSS Grid properties go on the container, and which on the items?
CSS Grid properties split cleanly: the template and auto properties go on the grid container, the placement properties go on the grid items.
Putting grid-template-columns on a child, or grid-column on the parent, does nothing — one of the most common reasons a grid “won’t work”.
| Property | Set it on | Default (initial value) | What it does |
|---|---|---|---|
grid-template-columns | container | none | sizes the explicit column tracks |
grid-template-rows | container | none | sizes the explicit row tracks |
grid-template-areas | container | none | names areas, drawn as text |
grid-auto-rows / grid-auto-columns | container | auto | sizes implicit (extra) tracks |
grid-auto-flow | container | row | fills row by row (or column, plus dense) |
gap | container | normal (0 in a grid) | space between tracks, never on the outside edges |
grid-column / grid-row | item | auto | start / end line, or span |
grid-area | item | auto | a named area, or all four lines at once |
The alignment properties you know from flexbox also work in CSS Grid: justify-items and align-items on the container move items inside their cells, and justify-self / align-self do it for one item.
place-items: center is the shortest way to center something in a grid cell. (gap and the alignment properties are defined in the CSS Box Alignment module; the grid spec replaced the old grid-gap with plain gap, though browsers still accept the old name.)
Should you use CSS Grid or flexbox?
Use CSS Grid when you need rows and columns to line up together — a page layout, a gallery, a form with labels and fields — and flexbox for a single row or column, like a navbar or a button group.
Google’s web.dev course describes it as a layout method designed for two-dimensional content
.
You don’t have to pick one per site: a common pattern is CSS Grid for the page skeleton and flexbox inside each piece. For the full decision, with a 12-layout cheat sheet, see flexbox vs Grid: which should you use.
Does CSS Grid work in every browser?
Yes — CSS Grid works in every modern browser. The Can I use data for CSS Grid, read on November 11, 2026, shows full support for 97.39% of users worldwide, since Chrome 57, Firefox 52, Safari 10.1 and Edge 16 (all released in 2017).
Only old Internet Explorer and Edge 12-15 had a partial, prefixed version of an earlier draft.
Subgrid, from the W3C CSS Grid Layout Module Level 2, lets a nested grid reuse its parent’s tracks, so cards inside a grid can line up their titles and buttons with each other.
Can I use lists subgrid at 94.68% of users: Firefox 71+, Safari 16+ and Chrome and Edge 117+.
How can you see the grid lines in your browser?
Turn on the grid overlay in your browser’s developer tools: it draws the lines, their numbers and the area names right over the page, just like our diagram.
In Chrome DevTools, click the grid badge next to the element in the Elements panel; the Layout pane lets you show line numbers (on by default), track sizes and area names. Firefox’s Grid Inspector does the same, with line numbers and area names you can switch on.
Now go build the header-sidebar-main-footer page from the areas example, open the grid overlay in DevTools, and watch your own lines light up. There is something deeply satisfying about seeing a layout snap into its slots — enjoy it, you earned it.
Sources and further reading
- W3C: CSS Grid Layout Module Level 1 — the specification (Candidate Recommendation Draft, 26 March 2025): grid lines, tracks, cells, areas, the fr unit, property defaults
- W3C: CSS Grid Layout Module Level 2 — adds subgrid
- MDN Web Docs: Basic concepts of grid layout — grid containers, tracks, lines and the fr unit
- MDN Web Docs: grid-template-areas — named areas and the rectangle rule
- MDN Web Docs: repeat() — auto-fill and auto-fit
- MDN Web Docs: Subgrid — nested grids that share their parent’s tracks
- Can I use: CSS Grid Layout — browser support, 97.39% global full support on November 11, 2026
- Can I use: CSS Subgrid — 94.68%: Chrome/Edge 117+, Firefox 71+, Safari 16+
- web.dev Learn CSS: Grid — Google’s free CSS course module on grid
- Chrome DevTools: inspect CSS grid — the grid badge, overlay and Layout pane
- Firefox DevTools: examine grid layouts — the Grid Inspector
Questions and answers
What is CSS Grid used for?
CSS Grid is used for layouts that need rows and columns to line up together: whole page layouts (header, sidebar, main, footer), image galleries, card grids, dashboards and forms. Inside each piece, flexbox often handles the single-row details.
Is CSS Grid better than flexbox?
Neither is better; they do different jobs. CSS Grid is two-dimensional and controls rows and columns at once, while flexbox is one-dimensional and lines things up along a single row or column. Most sites use both: grid for the page, flexbox inside components.
How many grid lines are in a 3 column grid?
A 3 column grid has 4 column lines, numbered 1 to 4 from the start edge (or -4 to -1 counting back from the end). There is always one more line than tracks, so grid-column: 1 / -1 spans every column.
What is the difference between auto-fit and auto-fill?
Both fit as many repeated columns as the container can hold. The difference shows when there are fewer items than columns: auto-fit collapses the empty columns so the items stretch to fill the row, while auto-fill keeps the empty columns, so the items stay the width they would have in a full row.
Can you use CSS Grid and flexbox together?
Yes. A grid item can itself be a flex container, and a flex item can be a grid container. A common pattern is display: grid on the page or card list and display: flex inside each card for its buttons or header row.