Dev Logs
/Tailwind CSS/ Grid Utilities
Chapters
  • 01Introduction to Tailwind CSS
  • 02Installation and Setup
  • 03Tailwind Configuration
  • 04Typography and Spacing
  • 05Colors and Theme Customization
  • 06Flexbox Utilities
  • 07Responsive Design
  • 08Grid Utilities
    • Introduction to CSS Grid in Tailwind
    • Grid vs Flexbox
    • Basic Grid Setup
    • Creating a Grid Container
    • Grid Template Columns
    • Grid Template Rows
    • Gap (Grid Spacing)
    • Grid Item Placement
    • Column Span
    • Row Span
    • Grid Area Placement
    • Grid Alignment
    • Justify Items (Horizontal Alignment)
    • Align Items (Vertical Alignment)
    • Individual Item Alignment
    • Responsive Grid Layouts
    • Common Grid Patterns
    • Card Grid
    • Dashboard Layout
    • Masonry-like Layout
    • Auto-Fit and Auto-Fill
    • Visual Layout Diagram
    • Common Pitfalls & Anti-Patterns
    • Overcomplicating Simple Layouts
    • Use Flexbox for Simple Layouts
    • Not Considering Content
    • Responsive Grid with Content Consideration
    • Ignoring Accessibility
    • Maintain Logical Order
    • When and Why to Use Grid
    • Perfect for Grid:
    • Consider Flexbox Instead:
    • Performance Considerations
    • Efficient Grid Patterns
    • Avoid Performance Issues
    • Mini Challenges
    • Challenge 1: Photo Gallery
    • Challenge 2: Dashboard Grid
    • Challenge 3: Magazine Layout
    • Interview Tips
    • Challenge Answers
    • Answer 1: Photo Gallery
    • Answer 2: Dashboard Grid
    • Answer 3: Magazine Layout
  • 09Pseudo-Classes and State-Based Styling
  • 10Dark Mode
  • 11Customizing and Extending Tailwind Configuration
  • 12Advanced Layout Techniques
  • 13Final Project: Dashify - Complete Dashboard Application
All chapters

Grid Utilities

Introduction to CSS Grid in Tailwind

CSS Grid is a powerful two-dimensional layout system that allows you to create complex layouts with rows and columns. Tailwind CSS provides a comprehensive set of utilities for working with CSS Grid, making it easy to create responsive grid layouts without writing custom CSS.

Grid vs Flexbox

Use CSS Grid when:

  • You need two-dimensional layouts (rows AND columns)
  • You want to control both horizontal and vertical alignment
  • You're creating complex layouts like dashboards, galleries, or card grids
  • You need precise control over item placement

Use Flexbox when:

  • You need one-dimensional layouts (either row OR column)
  • You're aligning items within a container
  • You're creating navigation bars, button groups, or simple component layouts
  • You need flexible item sizing

Basic Grid Setup

Creating a Grid Container

html
<!-- Basic grid container -->
<div class="grid">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
</div>

<!-- Grid with defined columns -->
<div class="grid grid-cols-3">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
  <div class="bg-yellow-500 text-white p-4">Item 4</div>
  <div class="bg-purple-500 text-white p-4">Item 5</div>
  <div class="bg-pink-500 text-white p-4">Item 6</div>
</div>

Grid Template Columns

Tailwind provides utilities for defining grid columns:

html
<!-- Fixed number of columns -->
<div class="grid grid-cols-1">1 column</div>
<div class="grid grid-cols-2">2 columns</div>
<div class="grid grid-cols-3">3 columns</div>
<div class="grid grid-cols-4">4 columns</div>
<div class="grid grid-cols-5">5 columns</div>
<div class="grid grid-cols-6">6 columns</div>
<div class="grid grid-cols-12">12 columns</div>

<!-- Fractional columns -->
<div class="grid grid-cols-2 gap-4">
  <div class="bg-blue-500 text-white p-4">1fr</div>
  <div class="bg-green-500 text-white p-4">1fr</div>
</div>

<div class="grid grid-cols-3 gap-4">
  <div class="bg-blue-500 text-white p-4">1fr</div>
  <div class="bg-green-500 text-white p-4">1fr</div>
  <div class="bg-red-500 text-white p-4">1fr</div>
</div>

<!-- Mixed column sizes -->
<div class="grid grid-cols-[200px_1fr_100px] gap-4">
  <div class="bg-blue-500 text-white p-4">200px</div>
  <div class="bg-green-500 text-white p-4">1fr (flexible)</div>
  <div class="bg-red-500 text-white p-4">100px</div>
</div>

Grid Template Rows

html
<!-- Fixed number of rows -->
<div class="grid grid-rows-2 grid-cols-2 h-64 gap-4">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
  <div class="bg-yellow-500 text-white p-4">Item 4</div>
</div>

<!-- Different row heights -->
<div class="grid grid-rows-[100px_1fr_50px] grid-cols-2 h-96 gap-4">
  <div class="bg-blue-500 text-white p-4">100px high</div>
  <div class="bg-green-500 text-white p-4">100px high</div>
  <div class="bg-red-500 text-white p-4">Flexible height</div>
  <div class="bg-yellow-500 text-white p-4">Flexible height</div>
  <div class="bg-purple-500 text-white p-4">50px high</div>
  <div class="bg-pink-500 text-white p-4">50px high</div>
</div>

Gap (Grid Spacing)

