Skip to content

CSS Grid Tutorial: Grid Layout Explained with Examples

CSS grid lays things out in rows and columns at the same time. You describe the columns on the parent, say grid-template-columns: 1fr 1fr 1fr for three equal ones, and the children fill the cells in order, starting a new row each time one fills up. From there you can stretch an item across several cells, name areas like “header” and “sidebar”, or let the browser work out how many columns fit the screen.

A first grid

Six menu items, three columns:

Three equal columns

display: grid makes the parent a grid container, and its direct children become grid items. You only said how many columns there are. The rows appear by themselves: three items fill the first row, the next three go on a second one. Add a seventh item and a third row starts.

Columns with px and fr

Each value in grid-template-columns is one column. Fixed sizes like 120px come first, and then fr (a “fraction”) shares out whatever room is left:

120px 1fr 2fr

The first column is always 120px. The other two split the rest one part to two parts, so the 2fr column is twice as wide as the 1fr one. You can mix in percentages and auto too (auto sizes a column to its content), but fr is the one you’ll use most, because it already leaves room for the gaps. Three columns of 33.33% plus two gaps would be wider than the container.

Rows work the same way with grid-template-rows, for example grid-template-rows: 60px 1fr. Most of the time you leave rows out and let them be as tall as their content.

repeat() and minmax()

Writing 1fr 1fr 1fr 1fr gets old. repeat(4, 1fr) means the same thing. And minmax() gives a column a smallest and a largest size:

  • repeat(4, 1fr): four equal columns.
  • repeat(2, 100px 1fr): the pattern twice, so 100px 1fr 100px 1fr.
  • minmax(200px, 1fr): at least 200px, and it can grow to take a share of the free room.
  • 200px minmax(0, 1fr): a fixed sidebar and a main column that fills the rest.

Put the two together and you get the trick behind most responsive grids, which comes next.

auto-fit vs auto-fill

Instead of a number, repeat() can take auto-fill or auto-fit. Both mean “as many columns as fit”. Here each column is at least 100px, and each grid holds only two items:

auto-fill keeps empty columns, auto-fit doesn’t

Both make as many 100px-or-wider columns as fit the width. The difference shows when there are fewer items than columns. auto-fill keeps the empty columns, so Latte and Mocha stay narrow on the left. auto-fit collapses the empty ones to nothing, and the two items stretch across the whole row. Once there are enough items to fill a row, the two look the same, which is also why both grids match on a phone: only two columns fit there.

gap

gap: 10px puts 10px between rows and between columns, but not around the outside. For different amounts, write two values: gap: 16px 8px is 16px between rows and 8px between columns. You can also set row-gap and column-gap on their own. In older code you may see grid-gap, the first name for the same thing.

Place items with line numbers

The lines between columns are numbered, starting at 1 on the left. Three columns have four lines: 1 before the first column, then 2, 3 and 4 after the last. You place an item by saying which lines it runs between:

A banner across the row and a special across two
  • grid-column: 1 / -1 runs from the first line to the last. Negative numbers count from the end, so -1 is always the last line, however many columns there are.
  • grid-column: span 2 takes two columns starting wherever the item would have gone anyway.
  • grid-column: 2 / 4 would put it in columns 2 and 3. grid-row works the same way for rows.

The other items flow around the placed ones by themselves. Change span 2 to span 3 and the special takes the whole second row.

Name areas with grid-template-areas

For a page layout, line numbers get hard to read. grid-template-areas lets you draw the layout with names, one quoted string per row:

Header, sidebar, main and footer

Each word is one cell. Writing header twice in a row makes the header span both columns. Then each element says which area it goes in with grid-area. Changing the layout is then a matter of moving words: to put the sidebar on the right, swap the words to "main side". A . leaves a cell empty. The areas must be rectangles; an L shape won’t work and the browser ignores the whole grid-template-areas line.

Rows you didn’t ask for

When there are more items than the cells you described, the grid adds rows for them. These extra rows are called implicit, and grid-auto-rows sets their height:

One 50px row, then 30px rows

grid-template-rows: 50px only describes the first row. Items 4 to 7 land in rows the browser added, and each of those is 30px tall. Without grid-auto-rows they’d be as tall as their content. grid-auto-rows: minmax(80px, auto) is a common setting: at least 80px, taller if the content needs it.

Center with place-items

