Skip to main content
Guide 8 min read

CSS Grid vs Flexbox: When to Use Each, With Layout Examples

You need to line up some boxes on a page. You reach for Flexbox, then you wonder if Grid would do it better. Then you read three tutorials that all disagree. The CSS Grid vs Flexbox question confuses people because both tools can build most layouts, so the choice feels like a coin flip. It isn’t. Each one has a job it does best, and once you see the pattern, you’ll pick the right one in seconds.

This guide explains how each tool thinks, when to use each one and how to combine them. You’ll get working code for six common layouts, a list of mistakes that waste hours and a quick checklist to settle the argument next time.

The short answer

Use Flexbox when you lay out items in a single line, either a row or a column. Use Grid when you lay out items in rows and columns at the same time. That’s the textbook rule, and it holds up well.

Here’s a second rule that helps when the first one doesn’t. Flexbox starts with the content and lets it decide the size. Grid starts with the layout and makes the content fit. If you know the shape you want before you know what goes in it, reach for Grid.

How Flexbox thinks

Flexbox works along one axis at a time. You pick a direction, and the items flow along it. The browser then shares out the free space, shrinks items that don’t fit and lets you line things up across the other axis.

It shines for pieces of a page that grow and shrink with their content, like a navigation bar, a row of buttons or a set of tags. You don’t tell each item where to go. You describe how they should behave as a group.

How Grid thinks

Grid works in two dimensions. You define columns and rows on the container, and the items drop into the cells. An item can also span several columns or rows, which Flexbox can’t do cleanly.

That makes Grid the better fit for whole page layouts, card galleries and anything with a clear structure, like a dashboard. You draw the shape first and place the content after.

Example 1: a navigation bar (Flexbox)

A logo on the left, links on the right, everything centered on the same line. That’s one row of items, so Flexbox fits:

.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

The gap property adds space between items without extra margins. Add a wrap setting on the container if the links should drop to a second line on narrow screens.

Example 2: a responsive card grid (Grid)

You want cards that fill the row, with as many columns as fit and no media queries. Grid does that in three lines:

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

Each card is at least 240 pixels wide. The browser fits as many as it can, then stretches them to fill the row. On a phone you get one column, on a laptop three or four. Try your own numbers in our CSS grid generator and copy the code.

You can build the same thing with Flexbox and a wrap setting, but the last row then stretches its cards to fill the space. Grid keeps every card the same width. That’s the clearest case where Grid wins.

Example 3: a full page layout (Grid)

Header on top, sidebar on the left, main content in the middle, footer at the bottom. Named areas make the code read like a sketch:

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  min-height: 100vh;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

@media (max-width: 700px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto auto;
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "footer";
  }
}

Look at what the media query does. It doesn’t touch the items. It only redraws the map. That’s the real power of Grid, because the markup stays the same while the whole layout changes.

Example 4: pin a button to the bottom of a card (Flexbox)

Cards in a row have different amounts of text. You want every button on the same baseline at the bottom. Inside each card, you have one column of items, so Flexbox fits again:

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

.card .actions {
  margin-top: auto;
}

The auto margin soaks up all the free space above the button and pushes it down. It’s a small trick that saves a lot of fiddling with fixed heights.

Example 5: center something both ways (either)

Here both tools work, and Grid is shorter:

.center {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

With Flexbox you write two lines, one to center across and one to center along the axis. Both are fine. Use whichever your team already reads faster.

Example 6: use both together

The question “Grid or Flexbox?” hides a trick. You don’t have to choose one for the whole page. Most real layouts use both, and nesting them is normal.

The page in Example 3 uses Grid for the big structure. Inside the header, Flexbox lays out the logo and links. Inside the main area, Grid builds the card gallery from Example 2. And inside each card, Flexbox pins the button down. Grid handles the outer shape, and Flexbox handles the small groups of items inside it.

Once the layout works, you’ll want to style it. Our CSS gradient generator and color picker converter help with card backgrounds and borders.

How to decide in ten seconds

Ask these questions in order:

  • Do the items need to line up in both rows and columns? Use Grid.
  • Do you know the shape of the layout before you know the content? Use Grid.
  • Does an item need to span several columns or rows? Use Grid.
  • Are the items in one line, and should their content decide the size? Use Flexbox.
  • Do you need to push one item away from the others, like a button to the bottom? Use Flexbox.
  • Is it a toolbar, a menu, a tag list or a row of buttons? Use Flexbox.

Still stuck? Start with Grid for the outer layout and Flexbox for the pieces inside it. That default works more often than not.

Mistakes that waste hours

These slips cause most layout bugs:

  • Using Flexbox for a two dimensional layout, then fighting it with fixed widths and extra wrapper elements.
  • Forgetting that a flex item won’t shrink below its content. A long word or a wide image makes the page overflow. Set the minimum width property to 0 on the item so it can shrink.
  • Using margins for spacing between items when the gap property does the job more cleanly.
  • Setting display on the wrong element. The display value goes on the parent, and the children become the items.
  • Putting a fixed height on a container and then wondering why the content spills out.
  • Nesting a grid inside a grid when a single grid with spanning items would do.
  • Writing a media query for every screen size when auto fit and wrap settings handle it.

Debug a layout that won’t behave

Open your browser’s developer tools and look for the small flex or grid badge next to an element in the inspector. Click it, and the browser draws the grid lines or flex areas right on the page. You’ll see at once where the columns are, where the gaps sit and which item pushes its neighbors around.

Test your layout at a narrow width too. Resize the window slowly and watch for the point where something overflows. Before you ship, you can squeeze the stylesheet down with our CSS minifier, and show your layout code on a page safely with our HTML encoder decoder. Changed a stylesheet and something broke? Compare the old and new versions in our text diff checker.

What about browser support?

Both tools work in all current major browsers, so you don’t need fallbacks for most projects. If you must support very old browsers, check a support table before you commit, and test on the devices your visitors use.

A checklist before you build

Run through this list once:

  1. Sketch the layout and note the rows and columns.
  2. Pick Grid for the outer structure.
  3. Pick Flexbox for single lines of items inside it.
  4. Use the gap property for spacing, not margins.
  5. Use auto fit and a minimum width for card galleries.
  6. Redraw the grid areas in a media query instead of moving items.
  7. Set a zero minimum width on flex items that hold long text.
  8. Check the layout in the browser’s grid and flex overlay at narrow widths.

The short version

Use Grid when you lay out rows and columns together, or when you know the shape before the content. Use Flexbox for a single line of items, where the content sets the size. Combine them: Grid for the page, Flexbox for the parts inside it. Use the gap property, shrink flex items with a zero minimum width and let auto fit handle responsive cards.

Pick one layout on your site that feels clumsy and rebuild it with the rule above today. Ten minutes in the grid generator will show you how little code you need.

Free browser based tools on Black Claaw to build grids, style your layout, shrink your stylesheet and compare changes.

View all tools

Advertisement