Ch 13 / 45
Chapter 13 Intermediate 10 min read Tailwind CSS

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.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
What makes Grid fundamentally different from Flexbox — two-dimensional vs one-dimensional layout
Defining columns and rows with grid-cols-* and grid-rows-*
Building breakpoint-responsive grids that reflow at every screen size
Auto-fit & auto-fill grids that reflow without writing any breakpoints at all
Controlling implicit tracks with grid-auto-flow, auto-rows, and auto-cols
Real grid patterns — galleries, dashboards, pricing tables, and form layouts

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.

A basic 3-column grid
<!-- 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.

grid
display: grid — a block-level, two-dimensional grid container.
inline-grid
display: inline-grid — an inline-level grid that only takes the width its content needs.
grid-cols-*
Defines the column tracks — e.g. grid-cols-3 splits the width into three equal columns.
grid-rows-*
Defines explicit row tracks — used far less often than columns, mostly for fixed-height sections.
Note Individual items can span multiple columns or rows with 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.

Combining columns and rows
<!-- 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.

Breakpoint-based responsive grid
<!-- 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:

Auto-fit grid (arbitrary value)
<!-- 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
Tip 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.

Auto-flow & auto-sizing classes — a quick reference
grid-flow-row
Default. New items fill left-to-right, adding new rows as needed.
grid-flow-col
New items fill top-to-bottom, adding new columns instead — useful for horizontal-scroll layouts.
grid-flow-dense
Backfills earlier empty gaps in the grid, even if it means placing later items out of source order — great for masonry-style galleries with mixed item sizes.
auto-rows-auto
Default. Implicit rows size themselves to fit their content.
auto-rows-fr
Every implicit row shares the available height equally — handy for uniform card heights.
auto-cols-*
Same idea as auto-rows, but sizes implicit columns instead — used with grid-flow-col.
Note You'll almost never need 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:

Live Preview
1
2
3
4
5
6

grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 — resize your browser to watch this reflow:

Live Preview
Stat A
Stat B
Stat C
Stat D

grid-cols-[repeat(auto-fit,minmax(140px,1fr))] — reflows without any breakpoints at all:

Live Preview
Tag A
Tag B
Tag C
Tag D
Tag E

Common Real-World Use Cases

Grid is the natural choice any time content needs to line up in both directions at once.

Photo / Image Gallery
grid grid-cols-3 gap-2 keeps every thumbnail the same size and perfectly aligned.
Dashboard Stat Cards
grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 is the standard responsive metrics row.
Pricing Table
grid-cols-1 md:grid-cols-3 keeps plan cards the same height and cleanly compared.
Blog / Article Grid
An auto-fit minmax(280px, 1fr) grid reflows naturally as new posts are added.
Two-Column Form Layout
grid-cols-2 gap-4 lines up label/field pairs like First Name and Last Name side by side.
App Shell Layout
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
Featured Course
Master Tailwind CSS with Real Projects(React Course)
Go from utility-class beginner to building production-grade UIs. Our structured course covers Tailwind v4, Vite integration, custom themes, component patterns, and 5 real projects including a full SaaS landing page and dashboard.
45 Chapters 10+ Hours of Content 5 Real Projects Certificate Included Live Mentoring
🎨
⭐ 4.9 · 1,200+ students
Tailwind CSS Grid Cheat Sheet
Download our free one-page cheat sheet covering every grid-cols, grid-rows, responsive grid pattern, auto-fit technique, and grid-auto-flow utility class.
CSS Grid Grid-Cols Responsive Grid Auto-Fit Grid-Flow Layout
Download Free Cheat Sheet
Chapter Summary
  • Grid is a two-dimensional layout model — define columns and rows at once with grid and grid-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-*, and auto-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.

Flexbox is one-dimensional — it arranges items along a single line, either a row or a column, and doesn't naturally align content across both directions at once. Grid is two-dimensional — it lets you define rows and columns together, so items line up like cells in a table both horizontally and vertically. A good rule of thumb: reach for Flexbox when aligning items in one direction (a navbar, a button group); reach for Grid when you need alignment in both directions at once (a photo gallery, a dashboard).
Use an arbitrary value with 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 automatically creates implicit tracks to hold the extra items — with 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.
Stack breakpoint-prefixed 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.

Question 1 of 4
Which class turns an element into a two-dimensional grid container?
Question 2 of 4
Which class creates a grid with 3 equal-width column tracks?
Question 3 of 4
Which technique creates a grid that automatically fits as many columns as will fit, each at least 200px wide, with no breakpoints written?
Question 4 of 4
Which class makes every implicit grid row share the available height equally?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel