LEVEL 2 • LESSON 6 OF 6
0% Complete
← Back to Learning Path

📚 Lesson 6: Grid vs Flexbox

Learn when to use Grid, when to use Flexbox, and how they work together.

The Question Everyone Asks

"Should I use Grid or Flexbox?" The answer: Both! They're not competitors - they're teammates. Each solves different problems.

The Simple Rule

Quick Decision Framework:

  • Use Grid: When you need to control rows AND columns (2D layout)
  • Use Flexbox: When arranging items in a single direction (1D layout)

Grid: Two-Dimensional

Grid excels at layouts where you need to control both axes at once.

/* Grid: Controls rows AND columns */
.layout {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: 60px 1fr 40px;
}

Perfect for: Page layouts, dashboards, card grids, complex UIs

Flexbox: One-Dimensional

Flexbox is perfect for arranging items in a row OR column (not both).

/* Flexbox: Controls one direction */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Perfect for: Navigation bars, button groups, centering, toolbars

Real-World Examples

Example 1: Navigation Bar

Use Flexbox - items in a single row

.nav {
  display: flex;
  gap: 20px;
  align-items: center;
}

Example 2: Page Layout

Use Grid - header, sidebar, content, footer

.page {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 40px;
}

Example 3: Card Content

Use Flexbox - stacking elements vertically

.card {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

Example 4: Photo Gallery

Use Grid - rows and columns of images

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

Using Them Together

The real power comes from combining them!

/* Grid for overall layout */
.page {
  display: grid;
  grid-template-columns: 200px 1fr;
}

/* Flexbox for navigation inside grid */
.nav {
  display: flex;
  align-items: center;
  gap: 20px;
}

/* Grid for card layout inside grid */
.content {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;
}

/* Flexbox for card content */
.card {
  display: flex;
  flex-direction: column;
}

Decision Flowchart

Ask yourself:

  1. 1. Do I need to control rows AND columns? → Use Grid
  2. 2. Am I arranging items in a single row OR column? → Use Flexbox
  3. 3. Do I need the layout to be responsive with wrapping? → Use Grid (auto-fit)
  4. 4. Am I just centering one item? → Use Flexbox (simpler)

Common Scenarios

Scenario Best Choice
Navigation barFlexbox
Page layoutGrid
Button groupFlexbox
Photo galleryGrid
Centering contentFlexbox
Dashboard panelsGrid
Form fieldsFlexbox
Card gridGrid

The Truth

Don't overthink it! Start with whichever feels natural, and switch if it becomes difficult. Both can achieve similar results - Grid just makes 2D layouts easier, and Flexbox makes 1D layouts simpler.

✅ Remember:

Grid and Flexbox work together, not against each other. Use Grid for the big picture, Flexbox for the details. Most real websites use both!

🎯 Practice Time!

Look at a website you visit daily. Can you identify where Grid vs Flexbox would be best? Try recreating a section in the generator!

Open Generator →

📝 Quick Check (3 Questions)

1. What's the main difference between Grid and Flexbox?

2. Which should you use for a navigation bar?

3. Can you use Grid and Flexbox together in the same project?

← Previous: Lesson 5