Grid has the shortest way to center something in a box:

place-items: center

place-items: center is short for align-items: center plus justify-items: center, so the paragraph is centered up and down and left to right inside its cell. Here the grid has just the one cell. The box needs some height to center in, here min-height: 140px. See how to center text in HTML for more ways.

A responsive card grid

One line of CSS gives you a grid that shows as many columns as fit, from one on a phone to several on a laptop, with no media queries:

Cards that reflow by themselves

Read repeat(auto-fit, minmax(min(150px, 100%), 1fr)) from the inside out. Every column is at least 150px and grows with 1fr, and auto-fit fits in as many as there’s room for. The min(150px, 100%) part is a safety net: on a screen narrower than 150px, the column shrinks to the container instead of sticking out of it. Unlike a wrapping flex row, the last card here stays the same width as the others.

When 1fr columns aren’t equal

Two 1fr columns should be the same width, but a long word or web address can push one of them wider:

1fr 1fr next to minmax(0, 1fr)

A 1fr column won’t get narrower than its longest word. In the first grid, “cafe.example/menu” can’t break, so its column grows and squeezes the other. The second grid uses minmax(0, 1fr), which lets each column go down to zero, so they stay equal. The address now needs overflow-wrap: anywhere to break onto two lines, or it would poke out of its cell. You’ll hit this with code blocks and long links too.

Grid or flexbox?

They overlap a lot, and both are fine for many layouts. A rough guide:

UseWhenFor example
Flexbox Things line up in one direction, and their own size should decide the layout A nav bar, a row of buttons, a search box and its button
Grid You want rows and columns to line up with each other, or a fixed set of areas A card grid, a photo gallery, a page with header, sidebar and footer

They also mix well. A page can use grid for the main layout and flexbox inside the header for the menu. The CSS flexbox tutorial covers the other half, and the CSS Grid Generator lets you draw a grid with the mouse and copy the CSS.

Questions people ask

What is CSS grid?

CSS grid is a layout mode for rows and columns at the same time. You put display: grid on a parent and describe its columns, for example grid-template-columns: 1fr 1fr 1fr, and its children fill the cells in order. Items can span several cells or go into named areas.

What does fr mean in CSS grid?

fr is a fraction of the free space in the grid. After fixed sizes and gaps are taken out, the rest is shared by the fr columns: in grid-template-columns: 120px 1fr 2fr the first column is 120px and the other two split what is left one part to two parts.

What is the difference between auto-fit and auto-fill?

Both create as many columns as fit, as in repeat(auto-fill, minmax(100px, 1fr)). The difference shows when there are fewer items than columns: auto-fill keeps the empty columns, so the items stay narrow, while auto-fit collapses them and the items stretch across the row. With enough items they look the same.

How do I make a responsive grid without media queries?

Use grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr)). Each column is at least 200px and grows to fill the row, and the browser fits in as many as there is room for, so a phone gets one column and a laptop several. The min(200px, 100%) stops a column from sticking out on very narrow screens.

How do I make a grid item span two columns?

Use grid-column: span 2 to take two columns from wherever the item would go, or give the lines: grid-column: 1 / 3 covers columns 1 and 2. grid-column: 1 / -1 spans the whole row, because -1 is always the last line. grid-row works the same way for rows.

How does grid-template-areas work?

You draw the layout as quoted strings, one per row, with a name for each cell: grid-template-areas: "header header" "side main" "footer footer". Then each element picks its area with grid-area: header. Repeating a name makes that area span those cells, and a dot leaves a cell empty. Each area has to be a rectangle.

Why are my 1fr columns not equal?

A 1fr column won't shrink below its content's smallest width, so a long word, a web address or a code block can make it wider than its neighbors. Use minmax(0, 1fr) instead of 1fr, for example grid-template-columns: repeat(3, minmax(0, 1fr)), and let long text wrap with overflow-wrap: anywhere.

How do I center a div with CSS grid?

Put display: grid and place-items: center on its parent, and give the parent some height: .parent { display: grid; place-items: center; min-height: 300px; }. place-items is short for align-items and justify-items, so the child is centered both ways.

Should I use grid or flexbox?

Use grid when rows and columns should line up with each other, like a card grid, a gallery or a page with header, sidebar and footer. Use flexbox for one row or one column where each item's own size matters, like a nav bar or a row of buttons. They work well together.

Tools that help