CSS Grid Layout vs Flexbox: Which One Fits Your Layout?

Every front-end developer hits the same fork in the road: you open a blank stylesheet, you know what the design should look like, and you have to decide between display: flex and display: grid. Both are modern, both are supported everywhere that matters, and both can produce a three-column card section. So the real question is not which one is “better”, it is which one makes your CSS shorter, more predictable and easier to maintain six months from now.

This guide compares CSS Grid Layout vs Flexbox across the five dimensions that actually change your code: one-dimensional vs two-dimensional thinking, content-first vs layout-first logic, alignment, responsiveness and browser support. Then we build the exact same card component twice, once with each tool, so you can see the difference instead of just reading about it.

Short answer: when to use Flexbox and when to use CSS Grid

Use Flexbox when you are arranging items along a single line, a row or a column, and you want the content to decide how much space it takes: navigation bars, button groups, toolbars, a label next to an icon, a card’s internal stack. Use CSS Grid when you are defining a structure in two directions at once, rows and columns together: page layouts, dashboards, galleries, pricing tables, anything where items must line up across both axes. In real projects you use both, Grid for the skeleton of the page and Flexbox for the contents of each box.

css grid layout

What is Flexbox (and what is “flex” in CSS)?

Flexbox, officially the CSS Flexible Box Layout module, is a layout mode you activate with display: flex on a parent element. That parent becomes a flex container and its direct children become flex items, laid out along a main axis (horizontal by default, vertical if you set flex-direction: column) and a cross axis perpendicular to it. A comparable breakdown sits on believemy.com.

The flex property itself is a shorthand on the children for three values: flex-grow (how much an item expands when there is spare space), flex-shrink (how much it compresses when there is not enough) and flex-basis (its starting size). Writing flex: 1 1 280px means “start at 280px, grow if you can, shrink if you must”. That sentence is the whole philosophy of Flexbox: the items negotiate space between themselves.

What is CSS Grid?

CSS Grid Layout is a two-dimensional layout system activated with display: grid. Instead of letting items negotiate, you draw the structure first: you declare columns with grid-template-columns, rows with grid-template-rows, and the children drop into the cells you created. You can also place an item explicitly on specific lines with grid-column and grid-row, or name entire areas with grid-template-areas and reference them by name.

Grid introduced the fr unit (a fraction of the free space), the repeat() function, the minmax() function and the auto-fit / auto-fill keywords. Those four tools together are what let a grid reflow responsively without a single media query.

css grid layout

CSS Grid Layout vs Flexbox: the comparison table

Dimension Flexbox CSS Grid
Dimensions One dimension: a row or a column. Wrapped lines are independent of each other. Two dimensions: rows and columns controlled at the same time, cells align across both axes.
Approach Content-first. The size of the content influences the layout. Layout-first. You define the tracks, the content fits into them.
Typical use Navbars, button groups, toolbars, tag lists, card internals, form rows, media objects. Page skeletons, dashboards, galleries, pricing tables, magazine and editorial layouts.
Alignment justify-content on the main axis, align-items / align-self on the cross axis, margin-left: auto for push tricks. Same box-alignment properties, plus justify-items and place-items, applied per cell and per track.
Responsiveness flex-wrap: wrap plus a flex-basis: items reflow, but the last line often looks uneven. repeat(auto-fit, minmax(280px, 1fr)): the column count adapts with no media query and rows stay aligned.
Overlap and layering Not supported natively, you need absolute positioning. Native: two items placed in the same cell overlap and can be stacked with z-index.
Gaps gap works, widely supported in current browsers. gap, row-gap and column-gap, supported since the start.
Browser support Supported in every current browser, including older mobile versions. The safest of the two for very old devices. Supported in every current browser since 2017. Subgrid is now available in current Chrome, Edge, Firefox and Safari.
Learning curve Shorter: roughly a dozen properties to memorise. Longer: tracks, lines, areas, implicit vs explicit grid, but far more expressive.

1D vs 2D: the distinction that decides almost everything

This is the rule that survives every other debate. Ask yourself: do I need the items on line 2 to line up with the items on line 1? If yes, that is a grid. If each line can live its own life, that is Flexbox.

A wrapped flex container does not know that it created multiple lines of a shared structure. It just wraps. That is why the famous “last row with two stretched cards” problem exists in Flexbox and does not exist in Grid. Much the same conclusion turns up on locionic.com.

Content-first vs layout-first

  • Flexbox is content-first: you do not know (or do not care) how wide each item is, and you let the browser distribute the remaining space. Perfect for a menu where labels have different lengths.
  • Grid is layout-first: you decide the structure up front, and the content adapts. Perfect when the design mockup already shows a clear column system.

Alignment: a tie, with one nuance

Both modules use the same box-alignment properties, so align-items: center behaves consistently in each. The nuance is scope: in Flexbox you align items along a line, in Grid you align items inside their cell and you can also align the tracks themselves inside the container. For centring one element inside another, both display: flex; place-items: center and display: grid; place-items: center work; the grid version is marginally shorter because it needs no justify-content.

