Flexbox vs grid in CSS
Flexbox lines things up in one direction, and grid lines them up in rows and columns at once. Here's the same layout built both ways, a table to help you choose, and how to use them together.

Put books on a shelf, and each one takes the space it needs. When the shelf fills up, the rest go on the next shelf, and they don't line up with the books above. An egg carton works differently: every egg sits in its own slot, in neat rows and columns. CSS has a tool for each, and that's the heart of flexbox vs grid.
Flexbox lays things out in one direction, a single row or a single column, while grid lays them out in rows and columns at the same time. Use flexbox to line up a few things along one line, like the links in a menu. Use grid when things must line up both across and down, like a photo gallery or a whole page.
What is flexbox?
Flexbox, short for flexible box layout, lines up a group of items along one line. Layout means deciding where each box sits and how big it is. You turn flexbox on by setting display: flex on a parent. The boxes directly inside it, its children, become flex items, and they line up in a row.
.menu {
display: flex;
gap: 16px;
align-items: center;
}
Every link in the menu now sits in one row. The gap property puts 16 pixels of space between them. Then align-items: center lines them up through their middles, even if one is taller than the rest.
By default, each item is as wide as its content. Add flex-direction: column, and the same items stack from top to bottom instead.
What is CSS grid?
With grid, you draw rows and columns first, and the items fill the cells. You turn it on with display: grid and describe the columns you want.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
The value repeat(3, 1fr) makes three equal columns. The fr unit means a fraction: one share of the free space. Items fill the cells in order, and a new row starts after every third item.
How are flexbox and grid different?
The clearest way to see it is to lay out the same five items both ways.
<div class="box">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
Here's the flexbox version:
.box {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.box > div {
flex: 1 1 180px;
}
With flex-wrap: wrap, items move to a new line when they run out of room. The rule flex: 1 1 180px starts each item at 180 pixels wide, then lets it grow or shrink to fill its line.
In a box about 600 pixels wide, three items fit on the first line. Four and Five wrap to a second line, and they stretch to share it. They don't line up under the items above, because each flex line shares out its own space.
And here's the grid version:
.box {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
Grid keeps all five items in three equal columns. Four and Five sit right under One and Two, and the last cell stays empty.

When should you use flexbox?
Use flexbox when items sit along one line and their content should decide their size. Flexbox works from the content out. Good fits include:
- A navigation menu or a toolbar.
- A row of buttons, or an icon next to a label.
- Tags that wrap onto new lines, whatever their length.
- A card with a photo on one side and text on the other.
- Centering one box inside another, as in how to center a div.
When should you use grid?
Use grid when items must line up in both directions. Grid works from the layout in: you draw the slots first, and the items fill them. Good fits include:
- A photo gallery or a page of product cards.
- A form whose labels and fields line up in two columns.
- A dashboard with panels of different sizes.
- A whole page with a header, a sidebar, a main area, and a footer.
Here's a handy hint. If you're setting widths on flex items just to make them line up with the row above, switch to grid.
Which one should you choose?
| Flexbox | Grid | |
|---|---|---|
| Directions | One: a row or a column | Two: rows and columns |
| Sizes come from | Each item's content | The columns and rows you set |
| Wrapped items line up? | No, each line is separate | Yes, they stay in their columns |
| Great for | Menus, toolbars, button rows, tags | Galleries, forms, dashboards, page layouts |
| Turn it on with | display: flex |
display: grid |
| Space between items | gap |
gap |
| Center one box | justify-content and align-items |
place-items: center |
Can you use flexbox and grid together?
Yes. Any grid item can also be a flex container, and any flex item can be a grid container.
.page {
display: grid;
grid-template-columns: 200px 1fr;
gap: 24px;
}
.menu {
display: flex;
gap: 12px;
}
The page is a grid with two columns: a 200-pixel sidebar, and a main area that takes the rest. Inside the main area, the menu is a flex row of links. Grid handles the big structure, and flexbox lines up the small parts inside it.
Is grid better than flexbox?
Neither one is better. They are both current parts of CSS, built for different jobs. Grid doesn't replace flexbox, and flexbox can't do everything grid does.
Which should you learn first?
Start with flexbox. One line is the simpler idea, and it covers many small parts of a page. Its alignment properties, like justify-content and align-items, work in grid too, so nothing you learn is wasted.
What mistakes do beginners make?
- Styling the child instead of the parent. The parent gets
display: flexordisplay: grid, and only its direct children are laid out. Their own children aren't affected. - Forcing flexbox into a strict grid. If cards must line up in columns, wrapping flex items will fight you. Use grid.
- Using grid for a simple row. A menu of five links doesn't need columns. Flexbox does it with less code.
- Spacing items with margins. The
gapproperty adds space only between items, never around the outside edges.
Where to go next
To see a layout's lines, open DevTools, the browser's built-in tools for inspecting a page. Right-click the container and choose Inspect. In Chrome, a flex or grid badge appears next to it in the Elements panel. Click it to draw the layout over the page. Firefox has a Flexbox Inspector and a Grid Inspector that do the same.
Flexbox and grid also follow the reading direction. On a page set to right-to-left, like one in Arabic or Hebrew, a row starts on the right. Right-to-left layouts shows how. To see what flex and grid cost the browser, read layout performance and the pixel pipeline.
For practice, Grid lights asks for a 3 by 3 grid of buttons in React, a natural job for CSS grid. Styling components shows where that CSS lives in a React app. Then read the CSS box model to see how each box gets its size, or HTML vs CSS vs JavaScript if you're just starting. MDN's guide to grid and other layout methods goes deeper.