html
<!-- Uniform gap -->
<div class="grid grid-cols-3 gap-4">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
</div>

<!-- Different horizontal and vertical gaps -->
<div class="grid grid-cols-3 gap-x-8 gap-y-4">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
  <div class="bg-yellow-500 text-white p-4">Item 4</div>
  <div class="bg-purple-500 text-white p-4">Item 5</div>
  <div class="bg-pink-500 text-white p-4">Item 6</div>
</div>

<!-- No gap -->
<div class="grid grid-cols-3 gap-0">
  <div class="bg-blue-500 text-white p-4 border border-white">Item 1</div>
  <div class="bg-green-500 text-white p-4 border border-white">Item 2</div>
  <div class="bg-red-500 text-white p-4 border border-white">Item 3</div>
</div>

Grid Item Placement

Column Span

html
<div class="grid grid-cols-6 gap-4">
  <!-- Span multiple columns -->
  <div class="col-span-2 bg-blue-500 text-white p-4">Spans 2 columns</div>
  <div class="col-span-3 bg-green-500 text-white p-4">Spans 3 columns</div>
  <div class="col-span-1 bg-red-500 text-white p-4">1 column</div>

  <!-- Full width -->
  <div class="col-span-full bg-yellow-500 text-white p-4">
    Spans all columns
  </div>

  <!-- Specific column placement -->
  <div class="col-start-2 col-end-5 bg-purple-500 text-white p-4">
    Starts at column 2, ends at column 5
  </div>
</div>

Row Span

html
<div class="grid grid-cols-3 grid-rows-4 h-96 gap-4">
  <div class="row-span-2 bg-blue-500 text-white p-4">Spans 2 rows</div>
  <div class="bg-green-500 text-white p-4">Normal</div>
  <div class="bg-red-500 text-white p-4">Normal</div>

  <div class="bg-yellow-500 text-white p-4">Normal</div>
  <div class="bg-purple-500 text-white p-4">Normal</div>

  <div class="row-span-2 bg-pink-500 text-white p-4">Spans 2 rows</div>
  <div class="bg-indigo-500 text-white p-4">Normal</div>

  <div class="bg-gray-500 text-white p-4">Normal</div>
</div>

Grid Area Placement

html
<!-- Using grid lines -->
<div class="grid grid-cols-4 grid-rows-3 h-64 gap-4">
  <div
    class="col-start-1 col-end-3 row-start-1 row-end-2 bg-blue-500 text-white p-4"
  >
    Header (columns 1-2, row 1)
  </div>
  <div
    class="col-start-3 col-end-5 row-start-1 row-end-3 bg-green-500 text-white p-4"
  >
    Sidebar (columns 3-4, rows 1-2)
  </div>
  <div
    class="col-start-1 col-end-3 row-start-2 row-end-4 bg-red-500 text-white p-4"
  >
    Main content (columns 1-2, rows 2-3)
  </div>
  <div
    class="col-start-3 col-end-5 row-start-3 row-end-4 bg-yellow-500 text-white p-4"
  >
    Footer (columns 3-4, row 3)
  </div>
</div>

Grid Alignment

Justify Items (Horizontal Alignment)

html
<!-- Justify items start -->
<div class="grid grid-cols-3 gap-4 justify-items-start">
  <div class="w-16 h-16 bg-blue-500"></div>
  <div class="w-16 h-16 bg-green-500"></div>
  <div class="w-16 h-16 bg-red-500"></div>
</div>

<!-- Justify items center -->
<div class="grid grid-cols-3 gap-4 justify-items-center">
  <div class="w-16 h-16 bg-blue-500"></div>
  <div class="w-16 h-16 bg-green-500"></div>
  <div class="w-16 h-16 bg-red-500"></div>
</div>

<!-- Justify items end -->
<div class="grid grid-cols-3 gap-4 justify-items-end">
  <div class="w-16 h-16 bg-blue-500"></div>
  <div class="w-16 h-16 bg-green-500"></div>
  <div class="w-16 h-16 bg-red-500"></div>
</div>

<!-- Justify items stretch (default) -->
<div class="grid grid-cols-3 gap-4 justify-items-stretch">
  <div class="h-16 bg-blue-500"></div>
  <div class="h-16 bg-green-500"></div>
  <div class="h-16 bg-red-500"></div>
</div>

Align Items (Vertical Alignment)

html
<!-- Align items start -->
<div class="grid grid-cols-3 gap-4 h-32 align-items-start">
  <div class="w-16 h-8 bg-blue-500"></div>
  <div class="w-16 h-8 bg-green-500"></div>
  <div class="w-16 h-8 bg-red-500"></div>
</div>

<!-- Align items center -->
<div class="grid grid-cols-3 gap-4 h-32 items-center">
  <div class="w-16 h-8 bg-blue-500"></div>
  <div class="w-16 h-8 bg-green-500"></div>
  <div class="w-16 h-8 bg-red-500"></div>
</div>

<!-- Align items end -->
<div class="grid grid-cols-3 gap-4 h-32 items-end">
  <div class="w-16 h-8 bg-blue-500"></div>
  <div class="w-16 h-8 bg-green-500"></div>
  <div class="w-16 h-8 bg-red-500"></div>
</div>

<!-- Align items stretch (default) -->
<div class="grid grid-cols-3 gap-4 h-32 items-stretch">
  <div class="w-16 bg-blue-500"></div>
  <div class="w-16 bg-green-500"></div>
  <div class="w-16 bg-red-500"></div>
