Key points
- Flexbox distributes items along one axis and lets their content set their size; Grid places items into rows and columns defined by the container.
- Use Grid for card grids and page layouts, Flexbox for navigation bars, button groups and simple alignment.
- Both share the same trap: an item that won’t shrink. min-width: 0 in Flexbox and minmax(0, 1fr) in Grid fix it.
“Should I use Grid or Flexbox here?” It comes up in almost every front-end code review we do. The standard answer, “Flexbox for one dimension, Grid for two”, is correct, but it doesn’t settle real-world cases: a nav bar that wraps onto two lines, a card grid whose last row stretches, a text column that overflows for no obvious reason. Here’s how we decide on our projects, with code for the layouts that come up most.
The real difference: who decides the size?
“One dimension versus two” is a decent start, but the more useful question is: who decides how big the items are, the content or the container?
- With Flexbox, content leads. Each item starts at its natural size, then leftover space is shared out (or taken away) according to
flex-growandflex-shrink. When items wrap withflex-wrap, each line is worked out on its own: nothing lines up items on one row with those on the next. - With Grid, the container leads. It defines columns and rows, and items slot into them. Columns are shared by every row, so everything stays aligned both ways.
Which gives a practical rule: if you want items to line up under each other, use Grid. If you want them to take the space they need and let everything else adapt, use Flexbox.
Flexbox: bars, groups and alignment
Flexbox shines with a run of variable-size items: a header with a logo and a menu, a row of buttons, tags, a search field with its button. The classic site header takes a few lines:
.site-header {
display: flex;
align-items: center;
gap: 1rem;
flex-wrap: wrap;
}
/* Pushes the menu and button to the right, whatever the logo's width */
.site-header .menu {
margin-inline-start: auto;
}
The auto margin soaks up all the free space. It’s the simplest way to push a group of items to the right without any extra property on the container. With flex-wrap, the menu drops below the logo when space runs out, no media query needed.
Flexbox’s classic trap is overflow. By default, a flex item won’t shrink below the minimum width of its content (min-width: auto). One long URL, a very long word or a table inside is enough to push the whole card past the screen edge, often only on mobile:
.media {
display: flex;
gap: 1rem;
}
.media img {
flex: none; /* the image keeps its size */
}
.media .body {
flex: 1;
min-width: 0; /* without this, a long URL overflows the card */
overflow-wrap: anywhere;
}
On sites we take over, that missing min-width: 0 is one of the most common causes of sideways scrolling on phones. Also worth knowing: flex: 1 means flex: 1 1 0%, so items share space equally regardless of their content. With flex: auto, they start from their natural size and only share what’s left.
Grid: card grids and page layouts
For a grid of cards, whether posts, products or team members, Grid does in one declaration what used to take percentage maths and media queries:
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(16rem, 100%), 1fr));
gap: 1.5rem;
}
The browser fits as many columns of at least 16rem as it can, then shares out the remaining space. The min() function prevents overflow on very small screens, where 16rem would be wider than the viewport. The difference between auto-fill and auto-fit only shows when there are few items: auto-fill keeps empty columns, so cards keep their width; auto-fit collapses them and stretches the existing cards. For lists of varying length we prefer auto-fill, so three search results don’t span the full width.
For page structure, named areas keep the CSS readable and make responsive rearranging trivial:
.page {
display: grid;
grid-template-columns: minmax(0, 1fr);
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
gap: 2rem;
}
@media (min-width: 64rem) {
.page {
grid-template-columns: minmax(0, 1fr) 20rem;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
}
.page > header { grid-area: header; }
.page > main { grid-area: main; }
.page > aside { grid-area: sidebar; }
.page > footer { grid-area: footer; }
Note minmax(0, 1fr) rather than plain 1fr. It’s the same trap as in Flexbox: a 1fr track won’t go narrower than its content’s minimum width, so a wide code block or table in the article widens the whole column instead of scrolling. With minmax(0, 1fr), the column keeps its width and the code block scrolls.
One more caution: moving areas around visually doesn’t change the HTML order, which is still the order for keyboard and screen reader users. Keep the source order logical and only rearrange visually what doesn’t depend on it.
The layouts where people hesitate
A card grid in Flexbox. With flex-wrap and flex: 1 1 16rem the grid works, until the last row: two lone cards stretch across the full width and no longer line up with the columns above. If cards must stay aligned, use Grid. If stretching is what you want, as with filters or tags, Flexbox is fine.
Centring an element. Both can do it. display: grid; place-items: center; is the shortest; display: flex; justify-content: center; align-items: center; is the Flexbox version. With a single child, the result is identical.
A label and field form. Flexbox aligns each row on its own, so fields don’t all start at the same point when labels differ in length. Grid lines everything up on shared columns:
.form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
align-items: center;
}
/* On mobile: label above the field */
@media (max-width: 40rem) {
.form { grid-template-columns: 1fr; }
}
This assumes labels and fields are direct children of the container. If each row is wrapped in its own element, add display: contents to that wrapper, or use subgrid, so its children join the parent grid.
Components inside a grid. You don’t pick one for the whole page. The most common combination on our projects: Grid for the page and the card grid, Flexbox inside each card to stack the title, text and button, with margin-top: auto on the button to pin it to the bottom of the card.
FAQ
Does Grid work in every browser?
Yes. Grid and Flexbox are supported by all current browsers. Newer features such as subgrid are worth checking on caniuse.com depending on your audience, but core Grid needs no special precautions.
Is one faster than the other?
Not noticeably on a normal page. Choose based on the layout you need; rendering performance problems come from images, fonts or JavaScript far more often than from Grid versus Flexbox.
Is your site overflowing on mobile, or does the layout break from one screen size to the next? We build and fix WordPress themes with modern, clean, maintainable CSS. Ask us to review your front end.



