Grid Layout
In this chapter, you learn how to leverage CSS Grid with Tailwind CSS.
Creating a grid container
Use the grid class to create a grid container.
<div class="grid">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Specifying the number of columns
Fixed number of columns
<!-- 3 columns -->
<div class="grid grid-cols-3">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
| Class | Columns |
|---|---|
grid-cols-1 | 1 |
grid-cols-2 | 2 |
grid-cols-3 | 3 |
grid-cols-4 | 4 |
grid-cols-6 | 6 |
grid-cols-12 | 12 |
Dynamic column counts in v4
In v4, you can specify any number directly.
<!-- v4: any number -->
<div class="grid grid-cols-5">...</div>
<div class="grid grid-cols-15">...</div>
Auto-fit
<!-- Adjusts automatically to the content -->
<div class="grid grid-cols-[repeat(auto-fit,minmax(200px,1fr))]">
<div>Card 1</div>
<div>Card 2</div>
<div>Card 3</div>
</div>
Specifying the number of rows
<div class="grid grid-rows-3 grid-flow-col">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
Spacing (gap)
<!-- All directions -->
<div class="grid grid-cols-3 gap-4">...</div>
<!-- Different gaps for horizontal and vertical -->
<div class="grid grid-cols-3 gap-x-4 gap-y-8">...</div>
Spanning (merging)
Column span
<div class="grid grid-cols-4 gap-4">
<div class="col-span-2">Spans 2 columns</div>
<div>1 column</div>
<div>1 column</div>
<div>1 column</div>
<div class="col-span-3">Spans 3 columns</div>
</div>
| Class | Span |
|---|---|
col-span-1 | 1 column |
col-span-2 | 2 columns |
col-span-3 | 3 columns |
col-span-full | All columns |
Row span
<div class="grid grid-cols-3 grid-rows-3 gap-4">
<div class="row-span-2">Spans 2 rows</div>
<div>Normal</div>
<div>Normal</div>
<div>Normal</div>
<div class="row-span-2">Spans 2 rows</div>
</div>
Start and end positions
Column position
<div class="grid grid-cols-6 gap-4">
<div class="col-start-2 col-end-4">Columns 2-3</div>
<div class="col-start-5">From column 5</div>
</div>
| Class | Position |
|---|---|
col-start-1 | Starts at 1 |
col-end-3 | Ends at 3 |
col-start-auto | Auto |
Row position
<div class="grid grid-cols-3 grid-rows-3 gap-4">
<div class="row-start-2 row-end-4">Rows 2-3</div>
</div>
Alignment
Aligning items (place-items)
<!-- Centered -->
<div class="grid place-items-center h-screen">
<div>Center of the screen</div>
</div>
| Class | Alignment |
|---|---|
place-items-start | Start |
place-items-end | End |
place-items-center | Center |
place-items-stretch | Stretch |
Aligning content (place-content)
<div class="grid grid-cols-2 place-content-center h-64">
<div>1</div>
<div>2</div>
</div>
Aligning individual items (place-self)
<div class="grid grid-cols-3 h-32">
<div class="place-self-start">Start</div>
<div class="place-self-center">Center</div>
<div class="place-self-end">End</div>
</div>
Auto placement (grid-auto-flow)
| Class | Placement direction |
|---|---|
grid-flow-row | Row direction (default) |
grid-flow-col | Column direction |
grid-flow-row-dense | Row direction (dense) |
grid-flow-col-dense | Column direction (dense) |
<!-- Place in the column direction -->
<div class="grid grid-rows-3 grid-flow-col gap-4">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
Subgrid
Uses for subgrid
Subgrid (grid-cols-subgrid / grid-rows-subgrid) is a CSS Grid Level 2 feature. Because child elements can inherit the track definitions of the parent grid, it is handy for aligning the height of footers in a card list, and similar cases.
The subgrid utilities are available from Tailwind CSS v3.4 onward (and likewise in v4).
<div class="grid grid-cols-4 gap-4">
<div class="col-span-3 grid grid-cols-subgrid">
<div>Sub 1</div>
<div>Sub 2</div>
<div>Sub 3</div>
</div>
</div>
Subgrid is especially useful when you want to align the height of multiple child elements, as in a card list.
<!-- Align the footer height within a card list -->
<div class="grid grid-cols-3 gap-6">
<div class="grid grid-rows-subgrid row-span-3">
<h3 class="text-lg font-bold">Title</h3>
<p class="text-gray-600">Description (aligns even when the lengths differ)</p>
<a href="#" class="text-blue-500">Read more →</a>
</div>
<!-- Other cards have the same structure -->
</div>
Practical examples
Dashboard layout
<div class="grid grid-cols-4 grid-rows-3 gap-4 h-screen p-4">
<!-- Header -->
<header class="col-span-4 bg-white rounded-lg shadow-sm p-4">
Header
</header>
<!-- Sidebar -->
<aside class="row-span-2 bg-white rounded-lg shadow-sm p-4">
Sidebar
</aside>
<!-- Main content -->
<main class="col-span-3 bg-white rounded-lg shadow-sm p-4">
Main content
</main>
<!-- Footer -->
<footer class="col-span-3 bg-white rounded-lg shadow-sm p-4">
Footer
</footer>
</div>
Card gallery
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 p-6">
<div class="bg-white rounded-lg shadow-sm overflow-hidden">
<img src="..." class="w-full h-48 object-cover" />
<div class="p-4">
<h3 class="font-bold">Card 1</h3>
<p class="text-gray-600">Description...</p>
</div>
</div>
<!-- Repeat -->
</div>
Image grid
<div class="grid grid-cols-4 gap-2">
<div class="col-span-2 row-span-2">
<img src="..." class="w-full h-full object-cover" />
</div>
<div>
<img src="..." class="w-full h-full object-cover" />
</div>
<div>
<img src="..." class="w-full h-full object-cover" />
</div>
<div>
<img src="..." class="w-full h-full object-cover" />
</div>
<div>
<img src="..." class="w-full h-full object-cover" />
</div>
</div>
Flexbox vs Grid
| Use case | Recommended |
|---|---|
| One-dimensional layout (rows or columns) | Flexbox |
| Two-dimensional layout (rows and columns) | Grid |
| Content-based sizing | Flexbox |
| Strict grid placement | Grid |
| Navigation, button groups | Flexbox |
| Card gallery, dashboard | Grid |
Summary
- Use
gridandgrid-cols-*for grid layouts - Use
col-span-*/row-span-*to merge cells - Use
place-items-*to align items - Use
gapto set spacing - Combine with responsive design for flexible layouts
What to read next
- Spacing — organize when to use gap / margin / padding
- Flexbox — revisit when to use it versus one-dimensional layouts
- Tailwind CSS Guide table of contents — back to the structure of all 18 chapters