</div>

Individual Item Alignment

html
<div class="grid grid-cols-3 gap-4 h-32">
  <div class="w-16 h-8 bg-blue-500 justify-self-start">Start</div>
  <div class="w-16 h-8 bg-green-500 justify-self-center">Center</div>
  <div class="w-16 h-8 bg-red-500 justify-self-end">End</div>
</div>

<div class="grid grid-cols-3 gap-4 h-32">
  <div class="w-16 h-8 bg-blue-500 self-start">Top</div>
  <div class="w-16 h-8 bg-green-500 self-center">Middle</div>
  <div class="w-16 h-8 bg-red-500 self-end">Bottom</div>
</div>

Responsive Grid Layouts

html
<!-- Responsive columns -->
<div
  class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4"
>
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
  <div class="bg-yellow-500 text-white p-4">Item 4</div>
  <div class="bg-purple-500 text-white p-4">Item 5</div>
  <div class="bg-pink-500 text-white p-4">Item 6</div>
  <div class="bg-indigo-500 text-white p-4">Item 7</div>
  <div class="bg-gray-500 text-white p-4">Item 8</div>
</div>

<!-- Responsive gaps -->
<div class="grid grid-cols-2 lg:grid-cols-4 gap-2 md:gap-4 lg:gap-6">
  <div class="bg-blue-500 text-white p-4">Item 1</div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
  <div class="bg-yellow-500 text-white p-4">Item 4</div>
</div>

<!-- Responsive spanning -->
<div class="grid grid-cols-2 lg:grid-cols-4 gap-4">
  <div class="col-span-2 lg:col-span-1 bg-blue-500 text-white p-4">
    Responsive span
  </div>
  <div class="bg-green-500 text-white p-4">Item 2</div>
  <div class="bg-red-500 text-white p-4">Item 3</div>
  <div class="bg-yellow-500 text-white p-4">Item 4</div>
</div>

Common Grid Patterns

Card Grid

html
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
  <!-- Card 1 -->
  <div class="bg-white rounded-lg shadow-md overflow-hidden">
    <div class="h-48 bg-gray-300"></div>
    <div class="p-6">
      <h3 class="text-xl font-semibold mb-2">Card Title</h3>
      <p class="text-gray-600 mb-4">
        Card description goes here. This is some sample text.
      </p>
      <button
        class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
      >
        Read More
      </button>
    </div>
  </div>

  <!-- Card 2 -->
  <div class="bg-white rounded-lg shadow-md overflow-hidden">
    <div class="h-48 bg-gray-300"></div>
    <div class="p-6">
      <h3 class="text-xl font-semibold mb-2">Another Card</h3>
      <p class="text-gray-600 mb-4">
        More card content with different text length.
      </p>
      <button
        class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
      >
        Read More
      </button>
    </div>
  </div>

  <!-- Card 3 -->
  <div class="bg-white rounded-lg shadow-md overflow-hidden">
    <div class="h-48 bg-gray-300"></div>
    <div class="p-6">
      <h3 class="text-xl font-semibold mb-2">Third Card</h3>
      <p class="text-gray-600 mb-4">Yet another card with some content.</p>
      <button
        class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
      >
        Read More
      </button>
    </div>
  </div>
</div>

Dashboard Layout

html
<div
  class="grid grid-cols-1 lg:grid-cols-4 grid-rows-[auto_1fr_auto] h-screen gap-4 p-4"
>
  <!-- Header -->
  <header class="col-span-full bg-white rounded-lg shadow p-4">
    <h1 class="text-2xl font-bold">Dashboard</h1>
  </header>

  <!-- Sidebar -->
  <aside class="lg:col-span-1 bg-white rounded-lg shadow p-4">
    <nav>
      <ul class="space-y-2">
        <li>
          <a href="#" class="block p-2 rounded hover:bg-gray-100">Overview</a>
        </li>
        <li>
          <a href="#" class="block p-2 rounded hover:bg-gray-100">Analytics</a>
        </li>
        <li>
          <a href="#" class="block p-2 rounded hover:bg-gray-100">Users</a>
        </li>
        <li>
          <a href="#" class="block p-2 rounded hover:bg-gray-100">Settings</a>
        </li>
      </ul>
    </nav>
  </aside>

  <!-- Main Content -->
  <main class="lg:col-span-3 bg-white rounded-lg shadow p-4">
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 mb-6">
      <!-- Stats Cards -->
      <div class="bg-blue-50 p-4 rounded-lg">
        <h3 class="text-sm font-medium text-blue-600">Total Users</h3>
        <p class="text-2xl font-bold text-blue-900">1,234</p>
      </div>
      <div class="bg-green-50 p-4 rounded-lg">
        <h3 class="text-sm font-medium text-green-600">Revenue</h3>
        <p class="text-2xl font-bold text-green-900">$12,345</p>
      </div>
      <div class="bg-purple-50 p-4 rounded-lg">
        <h3 class="text-sm font-medium text-purple-600">Orders</h3>
        <p class="text-2xl font-bold text-purple-900">567</p>
      </div>
    </div>

    <!-- Chart Area -->
    <div class="h-64 bg-gray-100 rounded-lg flex items-center justify-center">
      <p class="text-gray-500">Chart goes here</p>
    </div>
  </main>

  <!-- Footer -->
  <footer
    class="col-span-full bg-white rounded-lg shadow p-4 text-center text-gray-600"
  >
    © 2024 Your Company. All rights reserved.
  </footer>
