DAY 25 · COMPARISON LESSON

Flexbox vs Grid: Which One and When?

The same layouts built both ways, side by side. See where they give the same result, where they differ, and what only one of them can do.

Day 25 of 30BeginnerComparison~25 min

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.

FEATUREFLEXBOXGRID
DimensionsOne (row or column)Two (rows and columns)
Starts fromThe contentThe layout
Rules usually onThe items (flex: 1 1 200px)The container (grid-template-columns)
Wrapped rows align?No, each row is independentYes, all rows share the same columns
Span rows or columnsNoYes (span)
Named regionsNoYes (grid-template-areas)
Best forComponentsPage 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.

LIVE RESULT
FLEXBOX
1
2
3
GRID
1
2
3
style.css
/* 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; }
VERDICT

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.

LIVE RESULT
FLEXBOX
1
2
3
4
5
GRID
1
2
3
4
5
style.css
/* 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;
}
VERDICT

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.

LIVE RESULT
FLEXBOX
side
content
GRID
side
content
style.css
/* 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; }
VERDICT

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.

LIVE RESULT
FLEXBOX
MySiteHomeAboutSign up
GRID
MySiteHomeAboutSign up
style.css
/* 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;
}
VERDICT

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.

LIVE RESULT
GRID ONLY
big (2x2)
1
tall
2
style.css
.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; }
ALSO GRID ONLY

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

LIVE RESULT
FLEXBOX
centered
GRID
centered
style.css
/* 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.

LIVE RESULT
MySiteSign up
DashboardLessonsSettings
Card 1Card 2Card 3Card 4
footer
style.css
/* 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; }
REMEMBER

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

QUESTIONANSWERUSE
Must rows and columns line up together?YesGrid
Do items need to span rows or columns?YesGrid
Is it a page or section skeleton?YesGrid
Is it one row or one column of items?YesFlexbox
Is it a small component (navbar, button, card inside)?YesFlexbox
Should content size decide the layout?YesFlexbox

Assignment: Flex or Grid?

HANDS-ON EXERCISE

Create compare.html and compare.css. Build each item below, then write one comment in your CSS saying why you picked Flexbox or Grid.

  1. A navbar with a logo, three links and a button pushed to the right.
  2. Six product cards that stay the same width on every row, even the last one.
  3. A page skeleton with header, sidebar, content and footer.
  4. A row of three tags that wrap naturally and stay as wide as their text.
  5. A photo gallery where one photo spans two columns and two rows.
  6. A login box centered in the middle of the screen.
  7. Bonus: put a flex navbar inside the header area of your grid page skeleton.
Course Overview