Flexbox vs Grid: When to Use Each

Flexbox vs grid is a question about dimensions. Flexbox arranges items along one line, either a row or a column. Grid arranges items in two dimensions, rows and columns together. Pick flexbox when you only care about one direction, and grid when you care about both.

Why this question exists

Both tools can put boxes side by side, so beginners often wonder why CSS needs two of them. Here's a simple example. Three cards in a row:

<div class="cards">
  <div class="card">One</div>
  <div class="card">Two</div>
  <div class="card">Three</div>
</div>
/* flexbox */
.cards { display: flex; gap: 16px; }

/* grid */
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }

Both versions look almost the same. The difference only shows up when the layout gets more complicated, and that's what interviewers want you to understand.

The idea: one dimension or two

Flexbox is a one-dimensional layout, grid is two-dimensional

Think of a shelf and a chessboard.

  • A shelf is one line. You put things on it, and you decide how they're spaced and aligned along that line. That's flexbox.
  • A chessboard has rows and columns, and every square belongs to both. You can say "this piece goes in column 3, row 2". That's grid.

MDN describes the same split: flexbox is a one-dimensional layout method and grid is a two-dimensional one.

How flexbox thinks: content out

With flexbox, you give the container display: flex, and the items line up along the main axis (a row by default). The items decide how much room they need, and flexbox shares out the leftover space.

.nav {
  display: flex;
  justify-content: space-between; /* spread along the main axis */
  align-items: center;            /* center on the cross axis */
  gap: 12px;
}

This is called working from the content out: the size of your content drives the layout. That's perfect when you don't know how wide each item will be, like menu links with different text lengths.

How grid thinks: layout in

With grid, you describe the tracks (the rows and columns) first, and then items drop into them.

.page {
  display: grid;
  grid-template-columns: 240px 1fr;       /* sidebar + main */
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 16px;
}

This is working from the layout in: you draw the board, and the content fits inside it. That's perfect for page layouts, where the structure matters more than the content.

The key difference: what happens when items wrap

This is the example that makes the difference click. Say you have a list of cards of different widths and you turn on wrapping:

.cards { display: flex; flex-wrap: wrap; gap: 16px; }

Flexbox wrapped lines are independent, grid aligns rows and columns

When flex items wrap, each new row becomes its own flex line. Each line sizes its items on its own, so the items in line 2 don't line up with the items in line 1. Often the last row ends up with a few items stretched or left floating.

Grid doesn't have that problem, because the columns belong to the whole grid, not to one row:

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

Now every card in every row snaps to the same column lines.

How to choose

Decision flow for choosing flexbox or grid

Ask one question: do I need to control rows and columns together?

  • No, just one direction → flexbox.
  • Yes, both → grid.
SituationBetter tool
Navigation bar, toolbar, button groupFlexbox
Centering one itemEither (flexbox is the shorter one)
Items of unknown size in a lineFlexbox
Full page layout with header, sidebar, footerGrid
Gallery where cards must line up in rows and columnsGrid
Dashboard with items spanning several rows or columnsGrid
Space between a card's title and its buttonFlexbox

Using them together

You don't have to choose one for the whole page. They work well as a team, and they use the same alignment properties (justify-content, align-items, gap), so what you learn for one carries over to the other.

A common pattern: grid for the outer layout, flexbox for the inside of each piece.

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

.card {
  display: flex;
  flex-direction: column;
}

.card .actions {
  margin-top: auto; /* pushes the buttons to the bottom of every card */
}

The grid places the cards in rows and columns. Inside each card, flexbox stacks the content and pins the buttons to the bottom.

Spacing with gap

Both layouts support the gap property. It adds space between items only, not around the outside, so you don't need tricks like "no margin on the last child".

.row  { display: flex; gap: 8px; }
.grid { display: grid; gap: 8px 16px; } /* row gap, then column gap */

Examples

1. A card list with an uneven last row

.cards { display: flex; flex-wrap: wrap; gap: 16px; }
.card  { flex: 1 1 200px; }

Problem: the last row has one card, and it stretches across the full width.

Why: each flex line shares out its own leftover space, so it doesn't know about the columns above it.

Fix: switch the container to grid with repeat(auto-fit, minmax(200px, 1fr)), or use auto-fill if you don't want the last items to stretch.

2. A navigation bar built with grid

.nav { display: grid; grid-template-columns: repeat(4, 100px); }

Problem: the menu has a fixed number of columns and fixed widths, so a longer label gets cut off or a fifth link breaks the layout.

Why: a nav is a single line whose items have unknown sizes, and that is a one-dimensional job.

Fix: use display: flex with gap, and let each link be as wide as its text.

3. Page layout built with nested flex rows

.page    { display: flex; }
.sidebar { width: 240px; }
.main    { flex: 1; }

Problem: adding a header and footer that span the full width needs extra wrapper div elements.

Why: flexbox only controls one direction at a time, so you have to nest containers to get a second one.

Fix: use grid-template-areas on a single container, as in the page layout above.

Interview answer

Flexbox is a one-dimensional layout system: it arranges items along a row or a column and sizes things from the content out. Grid is two-dimensional: you define rows and columns first, and items are placed into that structure. I use flexbox for components like navbars and toolbars, and grid for page layouts and galleries where items must line up in both directions. They also combine well, for example a grid of cards where each card uses flexbox internally.

Summary

  • Flexbox is one-dimensional: a single row or column.
  • Grid is two-dimensional: rows and columns together.
  • Flexbox works from the content out. Grid works from the layout in.
  • Wrapped flex items form independent lines, so they don't line up with the rows above.
  • Grid columns belong to the whole grid, so every row aligns.
  • Use flexbox for navbars, toolbars, button groups and small components.
  • Use grid for page layouts, galleries and dashboards.
  • If you're disabling flexbox's flexibility to force alignment, try grid.
  • Both support gap and the same alignment properties.
  • Use both together: grid outside, flexbox inside.