</div>

Masonry-like Layout

html
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
  <!-- Varying heights create masonry effect -->
  <div class="bg-blue-500 text-white p-6 rounded-lg">
    <h3 class="text-xl font-bold mb-2">Short Card</h3>
    <p>This is a shorter card with less content.</p>
  </div>

  <div class="bg-green-500 text-white p-6 rounded-lg">
    <h3 class="text-xl font-bold mb-2">Medium Card</h3>
    <p class="mb-4">This card has a bit more content than the first one.</p>
    <p>It has multiple paragraphs to make it taller.</p>
  </div>

  <div class="bg-red-500 text-white p-6 rounded-lg">
    <h3 class="text-xl font-bold mb-2">Tall Card</h3>
    <p class="mb-4">This card has even more content.</p>
    <p class="mb-4">It has multiple paragraphs and sections.</p>
    <p class="mb-4">Making it the tallest card in the grid.</p>
    <p>This creates a natural masonry-like effect.</p>
  </div>

  <div class="bg-yellow-500 text-white p-6 rounded-lg">
    <h3 class="text-xl font-bold mb-2">Another Short</h3>
    <p>Back to a shorter card.</p>
  </div>

  <div class="bg-purple-500 text-white p-6 rounded-lg">
    <h3 class="text-xl font-bold mb-2">Medium Again</h3>
    <p class="mb-4">This one is medium height.</p>
    <p>With two paragraphs of content.</p>
  </div>

  <div class="bg-pink-500 text-white p-6 rounded-lg">
    <h3 class="text-xl font-bold mb-2">Final Card</h3>
    <p>The last card in our grid.</p>
  </div>
</div>

Auto-Fit and Auto-Fill

html
<!-- Auto-fit: columns collapse when empty -->
<div class="grid grid-cols-[repeat(auto-fit,minmax(200px,1fr))] gap-4">
  <div class="bg-blue-500 text-white p-4 rounded">Auto-fit 1</div>
  <div class="bg-green-500 text-white p-4 rounded">Auto-fit 2</div>
  <div class="bg-red-500 text-white p-4 rounded">Auto-fit 3</div>
</div>

<!-- Auto-fill: maintains empty columns -->
<div class="grid grid-cols-[repeat(auto-fill,minmax(200px,1fr))] gap-4">
  <div class="bg-blue-500 text-white p-4 rounded">Auto-fill 1</div>
  <div class="bg-green-500 text-white p-4 rounded">Auto-fill 2</div>
  <div class="bg-red-500 text-white p-4 rounded">Auto-fill 3</div>
</div>

Visual Layout Diagram

Grid Terminology:

┌─────────────────────────────────────────┐
│ Grid Container                          │
│ ┌─────┬─────┬─────┐ ← Grid Line (4)    │
│ │  1  │  2  │  3  │ ← Grid Track (Row) │
│ ├─────┼─────┼─────┤ ← Grid Line (3)    │
│ │  4  │  5  │  6  │                    │
│ └─────┴─────┴─────┘ ← Grid Line (2)    │
│   ↑     ↑     ↑                        │
│   │     │     └─ Grid Line (4)         │
│   │     └─ Grid Line (3)               │
│   └─ Grid Line (2)                     │
│ Grid Line (1)                          │
└─────────────────────────────────────────┘

Grid Areas:
┌─────────────────────────────────────────┐
│ Header (col 1-3, row 1)                 │
├─────────────────┬───────────────────────┤
│ Sidebar         │ Main Content          │
│ (col 1, row 2-3)│ (col 2-3, row 2)     │
│                 ├───────────────────────┤
│                 │ Footer                │
│                 │ (col 2-3, row 3)     │
└─────────────────┴───────────────────────┘

Responsive Grid:
Mobile (1 col):     Desktop (4 cols):
┌─────────────┐     ┌───┬───┬───┬───┐
│      1      │     │ 1 │ 2 │ 3 │ 4 │
├─────────────┤     ├───┼───┼───┼───┤
│      2      │     │ 5 │ 6 │ 7 │ 8 │
├─────────────┤     └───┴───┴───┴───┘
│      3      │
├─────────────┤
│      4      │
└─────────────┘

Common Pitfalls & Anti-Patterns

❌ Overcomplicating Simple Layouts

html
<!-- Bad: Using grid for simple one-dimensional layout -->
<div class="grid grid-cols-1 gap-4">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

✅ Use Flexbox for Simple Layouts

html
<!-- Good: Use flexbox for one-dimensional layouts -->
<div class="flex flex-col gap-4">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

❌ Not Considering Content

html
<!-- Bad: Fixed grid that doesn't adapt to content -->
<div class="grid grid-cols-4 gap-4">
  <div class="bg-blue-500 p-4">Short</div>
  <div class="bg-green-500 p-4">
    This is a much longer piece of content that might overflow
  </div>
  <div class="bg-red-500 p-4">Medium length content</div>
  <div class="bg-yellow-500 p-4">Short</div>
</div>

✅ Responsive Grid with Content Consideration

html
<!-- Good: Responsive grid that adapts to content and screen size -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
  <div class="bg-blue-500 p-4 rounded">Short</div>
  <div class="bg-green-500 p-4 rounded md:col-span-2 lg:col-span-1">
    This is a much longer piece of content that gets appropriate space
  </div>
  <div class="bg-red-500 p-4 rounded">Medium length content</div>
  <div class="bg-yellow-500 p-4 rounded">Short</div>
