Junior (0-2 years)HTML & CSS

What is the difference between Flexbox and CSS Grid?

Quick answer

Flexbox is one-dimensional (a row or a column). CSS Grid is two-dimensional (rows and columns at once). Use Flexbox for alignment along a line; use Grid when the layout is a real 2D structure.

A nav bar, a cluster of buttons, or a card header with a title on the left and actions on the right is a Flexbox problem. A page with a sidebar, a main column and a footer, or a photo gallery with consistent tracks, is a Grid problem. They combine well: a Grid page whose cards use Flexbox internally.

Grid lets you name areas (grid-template-areas) and place items on explicit lines. Flexbox shines at distributing leftover space (justify-content, flex-grow) and wrapping. Neither replaces the other; interviews often ask you to pick one and say why.

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}