How it works
The flex algorithm (Flexbox §9) works per item, then per line: find each item's flex base size, collect items into lines by those sizes, then resolve flexible lengths and alignment line by line. An item only ever competes with its neighbours on the same line.
The grid algorithm (Grid §12) places every item first, then runs the track sizing algorithm for columns and then rows. Every item in a column contributes to that column's size, so content in row 3 can widen a column for rows 1 and 2. That shared sizing is the two-dimensional part.
The difference is easiest to see with a label beside a field:
<style>
.row { display: flex; width: 300px; }
.row > .field { flex: 1; }
.form { display: grid; grid-template-columns: max-content 1fr; width: 300px; }
.field { height: 20px; }
i { display: block; height: 20px; }
</style>
<div class="row"><label><i style="width: 40px"></i></label><div class="field"></div></div>
<div class="row"><label><i style="width: 80px"></i></label><div class="field"></div></div>
<div class="form">
<label><i style="width: 40px"></i></label><div class="field"></div>
<label><i style="width: 80px"></i></label><div class="field"></div>
</div>
<script>
for (const f of document.querySelectorAll(".field")) {
const offset = f.getBoundingClientRect().left - f.parentElement.getBoundingClientRect().left;
console.log(f.parentElement.className, offset, f.getBoundingClientRect().width);
}
// → row 40 260
// → row 80 220
// → form 80 220
// → form 80 220
</script>
Each flex row sizes its label from its own content, so the fields start at 40 and 80. Grid's max-content column is as wide as the widest label in it, so both fields start at 80. Chrome 155 and Firefox 158 agreed.
Edge cases
- The last row. A wrapping flex line has no knowledge of the line above, so with
flex-grow its items are wider, and with space-between they spread to the edges (see the exercise). Grid with repeat(auto-fill, …) keeps empty cells instead.
- Column flex wrapping needs a definite or maximum height; otherwise the line is as tall as its content and never wraps.
- One-dimensional grid is fine:
grid-auto-flow: column puts each item in a new column. Prefer flexbox when the items should share leftover space by content size rather than by track.
Real-world scenario
A product listing: grid places the cards, flexbox lays out each card.
<style>
.cards { display: grid; width: 330px; gap: 15px; grid-template-columns: repeat(2, 1fr); }
.card { display: flex; flex-direction: column; }
.card > p { margin: 0; }
.card > button { margin-top: auto; height: 30px; padding: 0; border: 0; }
</style>
<div class="cards">
<article class="card"><p style="height: 40px"></p><button></button></article>
<article class="card"><p style="height: 100px"></p><button></button></article>
</div>
<script>
for (const button of document.querySelectorAll(".card > button")) {
const card = button.parentElement;
console.log(card.offsetHeight, card.offsetWidth, button.offsetTop - card.offsetTop);
}
// → 130 158 100
// → 130 158 100
</script>
Grid stretches both cards to the row's 130px and makes the columns 158px each, (330 − 15) / 2 rounded by offsetWidth. Inside the short card, the button's margin-top: auto takes the spare 60px, so both buttons sit 100px down. Chrome 155 and Firefox 158 agreed.
Trade-offs
- Flexbox needs no up-front structure: add or remove items and the line rebalances. That suits content of unknown size and count. The cost is that alignment only holds within one line.
- Grid needs track definitions, but in return gives you alignment across rows, placement by line or area name, spanning and overlap without positioning. Responsive grids that change column count without breakpoints belong to the responsive-layout topic.
- Nesting costs alignment. A flex row inside a grid cell can't align its children to the outer grid's tracks;
subgrid exists for that.