</div>

❌ Ignoring Accessibility

html
<!-- Bad: Visual order doesn't match DOM order -->
<div class="grid grid-cols-3">
  <div class="col-start-3">First in DOM, but appears last</div>
  <div class="col-start-1">Second in DOM, but appears first</div>
  <div class="col-start-2">Third in DOM, appears middle</div>
</div>

✅ Maintain Logical Order

html
<!-- Good: Visual order matches logical order -->
<div class="grid grid-cols-3">
  <div>First item</div>
  <div>Second item</div>
  <div>Third item</div>
</div>

<!-- Or use CSS order property sparingly and with care -->
<div class="grid grid-cols-3">
  <div class="order-1">Logically first, visually first</div>
  <div class="order-2">Logically second, visually second</div>
  <div class="order-3">Logically third, visually third</div>
</div>

When and Why to Use Grid

✅ Perfect for Grid:

  1. Two-dimensional layouts - When you need control over both rows and columns
  2. Complex layouts - Dashboards, magazine-style layouts, card grids
  3. Overlapping elements - When items need to overlap or span multiple areas
  4. Precise control - When you need exact placement of items
  5. Responsive design - When layout needs to change dramatically across breakpoints

❌ Consider Flexbox Instead:

  1. One-dimensional layouts - Navigation bars, button groups
  2. Content-driven sizing - When items should size based on their content
  3. Simple alignment - Centering items, distributing space
  4. Component layouts - Internal layout of cards, forms, etc.

Performance Considerations

Efficient Grid Patterns

html
<!-- Good: Use semantic HTML with grid -->
<main class="grid grid-cols-1 lg:grid-cols-[250px_1fr] gap-6">
  <aside class="bg-white p-6 rounded-lg shadow">
    <!-- Sidebar content -->
  </aside>
  <section class="bg-white p-6 rounded-lg shadow">
    <!-- Main content -->
  </section>
</main>

<!-- Good: Minimize DOM nesting -->
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
  <article class="bg-white p-4 rounded shadow">
    <!-- Card content -->
  </article>
  <!-- More cards -->
</div>

Avoid Performance Issues

html
<!-- Avoid: Too many nested grids -->
<div class="grid grid-cols-3">
  <div class="grid grid-cols-2">
    <div class="grid grid-cols-2">
      <!-- Too much nesting -->
    </div>
  </div>
</div>

<!-- Better: Flatten the structure -->
<div class="grid grid-cols-6 gap-4">
  <div class="col-span-3">Content 1</div>
  <div class="col-span-2">Content 2</div>
  <div class="col-span-1">Content 3</div>
</div>

Mini Challenges

Challenge 1: Photo Gallery

Create a responsive photo gallery that:

  • Shows 1 photo per row on mobile
  • Shows 2 photos per row on tablet
  • Shows 3 photos per row on desktop
  • Has a featured photo that spans 2 columns on desktop
  • Includes proper spacing and hover effects

Challenge 2: Dashboard Grid

Build a dashboard layout with:

  • A header that spans the full width
  • A sidebar that's hidden on mobile, visible on desktop
  • A main content area with a 2x2 stats grid
  • A chart area that spans the full width of the main content
  • Responsive behavior that stacks vertically on mobile

Challenge 3: Magazine Layout

Create a magazine-style layout featuring:

  • A hero article that spans 2 columns and 2 rows
  • Smaller articles in a grid around the hero
  • Different sized articles (some span 2 columns, others are single column)
  • Responsive behavior that adapts to different screen sizes
  • Proper typography hierarchy

Interview Tips

Q: "When would you choose CSS Grid over Flexbox?"

A: "I choose CSS Grid when:

  1. Two-dimensional control - I need to control both rows and columns simultaneously
  2. Complex layouts - Creating dashboard layouts, magazine-style designs, or card grids
  3. Precise placement - When items need to be placed in specific grid areas
  4. Overlapping content - When elements need to overlap or span multiple areas
  5. Layout-first design - When the layout structure is more important than content flow

I choose Flexbox when I need one-dimensional layouts, content-driven sizing, or simple alignment tasks."

Q: "How do you make CSS Grid responsive?"

A: "I make CSS Grid responsive by:

  1. Using responsive grid-cols utilities - grid-cols-1 md:grid-cols-2 lg:grid-cols-3
  2. Responsive spanning - col-span-2 lg:col-span-1 to change how items span
  3. Responsive gaps - gap-2 md:gap-4 lg:gap-6 for appropriate spacing
  4. Auto-fit/auto-fill - For dynamic column counts based on available space
  5. Responsive grid areas - Changing item placement at different breakpoints
  6. Mobile-first approach - Starting with single column and enhancing for larger screens"

Q: "How do you handle grid items with varying content lengths?"

A: "For varying content lengths, I:

  1. Use min-height - Set minimum heights for consistency when needed
  2. Embrace natural flow - Let content determine height for masonry-like effects
  3. Use align-items - Control vertical alignment within grid cells
  4. Consider subgrid - For nested grids that need to align with parent grid
  5. Responsive design - Adjust layout at different breakpoints to accommodate content
  6. Content strategy - Work with content creators to establish length guidelines"

Challenge Answers

