Skip to main content
TailwindCSS Basics
CHAPTER 08 Beginner

Tailwind CSS Grid Layouts

Updated: May 12, 2026
25 min read

# Chapter 8: Tailwind CSS Grid Layouts

While Flexbox is fantastic for one-dimensional layouts (a single row or column), there are times when you need true two-dimensional control. You need rigid columns AND rigid rows simultaneously. This is where CSS Grid comes in.

Tailwind CSS provides a very elegant abstraction over CSS Grid, making complex layouts like dashboards, photo galleries, and card grids incredibly simple to build.

---

1. Introduction

CSS Grid allows you to divide a container into a grid of columns and rows, and then explicitly place child elements into specific cells within that grid.

With Tailwind, you define a grid using the grid class, specify the number of columns with grid-cols-{n}, and manage spacing with the gap utility.

2. Learning Objectives

By the end of this chapter, you will be able to:

  • Create a Grid container.
  • Define a specific number of equal-width columns using grid-cols-.
  • Make elements span across multiple columns or rows.
  • Build complex, responsive layouts without complex media queries.

3. Beginner-Friendly Explanations

Flexbox vs. Grid

  • Flexbox: Imagine a string of pearls. You can stretch the string, wrap it, or squish the pearls together. It's fluid.
  • Grid: Imagine a chessboard. It has strict, defined squares. You can place pieces in specific squares, or have a piece (like a ship in Battleship) span across 3 squares.

4. Syntax Explanation

  • Initialization: grid
  • Columns: grid-cols-1, grid-cols-2, grid-cols-3 ... up to grid-cols-12.
  • Gap: gap-4, gap-x-4 (horizontal gap), gap-y-4 (vertical gap).
  • Column Spanning (for children): col-span-2 (take up 2 columns), col-span-full (take up the entire row).
  • Row Spanning (for children): row-span-2 (take up 2 rows).

5. Real-World Examples

A classic use case for Grid is an e-commerce product listing page. You want a grid that shows 1 product per row on mobile, 2 on tablets, and 4 on desktop. <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6"> This ensures all product cards align perfectly in rows and columns, regardless of screen size.

6. Multiple Code Examples

Let's build some grids.

Example 1: Basic 3-Column Grid

html
12345678
<!-- TailwindCSS Example -->
<div class="grid grid-cols-3 gap-4 bg-gray-100 p-4">
  <div class="bg-blue-500 text-white p-4 text-center rounded">1</div>
  <div class="bg-blue-500 text-white p-4 text-center rounded">2</div>
  <div class="bg-blue-500 text-white p-4 text-center rounded">3</div>
  <div class="bg-blue-500 text-white p-4 text-center rounded">4 (Wraps)</div>
  <div class="bg-blue-500 text-white p-4 text-center rounded">5</div>
</div>

Example 2: Spanning Columns

Making an element take up more space than a single cell.

html
123456
<!-- TailwindCSS Example -->
<div class="grid grid-cols-3 gap-4 bg-gray-100 p-4">
  <div class="col-span-2 bg-purple-500 text-white p-4 rounded">Takes up 2 columns</div>
  <div class="bg-pink-500 text-white p-4 rounded">Takes 1</div>
  <div class="col-span-full bg-green-500 text-white p-4 rounded">Takes entire row</div>
</div>

Example 3: Spanning Rows

html
12345678
<!-- TailwindCSS Example -->
<div class="grid grid-cols-3 grid-rows-2 gap-4 bg-gray-100 p-4 h-64">
  <!-- This item spans 2 rows vertically -->
  <div class="row-span-2 bg-indigo-500 text-white p-4 rounded">Tall Item</div>
  <div class="bg-blue-400 p-4 rounded">Standard</div>
  <div class="bg-blue-400 p-4 rounded">Standard</div>
  <div class="col-span-2 bg-blue-600 p-4 rounded text-white">Wide Bottom</div>
</div>

Example 4: Unequal Columns (Arbitrary Grid Templates)

Sometimes you don't want equal fractions. You want a sidebar fixed at 250px, and the main content to take the rest.

html
12345
<!-- TailwindCSS Example -->
<div class="grid grid-cols-[250px_1fr] gap-4 h-48">
  <div class="bg-slate-800 text-white p-4">Sidebar (250px fixed)</div>
  <div class="bg-slate-100 p-4 border border-slate-300">Main Content (1fr - fractional remaining space)</div>
