CSS Grid and Flexbox are the two layout systems every web developer needs to know. They overlap a little, which makes it easy to wonder which one to use. This guide explains what each one is best at, with interactive demos to help it click, and a simple way to decide between them.
The one-line answer
If you remember nothing else: Flexbox is for one dimension (a row or a column), and Grid is for two dimensions (rows and columns at the same time). That's just the starting point, though. Knowing when to reach for each one is what makes layouts easy.
Mental model: think of Flexbox as arranging books along a single shelf. Think of Grid as designing the whole bookcase: the rows, the columns, and where every book goes in relation to the rest.
Flexbox: for one dimension
Flexbox is at its best when items flow along a single line, sharing out the space and lining themselves up, without you setting exact positions. It's content-driven: the layout responds to what's inside it.
Flexbox playground
Change the settings below to see how each property affects the four items. The CSS underneath updates as you go:
Item B is a little taller than the others, so you can see what align-items does. With stretch, the default, every item grows to match the tallest one.
Flexbox shines for
- Navigation bars: a logo on the left, links in the middle and a button on the right, using
justify-content: space-between. - Button groups: centring icons inside buttons, and spacing a row of buttons evenly.
- Card rows: a strip of cards that wraps onto more lines as space runs out.
- Form rows: labels and inputs side by side, or stacked with consistent gaps.
A real navbar example
nav {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
height: 60px;
}
.nav-links {
display: flex;
gap: 24px;
}
The outer nav spreads its three children across the bar with space-between, and centres them vertically with align-items: center. The links are their own small flex container, spaced with gap.
Pushing items apart
Two flexbox tricks you'll use all the time: an auto margin pushes an item as far as it can go, and flex: 1 makes an item grow to fill the leftover space.
/* Push the last item to the far end of the row */
.nav-cta {
margin-inline-start: auto;
}
/* Make one item take up all the leftover space */
.search-box {
flex: 1;
}
Correction: the original version of this post suggested combining flex: 1 with justify-content: space-between to push items to the edges. Those two don't work together: once an item with flex: 1 has taken all the spare space, there's none left for space-between to share out. Use one or the other, or an auto margin, as above.
To build flexbox layouts visually, try our Flexbox Generator. It writes the CSS for you, so there's no need to memorise the property names.
CSS Grid: for two dimensions
Grid lets you define rows and columns, then place items exactly where you want them within that structure. It's layout-driven: you design the frame first, then put the content into it. That makes it ideal for layouts that need to line up in both directions.
Grid playground
Items 1 and 4 use grid-column: span 2 to stretch across two columns. Try the different column settings to see how the items flow into the grid:
Grid shines for
- Page layouts: a header, sidebar, main content and footer, defined once and placed exactly.
- Image galleries: uniform grids where images line up in both rows and columns.
- Dashboards: cards and widgets that span several columns or rows.
- Magazine layouts: editorial designs where items overlap, span and break the grid on purpose.
Correction: the original version of this post listed "masonry-style" galleries as a Grid use case. A normal grid can't do masonry, where items of different heights pack tightly into columns, because every item in a row shares the same row height. That needs the newer display: grid-lanes, which so far works only in Safari.
Page layout with named areas
grid-template-areas lets you name the parts of a layout and draw them in your CSS, almost like a picture. Each string is a row, and each word is a cell:
.layout {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 200px 1fr;
grid-template-rows: 60px 1fr 50px;
gap: 16px;
}
/* Then assign each child to its area */
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }
Responsive columns without media queries
This single line is one of the most useful in CSS. It fits as many columns as there's room for, each at least 250px wide, and adds or removes columns as the container changes size, with no media queries needed:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 16px;
}
Try the auto-fill option in the grid playground above to see it in action. Its close relative, auto-fit, behaves the same way, except that when there are only a few items, it stretches them to fill the row instead of leaving empty columns.
Side-by-side comparison
| Feature | Flexbox | CSS Grid |
|---|---|---|
| Dimensions | One (a row or a column) | Two (rows and columns) |
| Driven by | The content | The layout |
| Item sizing | Flexible, based on content | Set by the rows and columns you define |
| Alignment | Strong alignment along and across the line | Precise placement in both directions |
| Wrapping | Natural, with flex-wrap | Controlled, with auto-fill and auto-fit |
| Overlapping items | Needs tricks such as negative margins | Built in: place items in the same cells |
| Best for | Navbars, forms, buttons, card rows | Pages, dashboards, galleries |
| Browser support | All modern browsers | All modern browsers |
How to decide
Ask yourself these three questions to pick the right tool:
| Question | Use Grid if… | Use Flexbox if… |
|---|---|---|
| Do you need to control rows and columns at the same time? | Yes: items must line up across both rows and columns. | No: items flow in one direction. |
| Is the layout set by the container or by the content? | The container: you define the tracks, and content fills them. | The content: items size themselves, and the parent shares out the space. |
| Are you building a whole page or a small component? | A whole page: the outer shell, or a dashboard's structure. | A component: buttons, cards, form rows, navbars. |
Key insight: the two work together; they don't compete. A very common pattern is to use Grid for the overall page structure, then Flexbox inside each grid area to arrange that component's content. You'll use both in almost every project.
Using both together
Here's a pattern you'll use constantly. Grid lays out the page and the row of cards, and Flexbox arranges the inside of each card:
/* Grid handles the big picture: the page */
.page {
display: grid;
grid-template-columns: 1fr 300px;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
/* Grid also lays out the row of cards */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: 20px;
}
/* Flexbox handles the inside of each card */
.card {
display: flex;
flex-direction: column;
gap: 12px;
}
.card__footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: auto; /* push the footer to the bottom of the card */
}
The margin-top: auto on the footer is the flexbox auto-margin trick again. It pushes each card's footer to the bottom, so the footers line up even when the cards have different amounts of text:
A short card with just a little text.
This card has a lot more text in it, so it is taller than the others. Notice that every footer still sits at the bottom of its card.
Grid for the row, flexbox inside each card.
Common mistakes to avoid
Using Grid for everything
Grid is powerful, but it's more than you need for a row of three buttons or a simple navbar. Flexbox is lighter and more natural for one-dimensional arrangements, so use it freely for components.
Using Flexbox for whole page layouts
If you find yourself nesting flex containers three levels deep to build a page structure, that's a job for Grid. Named template areas make the same layout much easier to read and change.
Forgetting about the implicit grid
When there are more items than your defined grid has room for, Grid creates extra rows or columns automatically. These are called implicit tracks, and they're sized auto by default, which can give you unexpected row heights. Control them with grid-auto-rows and grid-auto-columns:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 150px; /* every extra row is 150px tall */
}
Mixing up align-items and align-content
In Flexbox, align-items lines up the items within each line, while align-content controls the space between lines when items wrap. If there's only one line, align-content does nothing, which is a common source of confusion.
DevTools tip: Chrome, Edge, Firefox and Safari all have Grid and Flexbox inspectors in their developer tools. Turn on the grid overlay to see the tracks, gaps and item positions drawn over your page. It makes layout problems much easier to spot.
TL;DR
Reach for Flexbox when you're arranging items along a single line: navbars, button groups, form rows and card strips. Reach for Grid when you're building a two-dimensional structure: page layouts, dashboards and galleries. And don't be afraid to combine them. Grid for the outer structure and Flexbox for the components inside is how most great layouts are built.
Want to experiment? Try our Flexbox Generator to build flexbox layouts visually and copy the CSS for your next project. It's free, with no sign-up needed.