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.
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
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
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
Horizontal links that need to stay aligned. Items flow in one direction.
Multiple cards in rows and columns. You define the grid structure upfront.
Three cards in a row. Items wrap naturally without explicit positioning.
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.
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!
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.
Why this works: Grid handles the big picture (page structure). Flexbox handles the details (navbar, card internals). This combination is powerful and flexible.
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
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 โ