All articles
165 articles · updated weekly See our Tools
All articles
All

CSS Grid vs Flexbox: when to use each one

Grid is two-dimensional, Flexbox is one-dimensional. The right choice depends on what the layout is trying to do — not personal preference.

CSS Grid vs Flexbox: when to use each one
COVER · All

Every project starts the same way: someone uses Flexbox for everything. It works until the layout with a fixed sidebar, a header and a main area that has to adjust its height shows up, and then it turns into a pile of flex-wrap, align-self and min-height that nobody can maintain anymore. The question that should have been asked first: is this layout a row or a grid?

The answer is the dividing line between Flexbox and CSS Grid. This post shows the difference in a table, the cases where each one wins, how to combine them and where the auto-fill versus auto-fit trap is. The layout behaviors quoted below were measured in a Chromium browser (Edge 157) with the post's own rules.

The rule that sums it up: one direction or two

Flexbox is a one-dimensional layout model: it distributes items along one direction, row or column. Grid is two-dimensional: it places items in rows and columns at the same time.

That's not spec marketing. With Flexbox, each line that flex-wrap creates is independent of the others: an item on the top line has no way to "know" the width of the item below it. With Grid, you define the tracks (columns and rows) and the items fit into them; a cell in row 2 lines up with the one in row 1 because both belong to the same column.

Another way to see it, and the one I use most when deciding: in Flexbox the content drives the size (each item takes what it needs and the leftover space is distributed); in Grid the track drives (you draw the structure and the content fits into it).

Flexbox vs Grid, side by side

Flexbox CSS Grid
Dimensions One (row or column) Two (rows and columns)
What sets the size Each item's content The tracks declared on the container
Alignment across lines None: each wrapped line is independent Built in: items in the same column line up
Key properties flex-direction, justify-content, align-items, flex-wrap, flex grid-template-columns/rows, grid-template-areas, grid-column, repeat(), minmax()
Typical cases Navbar, icon button, tag list, centering, header with items at both ends Page layout, card grid, gallery, dashboard, multi-column form
Responsive without media queries flex-wrap + flex-basis repeat(auto-fill, minmax(...))

gap works in both. And display: flex and display: grid are mutually exclusive on the same element, but an item can be a Grid child and a Flexbox parent at the same time, which is what you'll see further down.

When to use Flexbox

Flexbox handles components where the flow is linear and the content decides the space: navigation bars, a button with an icon and text, tag lists, a card with stacked content, centering an element, spreading items with space-between.

/* center vertically and horizontally */
.card-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* button with icon */
.btn {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* header with logo and nav at opposite ends */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
}

The sign Flexbox is the right choice: you're thinking in one direction only, and items can have different sizes without a problem. The sign you're forcing it: you need items on different lines to align with each other, and you've started giving them width: 33.333% to fake columns. That's Grid's job.

When to use CSS Grid

Grid is for layouts where several items need to align on both axes, where the grid matters and not just the flow: page layout, card grids, galleries, dashboards, forms with labels and fields in columns.

/* classic application layout */
.app {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 240px 1fr;
  grid-template-rows: 64px 1fr 48px;
  min-height: 100vh;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }
/* responsive card grid with no media query */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}

The second example is one of the most useful tricks in modern CSS: repeat() with minmax(280px, 1fr) creates as many columns of at least 280px as fit, without a single @media. The choice between auto-fill and auto-fit there isn't a detail, and it deserves its own section.

auto-fill or auto-fit: the difference shows with few items

Both keywords create the same number of tracks: as many as fit in the container. The difference is what happens to the tracks that end up empty. With auto-fill, they keep existing and taking up space. With auto-fit, empty tracks collapse to zero and the leftover space goes to the columns that have items, because of the 1fr.

I measured it in a 1200px container with minmax(280px, 1fr) and gap: 24px:

2 cards, auto-fill -> tracks: 282px 282px 282px 282px  (cards at 282px, two empty tracks)
2 cards, auto-fit  -> tracks: 588px 588px 0px 0px      (cards stretched to fill the row)
6 cards, either one -> all at 282px

When there are enough items to fill the row, both behave the same. The choice only matters when few items are left. I use auto-fill for card grids whose count varies (search results, a filtered catalog): a card keeps the same width with 2 or 20 results, and a single result doesn't become a 1200px strip. auto-fit is the choice when the intent is exactly that a few items fill the whole row, like two or three highlights on a home page.

Combining both: the real-world approach

The most common thing in real projects isn't picking one. It's Grid for the macro structure and Flexbox inside the components:

/* Grid defines the page structure */
.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 64px 1fr;
  height: 100vh;
}

