Grid Utilities — Tailwind CSS

grid = display: grid, on the parent. grid-cols-3 = three equal columns (repeat(3, minmax(0, 1fr))), and Grid makes the rows itself: the 4th item moves to the…

An online shop shows its products three in a row, and the next row lines up exactly below. Flexbox lines items up in one direction only. For a grid like the shop's (columns and rows together), you need Grid, which splits its parent into columns and rows at the same time. In Tailwind, CSS Grid is written as classes too.

It works like a shoe rack with dividers: you decide how many columns there are, and the shoes fill the slots from left to right. Once a row is full, the next shoe goes to the row below. A wide pair of boots may take two slots. In Grid, the rack is the parent, each slot is a place for one item, and the shoes are the items inside the parent.

A three-column grid

This is the same grid as in the exercise:

<div id="grid" class="grid grid-cols-3 gap-4">
  <div>A</div>
  <div>B</div>
  <div>C</div>
</div>
Class CSS it generates Meaning
grid display: grid; Grid is on, A, B and C become grid items
grid-cols-3 grid-template-columns: repeat(3, minmax(0, 1fr)); three equal columns
gap-4 gap: 1rem; 16px between columns and between rows

You get A, B and C side by side in one row, equally wide, 16px apart.

The next row fills itself

You only set the number of columns. Grid makes the rows itself, as many as the items need. Here is an example with the six boxes from the Lab, A to F:

Class Row 1 Row 2
grid-cols-3 A B C D E F
grid-cols-4 A B C D E F
grid-cols-2 A B C D, then row 3: E F

In grid-cols-4, the first row is full with A, B, C, D, so E and F move to the second row. That row holds only two boxes, and the two remaining slots stay empty.

One item spans wider: col-span-2

col-span-2 generates grid-column: span 2 / span 2: the item that has this class takes two columns. This class goes on the item, not on the parent. In this example, A has col-span-2:

<div class="grid grid-cols-3 gap-4">
  <div class="col-span-2">A</div>
  <div>B</div>
  <div>C</div>
  <div>D</div>
  <div>E</div>
  <div>F</div>
</div>

Row 1 holds A (two slots) and B. Row 2 holds C, D, E. Row 3 holds only F. Do you want one item as wide as every column, like a heading? Use col-span-full (grid-column: 1 / -1).

📌 Different gaps per direction, and item positions

🤔 Grid or flex?

Just one direction, like a navbar or a row of buttons? Use flex. Boxes that must line up in columns across every row, like a gallery, a product list or a dashboard? Use grid. In Grid, the boxes in the second row always line up with the columns above them, because the parent decides the column widths.

⚠️ Grids that don't form and cells left empty

TL;DR: grid = display: grid, on the parent. grid-cols-3 = three equal columns (repeat(3, minmax(0, 1fr))), and Grid makes the rows itself: the 4th item moves to the second row. gap-4 = 16px between columns and between rows. col-span-2 goes on one item so that item takes two columns, and col-span-full makes it as wide as every column. Without grid, every grid-cols-* and gap-* class is ignored.

⚡ A three-column grid

Write:

<div id="grid" class="grid grid-cols-3 gap-4">
  <div>A</div>
  <div>B</div>
  <div>C</div>
</div>

Meaning: grid turns Grid on for the parent, grid-cols-3 makes three equal columns, and gap-4 puts 16px between the columns and between the rows.

⚡ The rows fill themselves

Guess first: six boxes, A to F, in grid-cols-4. What's in the second row?

Only E and F. The first row is full with A to D, and the other two places in the second row stay empty.

Meaning: You only set the number of columns. Grid makes the rows itself, as many as the items need.

⚡ One item spreads out

Write:

<div class="grid grid-cols-3 gap-4">
  <div class="col-span-2">A</div>
  <div>B</div>
  <div>C</div>
  <div>D</div>
  <div>E</div>
  <div>F</div>
</div>

Meaning: col-span-2 goes on the item, not the parent, and that item takes two columns. Here A takes the first two columns, and B fills the third. col-span-full makes an item as wide as every column.

What you will learn