Answer 1: Photo Gallery

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Photo Gallery</title>
    <script src="https://cdn.tailwindcss.com"></script>
  </head>
  <body class="bg-gray-100 p-4">
    <div class="max-w-6xl mx-auto">
      <h1 class="text-3xl font-bold text-center mb-8">Photo Gallery</h1>

      <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
        <!-- Featured Photo (spans 2 columns on desktop) -->
        <div class="lg:col-span-2 lg:row-span-2 group cursor-pointer">
          <div
            class="relative overflow-hidden rounded-lg shadow-lg h-64 lg:h-full bg-gradient-to-br from-blue-400 to-purple-500 transition-transform group-hover:scale-105"
          >
            <div
              class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
            ></div>
            <div class="absolute bottom-4 left-4 text-white">
              <h3 class="text-xl font-bold">Featured Photo</h3>
              <p class="text-sm opacity-90">Beautiful landscape</p>
            </div>
          </div>
        </div>

        <!-- Regular Photos -->
        <div class="group cursor-pointer">
          <div
            class="relative overflow-hidden rounded-lg shadow-lg h-64 bg-gradient-to-br from-green-400 to-blue-500 transition-transform group-hover:scale-105"
          >
            <div
              class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
            ></div>
            <div class="absolute bottom-4 left-4 text-white">
              <h3 class="text-lg font-semibold">Photo 1</h3>
            </div>
          </div>
        </div>

        <div class="group cursor-pointer">
          <div
            class="relative overflow-hidden rounded-lg shadow-lg h-64 bg-gradient-to-br from-red-400 to-pink-500 transition-transform group-hover:scale-105"
          >
            <div
              class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
            ></div>
            <div class="absolute bottom-4 left-4 text-white">
              <h3 class="text-lg font-semibold">Photo 2</h3>
            </div>
          </div>
        </div>

        <div class="group cursor-pointer">
          <div
            class="relative overflow-hidden rounded-lg shadow-lg h-64 bg-gradient-to-br from-yellow-400 to-orange-500 transition-transform group-hover:scale-105"
          >
            <div
              class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
            ></div>
            <div class="absolute bottom-4 left-4 text-white">
              <h3 class="text-lg font-semibold">Photo 3</h3>
            </div>
          </div>
        </div>

        <div class="group cursor-pointer">
          <div
            class="relative overflow-hidden rounded-lg shadow-lg h-64 bg-gradient-to-br from-purple-400 to-indigo-500 transition-transform group-hover:scale-105"
          >
            <div
              class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
            ></div>
            <div class="absolute bottom-4 left-4 text-white">
              <h3 class="text-lg font-semibold">Photo 4</h3>
            </div>
          </div>
        </div>

        <div class="group cursor-pointer">
          <div
            class="relative overflow-hidden rounded-lg shadow-lg h-64 bg-gradient-to-br from-teal-400 to-cyan-500 transition-transform group-hover:scale-105"
          >
            <div
              class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
            ></div>
            <div class="absolute bottom-4 left-4 text-white">
              <h3 class="text-lg font-semibold">Photo 5</h3>
            </div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

Answer 2: Dashboard Grid

