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 |
gridgoes on the parent,#grid. The three<div>s inside go into the slots by themselves, with no class at all.repeat(3, ...)means there are three columns.1frmeans one share of the leftover space, so the three columns get equal shares.minmax(0, 1fr)stops long content from making one column wider than the others.- The number in
grid-cols-3is the number of columns.grid-cols-2is two columns,grid-cols-4four columns. In Tailwind v4 any number works, for examplegrid-cols-7.
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
gap-x-4(column-gap: 1rem) sets only the space between columns, andgap-y-2(row-gap: 0.5rem) only the space between rows.grid-rows-2(grid-template-rows: repeat(2, minmax(0, 1fr))) makes two rows of equal height.col-start-2(grid-column-start: 2) places an item starting at the second column.
🤔 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
- Forgetting
grid.grid-cols-3 gap-4withoutgridis ignored, and A, B, C just sit one below the other. The exercise checksdisplay: gridon#grid. - Wrong class names.
grid-col-3,grid-columns-3andcols-3don't exist. The right one isgrid-cols-3, with an s. The exercise checks for agrid-cols-class followed by a number. - A
styleattribute.style="display: grid"does work, but the exercise wants every style to come from classes. So nostyleattribute may appear at all. - An item that doesn't fit. When an item with
col-span-2reaches the last column, it doesn't fit in that row. So the item moves to the next row and leaves an empty slot behind.grid-flow-denseon the parent fills that slot with a later item, but then the order on screen differs from the order in the HTML.
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.