CSS Grid in Tailwind CSS
Flexbox arranges items along a single line — Grid arranges them across rows and columns at the same time. Whenever you're building a photo gallery, a dashboard of stat cards, or a pricing table with perfectly aligned columns, Tailwind's Grid utilities give you that two-dimensional control with none of the manual math old-school float or table layouts required. In this chapter you'll define grid tracks, build layouts that reflow responsively, and learn the auto-placement rules that fill in the gaps for you.
What is CSS Grid in Tailwind?
Grid is a two-dimensional layout model — it lets you define both columns and rows at once, and place items into that structure. Flexbox is brilliant for aligning items along one line; Grid is what you reach for the moment you need things to line up in both directions, like cells in a spreadsheet.
Add grid or
inline-grid to an element to
make it a grid container, then use
grid-cols-* to slice it into
equal-width columns.
<!-- 3 equal columns, with a gap between every cell -->
<div class="grid grid-cols-3 gap-4">
<div class="bg-cyan-500 rounded-lg p-6">1</div>
<div class="bg-purple-500 rounded-lg p-6">2</div>
<div class="bg-orange-500 rounded-lg p-6">3</div>
<div class="bg-green-500 rounded-lg p-6">4</div>
<div class="bg-pink-500 rounded-lg p-6">5</div>
<div class="bg-blue-500 rounded-lg p-6">6</div>
</div>
Notice the 6 items automatically wrapped into 2 rows of 3 — Grid handled that placement itself. You never told it there'd be two rows; it figured that out from the number of items and the column count you gave it.
display: grid — a
block-level, two-dimensional grid container.
display: inline-grid
— an inline-level grid that only takes the width its content
needs.
grid-cols-3 splits the width into three equal
columns.
col-span-* and
row-span-* — precise item
placement like this is covered in full in the next chapter,
Grid Placement.
Defining Columns & Rows
Tailwind ships
grid-cols-1 through
grid-cols-12 out of the box,
each creating that many equal-width columns —
matching the same 12-column convention most design systems use.
| Class | CSS Equivalent | Result |
|---|---|---|
| grid-cols-1 | grid-template-columns: repeat(1, minmax(0, 1fr)) | Single column — items stack vertically |
| grid-cols-2 | grid-template-columns: repeat(2, minmax(0, 1fr)) | Two equal-width columns |
| grid-cols-3 | grid-template-columns: repeat(3, minmax(0, 1fr)) | Three equal-width columns |
| grid-cols-4 | grid-template-columns: repeat(4, minmax(0, 1fr)) | Four equal-width columns |
| grid-cols-12 | grid-template-columns: repeat(12, minmax(0, 1fr)) | Classic 12-column design-system grid |
| grid-cols-none | grid-template-columns: none | No explicit column tracks defined |
grid-rows-* works identically
for rows, but you'll use it far less — most grids only need column
tracks defined, and let rows size themselves automatically based
on content.
<!-- 4-column stat card row -->
<div class="grid grid-cols-4 gap-4">
<div class="bg-white p-4 rounded-lg shadow-sm">Revenue</div>
<div class="bg-white p-4 rounded-lg shadow-sm">Users</div>
<div class="bg-white p-4 rounded-lg shadow-sm">Orders</div>
<div class="bg-white p-4 rounded-lg shadow-sm">Growth</div>
</div>
<!-- Fixed 2-row layout: header row, content row -->
<div class="grid grid-rows-[auto_1fr] h-screen">
<header class="bg-gray-900 text-white p-4">Header</header>
<main class="p-4 overflow-y-auto">Scrollable content</main>
</div>
Responsive Grids — Two Approaches
Real layouts rarely use the same column count on every screen. Tailwind gives you two ways to build a grid that adapts: breakpoint prefixes for explicit control, and auto-fit/auto-fill for a grid that reflows itself without you writing a single breakpoint.
<!-- 1 column on mobile, 2 on tablets, 4 on desktop -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
<div class="bg-cyan-500 rounded-lg p-6">Card 1</div>
<div class="bg-purple-500 rounded-lg p-6">Card 2</div>
<div class="bg-orange-500 rounded-lg p-6">Card 3</div>
<div class="bg-green-500 rounded-lg p-6">Card 4</div>
</div>
The auto-fit approach uses an arbitrary value to describe the raw CSS directly — no breakpoints needed, the browser recalculates the column count on every resize:
<!-- As many 200px+ columns as will fit, no breakpoints needed -->
<div class="grid grid-cols-[repeat(auto-fit,minmax(200px,1fr))] gap-4">
<div class="bg-cyan-500 rounded-lg p-6">Card 1</div>
<div class="bg-purple-500 rounded-lg p-6">Card 2</div>
<div class="bg-orange-500 rounded-lg p-6">Card 3</div>
</div>
| Approach | Best for | Trade-off |
|---|---|---|
| Breakpoint prefixes (sm:, lg:) | Layouts needing exact column counts at known widths | More classes to write and maintain |
| auto-fit / auto-fill | Card grids, galleries, tag lists of unknown length | Less precise control over exact breakpoints |
auto-fit collapses empty
tracks so existing items stretch to fill the row;
auto-fill keeps those empty
tracks reserved instead. For most card grids,
auto-fit is what you want.
Grid Auto-Flow, Auto-Rows & Auto-Cols
When you have more items than defined grid cells, Grid creates
implicit tracks to fit them.
grid-auto-flow controls the
direction those implicit tracks are added in, and
auto-rows-* /
auto-cols-* control how big
they are.
grid-flow-col.
grid-auto-flow for simple card
grids — it only matters once you start manually placing some items
with col-span-* or row-span-*, which is
exactly what the next chapter covers.
Live Examples — See Grid in Action
These boxes are rendered with real Tailwind utility classes right on this page.
grid grid-cols-3 gap-3
— a basic equal-width grid:
grid-cols-1 sm:grid-cols-2 lg:grid-cols-4
— resize your browser to watch this reflow:
grid-cols-[repeat(auto-fit,minmax(140px,1fr))]
— reflows without any breakpoints at all:
Common Real-World Use Cases
Grid is the natural choice any time content needs to line up in both directions at once.
grid grid-cols-3 gap-2
keeps every thumbnail the same size and perfectly aligned.
grid-cols-1 sm:grid-cols-2 lg:grid-cols-4
is the standard responsive metrics row.
grid-cols-1 md:grid-cols-3
keeps plan cards the same height and cleanly compared.
minmax(280px, 1fr)
grid reflows naturally as new posts are added.
grid-cols-2 gap-4
lines up label/field pairs like First Name and Last Name side
by side.
grid-rows-[auto_1fr_auto]
pins a header and footer while the middle row fills and
scrolls.
Quick Reference — Grid Classes
| Class | CSS Equivalent | Description |
|---|---|---|
| grid | display: grid | Block-level, two-dimensional grid container |
| inline-grid | display: inline-grid | Inline-level grid container |
| grid-cols-3 | grid-template-columns: repeat(3, minmax(0, 1fr)) | Three equal-width column tracks |
| grid-cols-none | grid-template-columns: none | No explicit columns defined |
| grid-rows-3 | grid-template-rows: repeat(3, minmax(0, 1fr)) | Three equal-height row tracks |
| grid-cols-[...] | grid-template-columns: [arbitrary value] | Custom track sizes, e.g. auto-fit grids |
| grid-flow-row | grid-auto-flow: row | Default — implicit items flow by row |
| grid-flow-col | grid-auto-flow: column | Implicit items flow by column instead |
| grid-flow-dense | grid-auto-flow: dense | Backfills earlier gaps automatically |
| auto-rows-fr | grid-auto-rows: minmax(0, 1fr) | Implicit rows share height equally |
| auto-cols-fr | grid-auto-columns: minmax(0, 1fr) | Implicit columns share width equally |
| gap-4 | gap: 1rem | Spacing between every row and column |
-
Grid is a two-dimensional layout model — define
columns and rows at once with
gridandgrid-cols-*. - Items placed beyond your defined tracks automatically wrap into new rows — Grid handles that placement for you.
-
Build responsive grids either with
breakpoint prefixes (
sm:grid-cols-2) for exact control, or auto-fit/auto-fill with an arbitrary value for a grid that reflows without any breakpoints at all. -
grid-auto-flow,auto-rows-*, andauto-cols-*control how implicit tracks are added and sized when you have more items than defined cells. - Real patterns include photo galleries, dashboard stat rows, pricing tables, article grids, form layouts, and app shells.
- Next chapter continues with Grid Placement — col-span, row-span, col-start/end, and grid-area for precisely positioning individual grid items.
Quick Check — Questions & Answers
Test your understanding of this chapter. Click a question to reveal the answer.
repeat(auto-fit, minmax(...)):<div class="grid
grid-cols-[repeat(auto-fit,minmax(200px,1fr))]
gap-4">This tells the browser to fit as many 200px-minimum columns as will comfortably fit, and stretch them (
1fr)
to share any remaining space evenly — all recalculated
automatically on every resize, no
sm:/md:/lg:
prefixes required.
grid-rows-* defines
explicit row tracks — a fixed number of
rows with a set height distribution, similar to how
grid-cols-* defines columns.Most grids skip it because row content is usually variable — you don't know in advance how many rows a photo gallery or blog list will need. Leaving rows undefined lets Grid create implicit rows automatically, sized to fit their content, which is what you want almost all of the time.
grid-rows-* is mainly useful for
fixed-structure layouts like a header/content/footer page
shell.
grid-cols-3, a 7th item simply starts a
new row rather than causing an error or overflow.
grid-auto-flow controls whether those new
tracks are added as rows (default) or columns, and
auto-rows-* / auto-cols-*
control how big they are — by default they size to fit their
content.
grid-cols-* classes, starting with the
unprefixed (mobile) value:<div class="grid grid-cols-1 sm:grid-cols-2
lg:grid-cols-4 gap-4">Tailwind is mobile-first, so
grid-cols-1 applies by default, then
sm:grid-cols-2 takes over at the
sm breakpoint and up, and
lg:grid-cols-4 takes over from
lg onward — each rule only applies at its
breakpoint and wider.
MCQ Quiz — Test Yourself
Select the best answer for each question. Correct answers are revealed after you click.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.