html
<div class="min-h-screen bg-gray-100">
  <div class="grid grid-rows-[auto_1fr] lg:grid-cols-[250px_1fr] h-screen">
    <!-- Header -->
    <header class="lg:col-span-2 bg-white shadow-sm border-b p-4">
      <div class="flex items-center justify-between">
        <h1 class="text-2xl font-bold text-gray-900">Dashboard</h1>
        <div class="flex items-center space-x-4">
          <button class="p-2 rounded-lg bg-gray-100 hover:bg-gray-200">
            <svg
              class="w-5 h-5"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M15 17h5l-5 5-5-5h5v-12"
              ></path>
            </svg>
          </button>
          <div class="w-8 h-8 bg-blue-500 rounded-full"></div>
        </div>
      </div>
    </header>

    <!-- Sidebar (hidden on mobile) -->
    <aside class="hidden lg:block bg-white shadow-sm border-r">
      <nav class="p-4">
        <ul class="space-y-2">
          <li>
            <a
              href="#"
              class="flex items-center p-3 rounded-lg bg-blue-50 text-blue-700"
            >
              <span class="w-5 h-5 mr-3 bg-blue-500 rounded"></span>
              Overview
            </a>
          </li>
          <li>
            <a
              href="#"
              class="flex items-center p-3 rounded-lg text-gray-700 hover:bg-gray-50"
            >
              <span class="w-5 h-5 mr-3 bg-gray-400 rounded"></span>
              Analytics
            </a>
          </li>
          <li>
            <a
              href="#"
              class="flex items-center p-3 rounded-lg text-gray-700 hover:bg-gray-50"
            >
              <span class="w-5 h-5 mr-3 bg-gray-400 rounded"></span>
              Users
            </a>
          </li>
          <li>
            <a
              href="#"
              class="flex items-center p-3 rounded-lg text-gray-700 hover:bg-gray-50"
            >
              <span class="w-5 h-5 mr-3 bg-gray-400 rounded"></span>
              Settings
            </a>
          </li>
        </ul>
      </nav>
    </aside>

    <!-- Main Content -->
    <main class="overflow-y-auto p-4 lg:p-6">
      <!-- Stats Grid -->
      <div
        class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-2 xl:grid-cols-4 gap-4 lg:gap-6 mb-6"
      >
        <div class="bg-white p-6 rounded-lg shadow-sm border">
          <div class="flex items-center justify-between">
            <div>
              <p class="text-sm font-medium text-gray-600">Total Users</p>
              <p class="text-3xl font-bold text-gray-900">1,234</p>
            </div>
            <div
              class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center"
            >
              <div class="w-6 h-6 bg-blue-500 rounded"></div>
            </div>
          </div>
          <p class="text-sm text-green-600 mt-2">+12% from last month</p>
        </div>

        <div class="bg-white p-6 rounded-lg shadow-sm border">
          <div class="flex items-center justify-between">
            <div>
              <p class="text-sm font-medium text-gray-600">Revenue</p>
              <p class="text-3xl font-bold text-gray-900">$12,345</p>
            </div>
            <div
              class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center"
            >
              <div class="w-6 h-6 bg-green-500 rounded"></div>
            </div>
          </div>
          <p class="text-sm text-green-600 mt-2">+8% from last month</p>
        </div>

        <div class="bg-white p-6 rounded-lg shadow-sm border">
          <div class="flex items-center justify-between">
            <div>
              <p class="text-sm font-medium text-gray-600">Orders</p>
              <p class="text-3xl font-bold text-gray-900">567</p>
            </div>
            <div
              class="w-12 h-12 bg-yellow-100 rounded-lg flex items-center justify-center"
            >
              <div class="w-6 h-6 bg-yellow-500 rounded"></div>
            </div>
          </div>
          <p class="text-sm text-red-600 mt-2">-3% from last month</p>
        </div>

        <div class="bg-white p-6 rounded-lg shadow-sm border">
          <div class="flex items-center justify-between">
            <div>
              <p class="text-sm font-medium text-gray-600">Growth</p>
              <p class="text-3xl font-bold text-gray-900">+12%</p>
            </div>
            <div
              class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center"
            >
              <div class="w-6 h-6 bg-purple-500 rounded"></div>
            </div>
          </div>
          <p class="text-sm text-green-600 mt-2">+2% from last month</p>
        </div>
      </div>

      <!-- Chart Area -->
      <div class="bg-white p-6 rounded-lg shadow-sm border">
        <div class="flex items-center justify-between mb-6">
          <h2 class="text-xl font-semibold text-gray-900">
            Analytics Overview
          </h2>
          <select class="border border-gray-300 rounded-lg px-3 py-2 text-sm">
            <option>Last 7 days</option>
            <option>Last 30 days</option>
            <option>Last 90 days</option>
          </select>
        </div>
        <div
          class="h-64 lg:h-80 bg-gray-50 rounded-lg flex items-center justify-center"
        >
          <p class="text-gray-500">Chart visualization would go here</p>
        </div>
      </div>
    </main>
  </div>

  <!-- Mobile Bottom Navigation -->
  <nav
    class="lg:hidden fixed bottom-0 left-0 right-0 bg-white border-t border-gray-200"
  >
    <div class="grid grid-cols-4">
      <a href="#" class="flex flex-col items-center p-3 text-blue-600">
        <div class="w-6 h-6 bg-blue-500 rounded mb-1"></div>
        <span class="text-xs">Overview</span>
      </a>
      <a href="#" class="flex flex-col items-center p-3 text-gray-500">
        <div class="w-6 h-6 bg-gray-400 rounded mb-1"></div>
        <span class="text-xs">Analytics</span>
      </a>
      <a href="#" class="flex flex-col items-center p-3 text-gray-500">
        <div class="w-6 h-6 bg-gray-400 rounded mb-1"></div>
        <span class="text-xs">Users</span>
      </a>
      <a href="#" class="flex flex-col items-center p-3 text-gray-500">
        <div class="w-6 h-6 bg-gray-400 rounded mb-1"></div>
        <span class="text-xs">Settings</span>
      </a>
    </div>
  </nav>
</div>

Answer 3: Magazine Layout

