Skip to content

Grid Or Flexbox

flexbox lays out one axis of content, grid places items on two; the layout intent picks the tool

Study this properly

Free flashcard deck: CSS Grid Vs Flexbox When To Use Which - 202 cards

Start studying

The short answer is yes, you almost always need both. Flexbox and Grid were designed to solve different problems, and treating them as rivals usually produces a layout that fights you. Reach for Flexbox when items flow along a single axis (a navbar, a row of buttons, a list of tags) and for Grid when the placement of an item depends on both a row and a column at the same time (a page shell, a card wall, a calendar).

Why Flexbox stays one-dimensional, even with wrapping

When you set flex-wrap: wrap, the browser still treats each flex line as an independent formatting context. Items on line two do not know how tall the items on line one are, so the rows often end up mismatched in height. Flexbox also has no concept of aligning one item against a column from above and another item from below; it only knows how to distribute space along the main axis of its current line. That is why a three-column card wall built with flex usually needs manual height fixes or negative margins to look right.

How Grid thinks about both axes at once

Grid defines tracks first, then drops items into them. When you write grid-template-columns: repeat(3, 1fr), you have told the browser there are exactly three vertical tracks and they should share the available width equally. Every card that follows lands in the next cell, row after row, and all cards in a row share the row's height automatically. Items can also be placed explicitly, like grid-column: 1 / 3, which is something Flexbox simply cannot express.

A common misunderstanding

People often assume Grid is only for big, page-level layouts because early tutorials focused on those. In practice, Grid is just as useful at the component level. A settings form with labels in one column and inputs in another, a pricing table, or a photo gallery are all two-axis problems and all become simpler in Grid. Conversely, some developers reach for Grid on a single row of nav links and then wonder why it feels heavier than display: flex; that is a one-axis job.

When the rule breaks down

If your content is genuinely unknown in both dimensions, such as a flow of tags whose count and length you cannot predict, Flexbox is often still the calmer choice. Grid also does not replace flex alignment properties like justify-content and align-items on a single line; you can use those inside a Grid cell. The two tools compose: a Grid page with a Flex navbar in the header and a Grid card list in the main area is a very common and very clean pattern. The question to ask is not "Flexbox or Grid?" but "where does each one do its specific job?"

Transcript

Cram I use flexbox for everything. Why would I ever reach for grid?

Rep Because flexbox only thinks in one direction. It shines when your content flows down a row or down a column.

Cram And grid handles both at the same time?

Rep Right. Grid places items against rows and columns together, which is what you want when the layout is a page, not a list.

Cram But flexbox has wrap, so it makes multiple lines. That feels two dimensional.

Rep Wrap just stacks new flex lines. Each line is still its own one axis problem, and items do not know about the line below them.

Cram So when does grid actually win?

Rep When the layout cares where things land on both axes. A page header, sidebar, content, footer. Flexbox would guess; grid would define the tracks.

Cram What if I just want a card layout that wraps to three columns?

Rep That is grid. You declare three columns and let cards drop in. With flex you fight wrapping, gaps, and inconsistent heights.

Cram Is grid harder to learn?

Rep Slightly, because you think in parent and children together. Once that clicks, two axis layouts stop being a puzzle.

Cram Can they ever work together?

Rep Often. A grid page with a flex navbar inside one cell is the cleanest pattern. Each tool does what it is good at.

Cram So the rule is simple?

Rep Yes. Content flowing one way, use flex. Layout shaped on two axes, use grid.

More lessons