The core difference: 1D vs 2D

Flexbox is for one-dimensional layouts โ€” either rows OR columns. Think of it as arranging items in a single line (that can wrap).

CSS Grid is for two-dimensional layouts โ€” rows AND columns at the same time. You define both axes and control where items go in a grid pattern.

Remember:
Flexbox = 1D (line), Grid = 2D (grid). Pick the tool that matches your layout shape.

Flexbox: Content-driven layout

Flexbox is content-driven. The items determine how much space they need, and Flexbox distributes space around them. It's perfect for:

  • Navigation bars โ€” horizontal links that grow/shrink
  • Button groups โ€” buttons side-by-side with gaps
  • Card internals โ€” content inside each card arranged horizontally
  • Flexible rows โ€” items that reflow when space changes
flexbox.html Navigation bar with Flexbox

Why Flexbox here? The navbar has one row of items that should stay aligned. Flexbox handles wrapping, centering, and spacing with minimal code.

Grid: Layout-driven design

CSS Grid is layout-driven. YOU define the grid structure (rows and columns), and items fill those cells. It's perfect for:

  • Page layouts โ€” header, sidebar, content, footer in a grid
  • Photo galleries โ€” items in a regular grid with equal spacing
  • Dashboards โ€” dashboard cards in a multi-row, multi-column layout
  • Form layouts โ€” labels and inputs aligned in columns
grid-layout.html Page layout with CSS Grid

Why Grid here? The page layout needs a predictable structure with multiple rows and columns. Grid lets you define the entire layout once.

Quick decision tree

๐Ÿ“ฑ
Use Flexbox For
Navigation Bar

Horizontal links that need to stay aligned. Items flow in one direction.

๐Ÿ“Š
Use Grid For
Dashboard Layout

Multiple cards in rows and columns. You define the grid structure upfront.

๐ŸŽด
Use Flexbox For
Card Row

Three cards in a row. Items wrap naturally without explicit positioning.

๐Ÿ–ผ๏ธ
Use Grid For
Photo Gallery

Images in a regular grid. Each item occupies the same-sized grid cell.

Scenario Flexbox or Grid? Why?
Navbar links Flexbox Single row that wraps. Content-driven.
Page layout (header, sidebar, footer) Grid Multiple rows and columns. Layout-driven.
Card internals (image, title, text) Flexbox Stack vertically in one column.
Image gallery Grid Regular grid of equal-sized items.
Horizontal button group Flexbox Buttons in a row with gaps.
Dashboard with widgets Grid Widgets positioned in a 2D grid.

Try it yourself: Flexbox layout

Below is a 3-card row built with Flexbox. Click Run to see the layout. Try changing the gap, adding more cards, or modifying flex properties.

โœ๏ธ Try It Yourself โ€” Flexbox Cards
Preview

Key takeaways: Each card uses flex: 1 to grow equally. The gap: 20px adds space between them. This layout is content-driven โ€” the cards grow/shrink based on available space.

Try it yourself: Grid layout

Here's the same 3-card layout, but built with CSS Grid. You'll see the code is different, but the visual result is similar. Compare the approaches!

โœ๏ธ Try It Yourself โ€” Grid Cards
Preview

Key takeaways: Grid uses grid-template-columns: 1fr 1fr 1fr to define three equal-width columns. You define the structure upfront. The visual result is nearly identical, but Grid is more explicit about the layout.

Use both together โ€” the best approach

Here's the secret: You don't have to choose. Use Grid for the overall page layout, and Flexbox for components inside the grid.

combined.html Grid for page layout, Flexbox for components

Why this works: Grid handles the big picture (page structure). Flexbox handles the details (navbar, card internals). This combination is powerful and flexible.

Challenge: E-commerce Page

Build a mini e-commerce page using both Grid and Flexbox:

  • Grid: Overall page layout โ€” header at top, sidebar on left, product grid in center, footer at bottom
  • Flexbox: Navbar links (horizontal) and product card internals (vertical stack)
  • Grid: Product grid โ€” 3 products per row, each product is a card

Start with the "Combining Both" example above. Then customize it with real content!

Hint: Use the combined example from Section 7 as your starting point. Replace the inline styles with a CSS section. Add more products and adjust grid-template-columns to fit them. Make the sidebar sticky so it stays visible while scrolling!

Key takeaways

  • Flexbox is 1D: Use it for rows or columns (navbars, card internals, button groups).
  • Grid is 2D: Use it for layouts with rows and columns (page layouts, photo galleries, dashboards).
  • Flexbox is content-driven: Items determine their size; Flexbox distributes space.
  • Grid is layout-driven: You define the grid structure; items fill the cells.
  • Use both together: Grid for the big picture, Flexbox for components. This is the most powerful approach.
  • No rule is absolute: You can use Flexbox for 2D layouts (with wrapping) and Grid for 1D layouts (single row). Pick what feels natural.

Test your knowledge

Question 1 of 5
Flexbox is best for ____ layout.
Question 2 of 5
CSS Grid is best for ____ layout.
Question 3 of 5
Which is better for a navigation bar?
Question 4 of 5
Which is better for a dashboard with multiple rows and columns of widgets?
Question 5 of 5
Can you use both Flexbox and Grid on the same page?
0
/5
Great work! You understand when to use Flexbox vs Grid.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered layout.
Next: responsive design on every device.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’