html
<div class="max-w-7xl mx-auto p-4 lg:p-8">
  <header class="text-center mb-8">
    <h1 class="text-4xl lg:text-5xl font-bold text-gray-900 mb-2">
      Tech Magazine
    </h1>
    <p class="text-gray-600">Latest news and insights from the tech world</p>
  </header>

  <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
    <!-- Hero Article (spans 2 columns and 2 rows on desktop) -->
    <article
      class="md:col-span-2 lg:row-span-2 bg-white rounded-lg shadow-lg overflow-hidden group"
    >
      <div
        class="relative h-64 lg:h-80 bg-gradient-to-br from-blue-500 to-purple-600"
      >
        <div
          class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
        ></div>
        <div class="absolute top-4 left-4">
          <span
            class="bg-red-500 text-white px-3 py-1 rounded-full text-sm font-medium"
            >Featured</span
          >
        </div>
      </div>
      <div class="p-6">
        <h2
          class="text-2xl lg:text-3xl font-bold text-gray-900 mb-3 group-hover:text-blue-600 transition-colors"
        >
          The Future of Artificial Intelligence in Web Development
        </h2>
        <p class="text-gray-600 mb-4 leading-relaxed">
          Explore how AI is revolutionizing the way we build and design
          websites, from automated code generation to intelligent user
          experience optimization.
        </p>
        <div class="flex items-center justify-between">
          <div class="flex items-center">
            <div class="w-10 h-10 bg-gray-300 rounded-full mr-3"></div>
            <div>
              <p class="font-medium text-gray-900">Sarah Johnson</p>
              <p class="text-sm text-gray-500">Tech Editor</p>
            </div>
          </div>
          <span class="text-sm text-gray-500">5 min read</span>
        </div>
      </div>
    </article>

    <!-- Medium Article (spans 2 columns on mobile/tablet) -->
    <article
      class="md:col-span-2 lg:col-span-2 bg-white rounded-lg shadow-lg overflow-hidden group"
    >
      <div class="relative h-48 bg-gradient-to-br from-green-500 to-teal-600">
        <div
          class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
        ></div>
      </div>
      <div class="p-6">
        <h3
          class="text-xl font-bold text-gray-900 mb-2 group-hover:text-green-600 transition-colors"
        >
          10 Essential VS Code Extensions for 2024
        </h3>
        <p class="text-gray-600 mb-4">
          Boost your productivity with these must-have extensions that every
          developer should know about.
        </p>
        <div class="flex items-center justify-between">
          <div class="flex items-center">
            <div class="w-8 h-8 bg-gray-300 rounded-full mr-2"></div>
            <span class="text-sm font-medium text-gray-700">Mike Chen</span>
          </div>
          <span class="text-sm text-gray-500">3 min read</span>
        </div>
      </div>
    </article>

    <!-- Small Articles -->
    <article class="bg-white rounded-lg shadow-lg overflow-hidden group">
      <div class="relative h-32 bg-gradient-to-br from-red-500 to-pink-600">
        <div
          class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
        ></div>
      </div>
      <div class="p-4">
        <h4
          class="text-lg font-bold text-gray-900 mb-2 group-hover:text-red-600 transition-colors"
        >
          React 18 New Features
        </h4>
        <p class="text-gray-600 text-sm mb-3">
          Discover the latest features and improvements in React 18.
        </p>
        <div class="flex items-center">
          <div class="w-6 h-6 bg-gray-300 rounded-full mr-2"></div>
          <span class="text-xs text-gray-500">Alex Kim</span>
        </div>
      </div>
    </article>

    <article class="bg-white rounded-lg shadow-lg overflow-hidden group">
      <div
        class="relative h-32 bg-gradient-to-br from-yellow-500 to-orange-600"
      >
        <div
          class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
        ></div>
      </div>
      <div class="p-4">
        <h4
          class="text-lg font-bold text-gray-900 mb-2 group-hover:text-yellow-600 transition-colors"
        >
          CSS Grid vs Flexbox
        </h4>
        <p class="text-gray-600 text-sm mb-3">
          When to use CSS Grid and when to stick with Flexbox.
        </p>
        <div class="flex items-center">
          <div class="w-6 h-6 bg-gray-300 rounded-full mr-2"></div>
          <span class="text-xs text-gray-500">Emma Davis</span>
        </div>
      </div>
    </article>

    <article class="bg-white rounded-lg shadow-lg overflow-hidden group">
      <div
        class="relative h-32 bg-gradient-to-br from-purple-500 to-indigo-600"
      >
        <div
          class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
        ></div>
      </div>
      <div class="p-4">
        <h4
          class="text-lg font-bold text-gray-900 mb-2 group-hover:text-purple-600 transition-colors"
        >
          TypeScript Tips
        </h4>
        <p class="text-gray-600 text-sm mb-3">
          Advanced TypeScript patterns for better code quality.
        </p>
        <div class="flex items-center">
          <div class="w-6 h-6 bg-gray-300 rounded-full mr-2"></div>
          <span class="text-xs text-gray-500">David Wilson</span>
        </div>
      </div>
    </article>

    <article class="bg-white rounded-lg shadow-lg overflow-hidden group">
      <div class="relative h-32 bg-gradient-to-br from-teal-500 to-cyan-600">
        <div
          class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
        ></div>
      </div>
      <div class="p-4">
        <h4
          class="text-lg font-bold text-gray-900 mb-2 group-hover:text-teal-600 transition-colors"
        >
          Web Performance
        </h4>
        <p class="text-gray-600 text-sm mb-3">
          Optimize your website for lightning-fast loading times.
        </p>
        <div class="flex items-center">
          <div class="w-6 h-6 bg-gray-300 rounded-full mr-2"></div>
          <span class="text-xs text-gray-500">Lisa Zhang</span>
        </div>
      </div>
    </article>

    <!-- Wide Article (spans 2 columns) -->
    <article
      class="md:col-span-2 bg-white rounded-lg shadow-lg overflow-hidden group"
    >
      <div class="relative h-40 bg-gradient-to-br from-indigo-500 to-blue-600">
        <div
          class="absolute inset-0 bg-black bg-opacity-20 group-hover:bg-opacity-10 transition-all duration-300"
        ></div>
      </div>
      <div class="p-6">
        <h3
          class="text-xl font-bold text-gray-900 mb-2 group-hover:text-indigo-600 transition-colors"
        >
          Building Scalable Design Systems with Tailwind CSS
        </h3>
        <p class="text-gray-600 mb-4">
          Learn how to create maintainable and scalable design systems using
          Tailwind CSS and modern development practices.
        </p>
        <div class="flex items-center justify-between">
          <div class="flex items-center">
            <div class="w-8 h-8 bg-gray-300 rounded-full mr-2"></div>
            <span class="text-sm font-medium text-gray-700">Rachel Green</span>
          </div>
          <span class="text-sm text-gray-500">7 min read</span>
        </div>
      </div>
    </article>
  </div>
</div>
PreviousResponsive DesignNextPseudo-Classes and State-Based Styling

Open source, free forever. Built by iammhador.

Contribute on GitHub