</div>

Example 5: Grid Flow

Change how elements automatically fill the grid.

html
1234567
<!-- TailwindCSS Example -->
<div class="grid grid-cols-3 grid-rows-3 grid-flow-col gap-2 p-4 border h-48">
  <div class="bg-amber-300 p-2 text-center">1 (Top Left)</div>
  <div class="bg-amber-400 p-2 text-center">2 (Mid Left)</div>
  <div class="bg-amber-500 p-2 text-center">3 (Bottom Left)</div>
  <div class="bg-amber-600 p-2 text-center">4 (Top Mid)</div>
</div>

*(Notice how it fills columns downwards first, instead of rows across).*

Example 6: Auto-fill / Auto-fit Grid (Magic Responsiveness)

Using arbitrary values to create a grid that automatically adds columns as the screen gets wider, without media queries!

html
1234567
<!-- TailwindCSS Example -->
<div class="grid grid-cols-[repeat(auto-fit,minmax(200px,1fr))] gap-4 p-4 border">
  <div class="bg-teal-500 h-24 rounded text-white p-4">Card</div>
  <div class="bg-teal-500 h-24 rounded text-white p-4">Card</div>
  <div class="bg-teal-500 h-24 rounded text-white p-4">Card</div>
  <div class="bg-teal-500 h-24 rounded text-white p-4">Card</div>
</div>

7. Output Explanations

In Example 4 (grid-cols-[250px_1fr]), we used an arbitrary value to define a custom grid template. The first column is strictly set to 250px. The second column is set to 1fr (one fractional unit). In CSS Grid, 1fr means "take up all the remaining available space". This is the absolute cleanest way to build a dashboard layout with a fixed sidebar!

8. Common Mistakes

  1. 1. Not applying grid to the parent: Just like Flexbox, applying col-span-2 to an element does nothing unless its parent container has the grid class.
  1. 2. Confusing Flexbox Gap and Grid Gap: While Tailwind unified the gap utility for both Flex and Grid, remember that in Grid, the gap creates strict gutters between cells, which maintains perfect mathematical alignment across complex multi-row layouts.
  1. 3. Overcomplicating simple rows: If you just need three items in a row, Flexbox (flex justify-between) is often simpler than Grid. Use Grid when you have multiple rows that need to align precisely with each other on both axes.

9. Best Practices

  • 12-Column Grids: A common standard in web design is a 12-column grid (grid-cols-12). This is highly divisible (by 2, 3, 4, 6), allowing for very complex component sizing within a single standard layout.
  • Responsive Grids: Always design mobile-first. Start with grid-cols-1, then add columns for larger screens (e.g., md:grid-cols-3). We will cover this deeply in the Responsive Design chapter.

10. Exercises

  1. 1. Create a 4-column grid with 4 equal squares.
  1. 2. Create a "Bento Box" layout: A 3x2 grid. Make the first item span 2 columns and 2 rows. Fill the remaining spots with smaller boxes.
  1. 3. Build a layout with a header spanning the full top row, a sidebar taking 1 column on the left, and main content taking 3 columns on the right.

11. Mini Project: Dashboard Grid Layout

Let's build a modern, complex Dashboard skeleton using Grid. We want a sidebar, a header, a main chart area, and two smaller stat cards.