Responsiveness without media queries

Grid has the stronger one-liner:

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

That single declaration produces four columns on a wide screen, two on a tablet and one on a phone, with every row perfectly aligned. Use auto-fill instead of auto-fit if you want empty tracks to be preserved rather than collapsed. hubspot.com has a solid rundown on this.

Flexbox can do something similar with flex-wrap and a flex-basis, but the final line stretches to fill the width, which is sometimes exactly what you want (a tag cloud) and sometimes not (a product grid).

Browser support in 2026

Both layout modes are baseline technology today. Flexbox and Grid work in all current versions of Chrome, Edge, Firefox and Safari, desktop and mobile. Newer additions deserve a quick check before you ship:

  • Subgrid: available in current Chrome, Edge, Firefox and Safari. Safe for progressive enhancement, worth a fallback if you support very old devices.
  • Masonry-style grid layout: still being specified and not something to rely on in production yet. Use a JavaScript solution or a column-based fallback for now.
  • Container queries: supported in current browsers and a natural companion to both Grid and Flexbox for component-level responsiveness.

The same component, built twice

Here is a classic card section: three cards, each with an image, a title, a description of variable length and a button that must stay glued to the bottom. Same HTML in both cases.

Shared markup

<section class="cards">
  <article class="card">
    <img class="card__img" src="a.jpg" alt="">
    <div class="card__body">
      <h3>Card title</h3>
      <p>A description that can be short or quite long.</p>
    </div>
    <a class="card__cta" href="#">Read more</a>
  </article>
  <!-- two more .card -->
</section>

Version A: Flexbox

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
}

.card {
  flex: 1 1 18rem;      /* grow, shrink, start at 18rem */
  display: flex;
  flex-direction: column;
}

.card__body {
  flex: 1;              /* pushes the CTA down */
}

.card__cta {
  margin-top: auto;
}

What happens: cards fill the row and wrap when they no longer fit. If the last line contains a single card, it stretches to 100% width unless you add flex-grow: 0 or a calculated flex-basis. Card heights match inside a line because flex items stretch by default, but a card on line 2 has no relationship with a card on line 1.

Version B: CSS Grid

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

.card {
  display: grid;
  grid-template-rows: auto 1fr auto;  /* image, body, CTA */
}

What happens: the number of columns adapts automatically, every card keeps the same width on every line, and a lone card on the last line stays the width of one column. Inside the card, the three explicit rows do the job that flex: 1 and margin-top: auto were doing before.

What the two versions tell you

  1. The Grid version is shorter for the outer container, because the structure is declared once.
  2. The Flexbox version is more natural for the inner stack when the number of elements inside a card varies from one card to another.
  3. The honest best practice is the hybrid: Grid outside, Flexbox inside, or the reverse when the card has a fixed three-part anatomy.
css grid layout

How to use CSS Grid layout: a 5-step method

  1. Declare the container: display: grid on the parent. Nothing else changes yet, children simply stack into implicit rows.
  2. Define your columns: grid-template-columns: repeat(3, 1fr) for three equal columns, or repeat(auto-fit, minmax(16rem, 1fr)) for a responsive track count. Mix fixed and flexible: 240px 1fr for a sidebar plus content.
  3. Add spacing: gap: 1.5rem replaces margins entirely and never creates edge overflow.
  4. Place the items that need it: use grid-column: span 2 for a featured card, or name areas for readability:
    .layout {
      display: grid;
      grid-template-columns: 240px 1fr;
      grid-template-areas:
        "sidebar header"
        "sidebar main"
        "sidebar footer";
    }
    .site-header { grid-area: header; }
    .site-nav    { grid-area: sidebar; }
    .site-main   { grid-area: main; }
    .site-footer { grid-area: footer; }
  5. Adapt at breakpoints: redraw the whole layout in one declaration inside a media query or a container query, for example switching grid-template-areas to a single-column stack on mobile.

Tip: open the Grid inspector in Chrome or Firefox DevTools. Seeing the numbered line overlays on top of your page shortens the learning curve more than any tutorial.

Decision checklist: Grid or Flexbox?

Run through these seven questions. If you answer yes to the first four, reach for Grid. If you answer yes to the last three, reach for Flexbox.

  1. Do items need to align across both rows and columns? → Grid
  2. Does the design show an obvious column system you can describe in advance? → Grid
  3. Does any element need to span several columns or rows, or overlap another? → Grid
  4. Do you want responsive reflow without media queries and with equal track widths? → Grid
  5. Are you laying out a single row or a single column of items? → Flexbox
  6. Is the number of items unknown or does their content dictate their width (tags, menu labels, badges)? → Flexbox
  7. Do you mainly need to align, space out or push a few elements inside a component? → Flexbox

Tie-breaker: if both work and the code length is similar, choose the one your team already reads fluently. Consistency beats cleverness in a shared codebase.

css grid layout

Can you use CSS Grid and Flexbox together?

Yes, and you should. They are not competitors, they are different levels of the same system. A grid item can itself be a flex container, and a flex item can itself be a grid container. There is no conflict, no performance penalty, no nesting limit.

