Design Tutorials & Tips

CSS Flexbox vs Grid: When to Use Each (With Simple Examples)

Flexbox and CSS Grid both build layouts, but they shine in different situations. Here's when to use each, with copy-paste examples.

In this article · 7 sections
  1. The one-line difference
  2. Flexbox examples
  3. Navigation bar
  4. Centre anything
  5. Button group that wraps on mobile
  6. Grid examples
  7. Responsive card grid (no media queries)
  8. Page layout with sidebar
  9. Named areas
  10. Use them together
  11. Quick decision table
  12. Common mistakes
  13. FAQs
  14. Is CSS Grid supported on all browsers?
  15. Should I use Bootstrap's grid instead?

Modern CSS has two powerful layout systems: Flexbox and CSS Grid. Beginners often ask which one to learn. The answer: both, because they solve different problems. Here's a practical guide to CSS Flexbox vs Grid.

The one-line difference

  • Flexbox is one-dimensional: it arranges items in a row or a column.
  • Grid is two-dimensional: it arranges items in rows and columns at the same time.

Flexbox examples

Navigation bar

.nav {
  display: flex;
  justify-content: space-between; /* logo left, menu right */
  align-items: center;
  gap: 16px;
}

Centre anything

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

Button group that wraps on mobile

.actions { display: flex; flex-wrap: wrap; gap: 12px; }

Use Flexbox for: navbars, button groups, aligning items in a card, centring, small components.

Grid examples

Responsive card grid (no media queries)

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 24px;
}

Cards automatically form 1, 2, 3 or 4 columns depending on screen width.

Page layout with sidebar

.page {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 32px;
}
@media (max-width: 900px) {
  .page { grid-template-columns: 1fr; }
}

Named areas

.layout {
  display: grid;
  grid-template-areas:
    "header header"
    "main   aside"
    "footer footer";
}
header { grid-area: header; }

Use Grid for: card grids, galleries, page layouts, dashboards, anything with rows and columns.

Use them together

A common pattern: Grid for the overall card layout, Flexbox inside each card to push the button to the bottom.

.card { display: flex; flex-direction: column; }
.card .btn { margin-top: auto; }

Quick decision table

Task Use
Items in a single row/column Flexbox
Equal cards in rows Grid
Centring one element Either (Flexbox is common)
Full page layout Grid
Content-sized items that wrap Flexbox

Common mistakes

  • Using Flexbox with fixed widths for card grids (uneven last rows); Grid handles this better
  • Forgetting gap; it's cleaner than margins between items
  • Not checking layouts at 360 px width on phones

For layout principles beyond code, see website grid and layout basics.

FAQs

Is CSS Grid supported on all browsers?

Yes, all modern browsers support Grid and Flexbox.

Should I use Bootstrap's grid instead?

Bootstrap's grid uses Flexbox under the hood. Native CSS Grid gives more control with less code. See Tailwind vs Bootstrap.

Joboo Web builds responsive layouts tested on real phones. See what we can build.

Share this article

Joboo Web

Web design, SEO & digital marketing team · Ludhiana, Punjab

Joboo Web has helped Indian businesses grow online since 2019 — building fast websites and running SEO, Google Ads and social campaigns for clients in 80+ cities. About us →

Keep reading

💬