Tailwind Grid
CSS Grid lays items out in rows and columns at the same time. Tailwind's grid utilities let you build galleries, dashboards and page layouts with a handful of classes.
Defining columns and rows
| Class | CSS |
|---|---|
grid |
display: grid |
grid-cols-3 |
grid-template-columns: repeat(3, minmax(0, 1fr)) |
grid-rows-2 |
grid-template-rows: repeat(2, minmax(0, 1fr)) |
grid-cols-none |
remove the column template |
grid-cols-[200px_1fr] |
any custom template (underscores = spaces) |
gap-4 / gap-x-6 / gap-y-2 |
gutters |
In v4 any number works: grid-cols-7, grid-cols-15 — no configuration needed.
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-4">
<div class="grid grid-cols-4 gap-3">
<div class="rounded-lg bg-indigo-500 p-4 text-center text-white">1</div>
<div class="rounded-lg bg-indigo-500 p-4 text-center text-white">2</div>
<div class="rounded-lg bg-indigo-500 p-4 text-center text-white">3</div>
<div class="rounded-lg bg-indigo-500 p-4 text-center text-white">4</div>
<div class="rounded-lg bg-indigo-500 p-4 text-center text-white">5</div>
<div class="rounded-lg bg-indigo-500 p-4 text-center text-white">6</div>
<div class="rounded-lg bg-indigo-500 p-4 text-center text-white">7</div>
</div>
<p class="mt-3 text-sm text-gray-500">Items flow into the next row automatically. Try grid-cols-3 or grid-cols-5.</p>
</body>
</html>Spanning cells
| Class | Meaning |
|---|---|
col-span-2 |
take up 2 columns |
col-span-full |
span the full row |
row-span-2 |
take up 2 rows |
col-start-2 / col-end-4 |
start / end at grid line 2 / 4 |
row-start-1 |
place on the first row line |
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-4">
<div class="grid h-60 grid-cols-3 grid-rows-3 gap-2 text-sm font-semibold text-white">
<div class="col-span-2 row-span-2 flex items-center justify-center rounded-lg bg-rose-500">col-span-2 row-span-2</div>
<div class="flex items-center justify-center rounded-lg bg-rose-400">A</div>
<div class="flex items-center justify-center rounded-lg bg-rose-400">B</div>
<div class="col-span-full flex items-center justify-center rounded-lg bg-rose-700">col-span-full</div>
</div>
</body>
</html>A page layout with a custom template
Arbitrary templates are ideal for app shells: a fixed sidebar and a flexible content area.
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="h-screen p-2">
<div class="grid h-full grid-cols-[140px_1fr] grid-rows-[auto_1fr_auto] gap-2 text-sm">
<header class="col-span-2 rounded bg-slate-800 p-3 text-white">Header</header>
<aside class="rounded bg-slate-200 p-3">Sidebar<br>140px</aside>
<main class="rounded bg-white p-3 ring-1 ring-slate-200">Main content (1fr)</main>
<footer class="col-span-2 rounded bg-slate-300 p-2 text-center text-xs">Footer</footer>
</div>
</body>
</html>Alignment in a grid
| Class | Aligns |
|---|---|
justify-items-center |
every item horizontally inside its cell |
items-center |
every item vertically inside its cell |
place-items-center |
both at once |
justify-self-end / self-start / place-self-center |
one item |
place-content-center |
the whole grid inside its container |
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-4">
<div class="grid h-48 grid-cols-3 place-items-center gap-2 rounded-xl bg-lime-100">
<span class="text-3xl">🍏</span><span class="text-3xl">🍐</span><span class="text-3xl">🍊</span>
<span class="text-3xl">🍋</span><span class="place-self-end text-3xl">🍉</span><span class="text-3xl">🍇</span>
</div>
</body>
</html>Auto flow and implicit tracks
| Class | Effect |
|---|---|
grid-flow-row / grid-flow-col |
fill rows first (default) / columns first |
grid-flow-dense |
backfill holes left by spanning items |
auto-rows-fr / auto-rows-[100px] |
size of rows created automatically |
grid-cols-subgrid |
inherit the parent grid's columns |
Tip: For a responsive card gallery without any breakpoints, try
grid grid-cols-[repeat(auto-fill,minmax(10rem,1fr))] gap-4— columns appear and disappear as the container resizes.
Test yourself
Which class makes an item span all columns of the grid?
col-span-allcol-span-fullw-fullspan-row
How do you write a grid template of a 200px column followed by a flexible column?
grid-cols-200-1frgrid-cols-[200px_1fr]grid-cols-[200px 1fr]cols-200px-fr
Which single class centres every grid item both horizontally and vertically in its cell?
justify-centercontent-centerplace-items-centerself-center