Flexbox handles rows or columns; CSS Grid handles rows and columns at once. Photo galleries, dashboards, pricing tables, and magazine layouts are all natural grid territory. Tailwind CSS turns the entire grid specification into readable utilities, and in this lesson you will learn columns, gaps, spanning, explicit placement, and auto-fit patterns.
Add grid and a column count, then let gap handle the spacing:
<div class="grid grid-cols-3 gap-4">
<div class="rounded bg-teal-200 p-6">1</div>
<div class="rounded bg-teal-300 p-6">2</div>
<div class="rounded bg-teal-400 p-6">3</div>
<div class="rounded bg-teal-500 p-6">4</div>
<div class="rounded bg-teal-600 p-6">5</div>
<div class="rounded bg-teal-700 p-6">6</div>
</div>grid-cols-3 creates three equal-width columns, and items flow into rows automatically. Tailwind supports grid-cols-1 through grid-cols-12, plus grid-rows-* for explicit row counts.
gap-4 spaces both axes; gap-x-6 and gap-y-2 control each axis independently. Unlike margins, gaps never create awkward outer spacing around the grid edges.
Real layouts need featured items that occupy more than one cell:
col-span-2 — item spans two columnsrow-span-2 — item spans two rowscol-span-full — item stretches across every column<div class="grid grid-cols-3 gap-4">
<div class="col-span-2 row-span-2 rounded bg-violet-500 p-6 text-white">Featured</div>
<div class="rounded bg-violet-200 p-6">A</div>
<div class="rounded bg-violet-200 p-6">B</div>
<div class="col-span-3 rounded bg-violet-300 p-6">Full-width footer cell</div>
</div>This five-line snippet is a complete bento-style layout, one of the most popular design patterns of the last few years.
You can pin an item to exact grid lines with col-start-* and col-end-*:
<div class="grid grid-cols-6 gap-2">
<div class="col-start-2 col-end-5 rounded bg-amber-300 p-4">Columns 2 through 4</div>
</div>Grid lines are numbered from 1, and col-end is exclusive, exactly like raw CSS Grid.
Grids shine when combined with breakpoints (fully covered next lesson). The most common card-grid recipe on the web:
<div class="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3">
<!-- cards -->
</div>One column on phones, two on tablets, three on desktops, all from three utilities.
For grids that add columns automatically as space allows, use an arbitrary value:
<div class="grid grid-cols-[repeat(auto-fit,minmax(200px,1fr))] gap-4">
<!-- items -->
</div>Every item is at least 200px wide, and the browser fits as many columns as possible. No media queries needed.
The same alignment vocabulary from flexbox applies: items-center aligns cell content vertically, justify-items-center horizontally, and place-items-center does both at once. On individual items, place-self-center overrides the defaults.
Use flexbox when content should determine sizing (navbars, button rows, tag lists). Use grid when the layout should determine sizing (galleries, dashboards, page scaffolding). They compose beautifully: a grid page with flexbox inside each card is the standard modern architecture.
grid grid-cols-{n} gap-{n} builds an equal-column grid in one line.col-span-* and row-span-* create featured, bento-style cells.col-start-* / col-end-* pin items to exact grid lines.auto-fit,minmax() arbitrary value makes self-responsive grids.Next up: Responsive Design and Breakpoints, where the sm:, md:, and lg: prefixes unlock adaptive layouts.