ComparisonComparisons

Flexbox vs Grid

Flexbox lays out items along one dimension and sizes to their content. Grid defines a two-dimensional grid of rows and columns that you place items into.

On this page

Side-by-side comparison

#
  • Number of dimensions

    Flexbox
    One: a row or a column
    Grid
    Two: rows and columns at once
  • Starting point

    Flexbox
    The size of the items decides the layout
    Grid
    The layout (the grid) decides where each item goes
  • Placing items

    Flexbox
    In sequence, wrapping to a new line with flex-wrap
    Grid
    On specific lines or in named areas, even with overlap
  • Spacing between items

    Grid
    gap (works the same way)
  • Aligning on both axes at once

    Flexbox
    Only one axis at a time (justify-content, align-items)
    Grid
    place-items, place-content handle both axes at once
  • Typical use

    Flexbox
    Menus, tag lists, button rows, centering
    Grid
    Whole-page layouts, grids of cards with equal columns

Example

#
CSS
.flex-row {
  display: flex;
  gap: 8px;
}

.grid-area {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
Preview

Definition and usage

#

Both technologies replaced the old tricks with float and tables, but they solve a different problem. Flexbox works along a single axis, a row or a column, and lays out items according to their natural size. Grid thinks in two dimensions from the start: you first draw a grid of rows and columns, then place items into it, even in a way that makes them overlap.

In practice, Flexbox works well where you have an unknown number of items that should flow one after another: a menu, a list of tags, a row of buttons. Grid wins when you know the layout up front: an entire page with a header, a sidebar and content, or a grid of cards with equal columns and rows.

Nothing stops you from using both at once. A common pattern is Grid for the whole page and Flexbox inside a single card, where all that matters is one row with an icon and some text.

Syntax

#
Syntax
.flex { display: flex; flex-direction: row | column; }
.grid { display: grid; grid-template-columns: ...; grid-template-rows: ...; }

More examples

#
Flexbox: a toolbar that wraps itself
CSS
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
Preview

Narrow the preview window: the items move to the next line on their own. Grid would require you to define how many columns the grid should fit.

Grid: a page layout with named areas
CSS
.layout {
  display: grid;
  grid-template-columns: 120px 1fr;
  grid-template-areas:
    "header header"
    "menu   content";
  gap: 8px;
}

.layout header { grid-area: header; }
.layout nav { grid-area: menu; }
.layout main { grid-area: content; }
Preview

A two-dimensional layout like this (a full-width header, a sidebar and content) would require nesting several containers in Flexbox.

Good practices

#
  • Ask yourself: am I laying out a single row of items, or a whole grid in two dimensions? The answer points to the right tool.
  • A single-column Grid (grid-template-columns: 1fr) behaves almost like a column Flexbox. In simple cases, the difference is cosmetic.
  • Combine both technologies: Grid for the page layout, Flexbox inside a single component.

Related entries

#

Spotted a mistake or missing an example? Write to us.