One Dimension vs Two
Flexbox arranges items along one axis, a row or a column, and the content usually decides the sizes. Grid arranges items across rows and columns together, and you define the structure first. A quick memory line: Flexbox arranges items in a line. Grid arranges items on a map.
| FEATURE | FLEXBOX | GRID |
|---|---|---|
| Dimensions | One (row or column) | Two (rows and columns) |
| Starts from | The content | The layout |
| Rules usually on | The items (flex: 1 1 200px) | The container (grid-template-columns) |
| Wrapped rows align? | No, each row is independent | Yes, all rows share the same columns |
| Span rows or columns | No | Yes (span) |
| Named regions | No | Yes (grid-template-areas) |
| Best for | Components | Page and section layouts |
Same Result, Different Code
Three equal columns. Flexbox puts the rule on the children (flex: 1), while Grid defines the structure on the parent.
/* FLEXBOX: rule on the children */
.row { display: flex; gap: 8px; }
.row > div { flex: 1; }
/* GRID: structure on the parent */
.row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }A tie. Use Flexbox if the number of items can change, and Grid if you want exactly three columns no matter how many items exist.
The Most Important Difference
Five cards, room for three per row. Look at the last row. Flexbox stretches the two leftover cards to fill the line. Grid keeps them the same width as every other card, because all rows share the same column tracks.
/* FLEXBOX: last row stretches */
.cards { display: flex; flex-wrap: wrap; gap: 8px; }
.card { flex: 1 1 80px; }
/* GRID: every card keeps the same width */
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
gap: 8px;
}Use Grid for galleries and product lists that must stay aligned. Use Flexbox when you want leftover items to fill the line, such as tags or buttons.
Grid Is Shorter Here
A fixed sidebar beside flexible content. Grid needs no rules on the children at all.
/* FLEXBOX: three rules */
.layout { display: flex; gap: 8px; }
.sidebar { flex: 0 0 70px; }
.content { flex: 1; }
/* GRID: one rule */
.layout { display: grid; grid-template-columns: 70px 1fr; gap: 8px; }Slight win for Grid because the whole layout reads in one line. Flexbox is still perfectly fine.
A Navbar: Flexbox's Home Ground
Flexbox handles a simple row of items with an auto margin trick: margin-left: auto pushes the button to the far right. Grid can do it too, with three tracks.
/* FLEXBOX: add or remove links freely */
.navbar { display: flex; align-items: center; gap: 10px; }
.navbar .btn { margin-left: auto; } /* push to the far right */
/* GRID: fixed three-part structure */
.navbar {
display: grid;
grid-template-columns: auto 1fr auto; /* logo | links | button */
align-items: center;
}Use Flexbox for navbars. The number of items can change, and the content decides the sizes.
Something Flexbox Cannot Do
An item that covers two columns and two rows, with others fitting around it. Flexbox has no way to span rows. Grid does it with one line.
.grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 46px; gap: 8px; }
.big { grid-column: span 2; grid-row: span 2; }
.tall { grid-row: span 2; }grid-template-areas (naming regions) and overlapping items in the same cell. If you need either, choose Grid.
Both Work, Grid Is One Line Shorter
/* FLEXBOX: two properties */
.box { display: flex; justify-content: center; align-items: center; }
/* GRID: one property */
.box { display: grid; place-items: center; }Grid for the Page, Flexbox for the Parts
This is how most real websites are built. Grid creates the page skeleton. Flexbox arranges the small pieces inside each region.
/* GRID: the page skeleton */
.page {
display: grid;
grid-template-columns: 90px 1fr;
grid-template-areas: "h h" "s m" "f f";
gap: 8px;
}
/* FLEXBOX: inside the header */
.header { grid-area: h; display: flex; justify-content: space-between; align-items: center; }
/* FLEXBOX: inside the sidebar */
.sidebar { grid-area: s; display: flex; flex-direction: column; gap: 6px; }
/* GRID again: the card area */
.main { grid-area: m; display: grid; grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); gap: 6px; }An element can be a grid item of its parent and a flex or grid container for its own children. They nest freely.
Ask These Questions in Order
| QUESTION | ANSWER | USE |
|---|---|---|
| Must rows and columns line up together? | Yes | Grid |
| Do items need to span rows or columns? | Yes | Grid |
| Is it a page or section skeleton? | Yes | Grid |
| Is it one row or one column of items? | Yes | Flexbox |
| Is it a small component (navbar, button, card inside)? | Yes | Flexbox |
| Should content size decide the layout? | Yes | Flexbox |
Assignment: Flex or Grid?
Create compare.html and compare.css. Build each item below, then write one comment in your CSS saying why you picked Flexbox or Grid.
- A navbar with a logo, three links and a button pushed to the right.
- Six product cards that stay the same width on every row, even the last one.
- A page skeleton with header, sidebar, content and footer.
- A row of three tags that wrap naturally and stay as wide as their text.
- A photo gallery where one photo spans two columns and two rows.
- A login box centered in the middle of the screen.
- Bonus: put a flex navbar inside the header area of your grid page skeleton.