Skip to content
Tutorials Center
My learning

Topics

Lesson 66 of 139 ยท Layout Fundamentals

Flexbox vs Grid: Choosing a Layout

Compare CSS Flexbox and Grid side by side. Learn the one-dimensional versus two-dimensional rule, build the same layout both ways and pick the right tool.

Sooner or later every CSS learner asks the same question: "Should I use Flexbox or Grid?" The honest answer is that both are excellent, they solve different problems, and most real pages use both. The trick is knowing which one to reach for first.

This lesson gives you a simple decision rule, then builds the same page section twice, once with each tool, so you can see exactly where each one is comfortable and where it fights you. You do not need to master either system yet. The full details live in the Flexbox and Grid chapters; here you are learning to choose.

The core difference: one dimension or two

Flexbox is one-dimensional. It lays items out along a single line, the main axis, and optionally wraps that line into more lines. Each line is handled on its own: the items in line two do not care how wide the items in line one are.

Grid is two-dimensional. You define rows and columns together, and items are placed into the cells they create. A column is shared by every row, so items line up vertically and horizontally at once.

flowchart TD
  Q["What are you laying out?"] --> A{"One row or one column of items?"}
  A -- "Yes" --> F["Flexbox"]
  A -- "No: rows and columns together" --> G["Grid"]
  F --> F2["Content decides the sizes"]
  G --> G2["The layout decides the sizes"]

A first-pass decision: a single line of items points to Flexbox, a table-like arrangement points to Grid.

Another way to say it: Flexbox starts from the content, Grid starts from the layout. With Flexbox, you throw items into a container and they size themselves based on what is inside, then share the leftover space. With Grid, you draw the tracks first and then drop items into them, so the items obey the structure.

Wrapping is not the same as a grid

Flexbox can wrap with flex-wrap: wrap, which makes it look two-dimensional. The difference shows up as soon as items have different widths. Each flex line distributes its own space, so columns do not line up from one line to the next. Grid keeps the columns aligned because the tracks belong to the container, not to the line.

Wrapping flex lines versus grid columns

In the flex version the second line has different item widths than the first, so nothing lines up. In the grid version every item sits in the same three columns. If alignment across rows matters, that is a grid job.

Build the same layout both ways

Here is a typical page: a header across the top, a sidebar and main content in the middle, and a footer at the bottom. First with Flexbox. A column flex container stacks header, middle and footer, and a nested row flex container places the sidebar next to the main content:

CSS
.page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.middle {
  display: flex;
  flex: 1;           /* take the leftover height */
}
.sidebar { flex: 0 0 180px; }
.main    { flex: 1; }

It works, but notice it needs two flex containers, one per dimension. Now the Grid version, which describes the whole page in one place:

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

Run both and compare them in the editor below. They look the same, but the HTML needs an extra wrapper for the Flexbox version.

The same page layout in Flexbox and in Grid

Both layouts hold up. The Grid version has a flatter HTML structure, and you can rearrange the whole page just by editing the grid-template-areas text, perhaps inside a media query. The Flexbox version is fine too, but changing the structure means touching the HTML or several rules.

Where Flexbox wins

Reach for Flexbox when the content is a single line (or a wrapping line) of things and you want them to adapt:

  • Navigation bars and toolbars: logo on the left, links in the middle, a button pushed to the right with margin-left: auto.
  • Centering one thing in the space available.
  • Button groups, tag lists, breadcrumbs where items just flow and wrap.
  • Cards with a pinned footer: a column flex container where the body takes flex: 1 so a button always sits at the bottom.
  • Components whose content size should drive the layout, such as an icon plus a label.

See more patterns in Flexbox Patterns.

Where Grid wins

Reach for Grid when the layout is defined first and the content must obey it:

  • Page layouts with header, sidebar, content and footer.
  • Card galleries where every card must have the same column width, such as repeat(auto-fill, minmax(220px, 1fr)).
  • Forms with labels in one column and inputs in another.
  • Overlapping items or items that span several rows and columns.
  • Dashboards where tiles of different sizes must fit together without gaps.

See Grid Patterns for recipes.

Using both together

The best layouts mix the two. Use Grid for the page skeleton, then Flexbox inside each region. Grid items can themselves be flex containers, and flex items can be grid containers.

Grid for the card list, Flexbox inside each card

The grid gives every card the same width and the same row height. Inside each card, a column flex container pushes the button to the bottom with flex: 1 on the paragraph.

A quick checklist

Ask yourself these questions in order:

  1. Do I need rows and columns to line up? Use Grid.
  2. Is it one line of items that may wrap? Use Flexbox.
  3. Should the content decide the size of each item? Flexbox.
  4. Should I define the structure first? Grid.
  5. Still unsure? Try Grid for the outer layout and Flexbox for the pieces inside. You can always swap one later; it is a few lines of CSS.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement best describes the difference between Flexbox and Grid?

    Flexbox arranges items along one axis at a time, while Grid defines rows and columns together.
  2. 2. A flex container wraps its items onto two lines. What happens to column alignment between the lines?

    Each flex line is laid out independently, which is why Grid is better when items must align in columns.
  3. 3. Which layout is the best fit for Grid?

    A page skeleton is defined in rows and columns, which is exactly what Grid is built for.
  4. 4. Can a grid item also be a flex container?

    display: flex on a grid item creates a flex container inside that cell. Mixing the two is common.

Summary

  • Flexbox is one-dimensional and content-driven; Grid is two-dimensional and layout-driven.
  • Wrapped flex lines size themselves, so items do not align across lines. Grid tracks are shared.
  • Use Flexbox for navbars, toolbars, button groups, centering and small components.
  • Use Grid for page skeletons, galleries, forms, overlaps and dashboards.
  • Combine them: Grid for the outer structure, Flexbox inside each region.
  • Switching between them later is cheap, so pick one and move on.

Next: Flexbox.