html
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
<!-- TailwindCSS Example: Dashboard Layout -->
<div class="h-screen bg-slate-100 p-4 font-sans">
  
  <!-- The Master Grid -->
  <div class="h-full grid grid-cols-12 grid-rows-[auto_1fr] gap-4">
    
    <!-- Sidebar: Spans 2 columns, full height (spanning both rows) -->
    <aside class="col-span-2 row-span-2 bg-slate-900 rounded-2xl p-6 text-white shadow-xl hidden md:block">
      <div class="font-bold text-2xl mb-10 text-indigo-400">DashUI</div>
      <nav class="space-y-4">
        <a href="#" class="block bg-slate-800 px-4 py-2 rounded-lg text-indigo-300">Overview</a>
        <a href="#" class="block px-4 py-2 text-slate-400 hover:text-white transition">Analytics</a>
        <a href="#" class="block px-4 py-2 text-slate-400 hover:text-white transition">Settings</a>
      </nav>
    </aside>

    <!-- Header: Spans the remaining 10 columns on top -->
    <header class="col-span-12 md:col-span-10 bg-white rounded-2xl p-4 shadow-sm flex justify-between items-center px-6">
      <h2 class="text-xl font-bold text-slate-800">Dashboard Overview</h2>
      <div class="w-10 h-10 bg-indigo-100 rounded-full border-2 border-indigo-500"></div>
    </header>

    <!-- Main Content Area: Spans 10 columns below the header -->
    <main class="col-span-12 md:col-span-10 grid grid-cols-3 gap-4">
      
      <!-- Big Chart Area: Takes up 2 out of 3 content columns -->
      <div class="col-span-3 lg:col-span-2 bg-white rounded-2xl p-6 shadow-sm border border-slate-200">
        <h3 class="font-semibold text-slate-500 mb-4 uppercase tracking-wider text-sm">Revenue Over Time</h3>
        <!-- Placeholder for chart -->
        <div class="w-full h-64 bg-slate-50 rounded-xl border-2 border-dashed border-slate-200 flex items-center justify-center text-slate-400">
          Chart Graphic Here
        </div>
      </div>

      <!-- Right Column for smaller stats -->
      <div class="col-span-3 lg:col-span-1 flex flex-col gap-4">
        
        <!-- Stat Card 1 -->
        <div class="flex-1 bg-gradient-to-br from-indigo-500 to-purple-600 rounded-2xl p-6 text-white shadow-lg">
          <p class="text-indigo-100 text-sm font-medium">Total Users</p>
          <p class="text-4xl font-extrabold mt-2">24,592</p>
          <p class="text-sm mt-4 text-indigo-200">+12% from last week</p>
        </div>
        
        <!-- Stat Card 2 -->
        <div class="flex-1 bg-white rounded-2xl p-6 shadow-sm border border-slate-200">
          <p class="text-slate-500 text-sm font-medium">Server Status</p>
          <div class="flex items-center gap-3 mt-4">
            <div class="w-4 h-4 rounded-full bg-emerald-500 animate-pulse"></div>
            <p class="text-xl font-bold text-slate-800">Healthy</p>
          </div>
        </div>

      </div>
    </main>

  </div>
</div>

Output Explanation: This layout is incredibly robust. The master wrapper uses a 12-column grid.

  • The sidebar takes exactly 2 columns (col-span-2) and stretches across both the header row and content row (row-span-2).
  • We then nest *another* grid inside the <main> area! Nested grids are highly encouraged for complex layouts. The main area splits into 3 columns; the chart takes 2, the stat cards take 1.

12. Coding Challenges

Challenge: Recreate an Instagram profile photo grid. It should be exactly 3 columns wide. Every photo should be perfectly square. (Hint: look into Tailwind's aspect-square utility to use alongside your grid).

13. MCQs with Answers

Question 1

To make an item span across 3 columns in a grid, which utility do you use?

Question 2

What does col-span-full do?

14. Interview Questions

Q: Provide a scenario where you would explicitly choose CSS Grid over Flexbox. *Answer:* I would choose Grid when building a complex layout that requires strict alignment on both the horizontal AND vertical axes simultaneously. For example, a dashboard layout with a sidebar and header, or a strict photo gallery. If I change the height of one card in a row, and I want the card directly *below* it in the next row to stay perfectly aligned with the rest of its row, Grid handles this natively, whereas Flexbox would break alignment or wrap awkwardly.

15. FAQs

Q: Can I use Flexbox inside a Grid? A: Absolutely! This is the standard way to build web apps. You use Grid for the macro-layout (Sidebar, Main content) and Flexbox inside the individual cells for the micro-layout (aligning an icon next to text inside a button).

16. Summary

Tailwind's Grid utilities abstract away the most complex parts of the CSS Grid specification. By defining grid-cols and using col-span on children, you can quickly sketch out robust, two-dimensional architectural layouts that would otherwise require hundreds of lines of brittle CSS.

17. Next Chapter Recommendation

Now that we can structure pages flawlessly, it's time to refine the visual details of our components. In Chapter 9: Tailwind CSS Borders and Shadows, we will explore how to add depth, establish boundaries, and create modern "card" aesthetics using border radii and box shadows.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·