/* Flexbox arranges the header, which spans the whole Grid row */
.app-header {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}

/* Flexbox inside the card */
.card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto; /* pushes the footer to the bottom of the card */
}

Grid doesn't know, and doesn't need to know, how the header arranges its items. Flexbox doesn't know the header spans grid-column: 1 / -1. Each does what it does best. How to take this pattern responsive, with minmax(), clamp() and container queries instead of media queries, is covered in responsive layouts without frameworks.

Why not use only Grid?

After learning Grid, the opposite temptation shows up. The problem is that Grid is more verbose for linear flow and, written the naive way, doesn't even do the same thing. A button with an icon, in the version I see most:

.btn {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 8px;
}

In a 300px button, the text no longer takes only what it needs: the 1fr track stretches the <span> to 260px, which changes the click area, underline and any background applied to it. The honest Flexbox equivalent is this:

.btn {
  display: grid;
  grid-auto-flow: column;
  justify-content: start;
  align-items: center;
  gap: 8px;
}

grid-auto-flow: column puts the children side by side and justify-content: start keeps the auto tracks from stretching. I measured both: this version gives the text the same 38px Flexbox does. It works, but it's four declarations to say "one item next to the other", and the intent is less clear than display: flex. Grid is also less natural when the number of items varies and each should take its own content's space, like a tag list: flex-wrap does that with no tracks at all.

subgrid and browser support

Flexbox and Grid have been Baseline widely available for years (Flexbox since 2018, Grid since 2020, according to the web-features data behind MDN's Baseline). IE11, the last excuse to avoid Grid, was retired by Microsoft in June 2022.

The newer feature worth knowing is subgrid, which lets a grid item reuse its parent's tracks instead of creating its own grid. Firefox has supported it since version 71 (2019), Safari since 16 (2022) and Chrome since 117, in September 2023, when it became Baseline. Per the web-features data, it became "widely available" in March 2026. Check the current table on caniuse before relying on it for an audience with older browsers.

.card-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;
}

.card {
  display: grid;
  grid-row: span 3;              /* each card takes 3 of the parent's rows */
  grid-template-rows: subgrid;   /* title, text and footer use those rows */
  row-gap: 8px;                  /* without this, it inherits the parent's 24px gap */
}

The parent doesn't declare grid-template-rows: each card's three rows are created implicitly by span 3, and each one takes the height of the tallest content in that row across neighboring cards. Measured with three cards whose titles have different lengths: all three footers at the same height, with no JavaScript and no fixed height. In the version with Flexbox inside each card, the footers landed at three different heights.

Frequently asked questions

What is the difference between Flexbox and Grid?

Flexbox arranges items in one direction (row or column), and the size comes from each item's content. Grid arranges them in rows and columns at the same time, and the size comes from the tracks you declare on the container. That's why Grid aligns items across different lines and Flexbox doesn't.

Can I use Flexbox and Grid on the same element?

No: display: flex and display: grid are mutually exclusive. But an element can be a Grid child and a Flexbox parent at the same time, which is exactly the combination pattern: Grid on the outer container, Flexbox in the components.

Why doesn't my Grid align items the way I expected?

The most common cause is not declaring tracks: with just display: grid and no grid-template-columns, the browser creates a single column and stacks everything. The second is expecting gap to add space at the edges; it only adds space between cells.

When should I use auto-fill and when auto-fit?

It only makes a difference when there are fewer items than columns that fit. auto-fill keeps the empty columns and the items at their minimum width; auto-fit collapses the empty ones and stretches the items to fill the row. For a card grid with a varying count, auto-fill; for a few highlights that should take the whole row, auto-fit.

The choice is about intent, not preference

The question is never which one is better, but what the layout is trying to do. Distributing items in one direction, with spacing and alignment: Flexbox. Placing elements on a grid with control over rows and columns: Grid. Structuring the whole page: Grid for the macro, Flexbox for the components. The confusion starts when Flexbox does Grid's job, with percentage widths and flex-wrap where grid-template-columns would solve it in one line. The semantics of the areas (header, main, aside, footer) you place with Grid are covered in semantic HTML.

The editorial content ends here. To prototype grid-template-areas and columns while seeing the result and the CSS at the same time, I use the Quick Tools CSS Grid generator; for the inline components, the CSS Flexbox generator.

RD
Author
Rafael Duarte
Backend developer with a background in fintech and B2B SaaS — worked on teams that scaled APIs from zero to millions of requests. Carries enough production scars to hold strong opinions on tools, patterns and architecture decisions. Not an academic: he read the UUID RFC when he had to choose between v4 and v7 for a write-heavy table.
View profile