A typical page in production looks like this:

  • Page skeleton (header, sidebar, main, footer): display: grid with named areas.
  • Header contents (logo, nav, search, account button): display: flex with justify-content: space-between.
  • Card section: display: grid with auto-fit and minmax().
  • Inside each card: display: flex; flex-direction: column so the CTA sticks to the bottom.
  • Card meta line (author, date, tags): display: flex with gap and flex-wrap.
  • Footer columns: display: grid again, with Flexbox for each link list.

One extra combination worth knowing: subgrid. When cards inside a grid each contain a title, text and a button, subgrid lets the inner elements align on the parent’s rows, so every title and every button lines up perfectly across cards, even when titles run onto two lines. Set grid-template-rows: subgrid on the card with a matching grid-row: span 3.

Is Flexbox still used? Is it deprecated?

Flexbox is not deprecated and it is not going away. It is a stable, actively maintained W3C module and one of the most used layout features on the web. The confusion usually comes from two things: the old 2009 syntax (display: box, box-flex) which is obsolete, and the arrival of Grid, which some people read as a replacement.

In practice, most modern stylesheets contain more display: flex declarations than display: grid ones, simply because small one-dimensional arrangements are everywhere: buttons with icons, form rows, breadcrumbs, pagination, card footers. Grid replaced float-based and table-based page layouts, not Flexbox.

css grid layout

Elementor Flexbox container vs Grid container: the short version

If you build WordPress sites with Elementor, the same logic applies inside the editor. Elementor’s container can be set to a Flexbox layout or a Grid layout, and the choice follows the same rule as in hand-written CSS:

  • Flexbox container: children flow in one direction, you control direction, wrap, justify, align and gap. Items size themselves around their content, so a container with three unequal blocks is easy. This is the default way most Elementor layouts are built.
  • Grid container: you set a number of columns and rows up front and the children drop into the cells. Items keep the same width whatever their content, which is what you want for feature boxes, pricing tables or icon grids that must stay perfectly aligned.

Practical approach inside Elementor: use a Grid container when the design is a clean matrix of equal blocks, and a Flexbox container for everything else, including the inner content of each block. The rendered CSS is the same display: flex / display: grid you would write by hand, so none of the knowledge above is wasted.

Five mistakes we see most often

  1. Using Flexbox for a real grid. Chains of calc(33.333% - 1rem) on flex-basis are a sign you should have used grid-template-columns.
  2. Using Grid for a navigation bar. A menu with labels of different lengths wants content-based sizing, which is Flexbox territory.
  3. Forgetting min-width: 0. Flex and grid items default to min-width: auto, which is the number one cause of long text or wide tables blowing out a layout.
  4. Reordering visually with order or explicit placement. It changes the visual order but not the DOM order, so keyboard and screen reader users get a different sequence. Keep the markup in a logical reading order.
  5. Nesting four levels deep. If you need flex inside grid inside flex inside grid for a simple section, the structure, not the layout mode, is the problem.

FAQ

Is Flexbox deprecated?

No. CSS Flexible Box Layout is a current, stable standard supported by every modern browser and widely used in production. Only the obsolete 2009 draft syntax (display: box) is dead.

What is a CSS Grid?

CSS Grid is a two-dimensional layout system. You turn an element into a grid container with display: grid, define columns and rows with grid-template-columns and grid-template-rows, and place children into the resulting cells, optionally spanning several of them.

Can I use CSS Grid and Flexbox together?

Yes, and it is the standard way to build modern layouts. Grid handles the overall page or section structure, Flexbox handles alignment inside each component. A grid item can be a flex container and vice versa.

What is a flex in CSS?

flex is a shorthand applied to flex items for flex-grow, flex-shrink and flex-basis. For example flex: 1 1 200px means the item starts at 200px, can grow to fill free space and can shrink if space runs out.

Which one is better for responsive design?

Grid usually wins for responsive sections because repeat(auto-fit, minmax(...)) changes the column count without media queries while keeping rows aligned. Flexbox with flex-wrap is still excellent for elements that should reflow freely, such as tag lists or toolbars.

Is CSS Grid faster than Flexbox?

Rendering performance differences between the two are negligible for normal page layouts. Choose based on the structure you need, not on speed. If you do notice layout slowness, the cause is almost always very deep nesting or thousands of DOM nodes, not the layout mode.

Should beginners learn Flexbox or Grid first?

Learn Flexbox first: fewer properties, immediate results on components you build every day. Move to Grid as soon as you need a page skeleton or a gallery, then practise combining the two.

Takeaway

The CSS Grid Layout vs Flexbox debate ends the moment you stop treating it as a competition. Flexbox distributes space along one axis and lets content lead; Grid defines a two-axis structure and lets the layout lead. Pick Grid for the map of the page, Flexbox for what happens inside each territory, and use the checklist above whenever you hesitate. For more layout patterns, code snippets and design inspiration, browse the rest of the resources on CSS Gallery Pro.