← Back to HTML & CSS, page 1

html-css-0013 · Medium mid

What is CSS Grid, and how does it differ from Flexbox?

Grid is two-dimensional: you define rows and columns, then place items. Flexbox is one-dimensional and sizes items from their content along one axis. Use Grid for page frames and layouts where rows and columns must line up, and Flexbox for rows of components such as navigation. They combine well.

Full answer

CSS Grid is a two-dimensional layout system. You define rows and columns on the parent, then place items into the cells. Flexbox is one-dimensional. It lays items along one line, the main axis, and wraps that line if you allow it.

A quick way to choose: Flexbox starts from the content and lets items decide how much room they need. Grid starts from the layout and puts content into a plan you drew first.

  • Use Flexbox for a navigation bar, a toolbar, a row of buttons or centring one thing.
  • Use Grid for a page frame (header, sidebar, main, footer), a photo gallery, or any design where rows and columns must line up.
  • They combine well: a Grid for the page, Flexbox inside each cell.

The header and footer stretch across both columns, the sidebar is 200px wide and the main area takes the remaining space. fr is a share of the free space, so 1fr means "whatever is left".

Grid can also create columns automatically, for example grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)), which makes as many equal columns as fit.

html
<style>
  body { margin: 0; font-family: sans-serif; }
  .page {
    display: grid; min-height: 100vh; gap: 8px;
    grid-template-columns: 200px 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "head head"
      "side main"
      "foot foot";
  }
  .page > * { padding: 12px; background: #e8eefc; }
  header { grid-area: head; display: flex; justify-content: space-between; }
  aside  { grid-area: side; }
  main   { grid-area: main; }
  footer { grid-area: foot; }
</style>
<div class="page" id="pg">
  <header id="hd"><strong>School</strong><nav>Home | Courses</nav></header>
  <aside id="sd">Menu</aside>
  <main id="mn">Content</main>
  <footer id="ft">Footer</footer>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Grid controls rows and columns together; Flexbox controls one axis.
  • Flexbox is content-out, Grid is layout-in.
  • grid-template-areas gives readable page frames.
  • fr units share free space; minmax and auto-fit make responsive grids.
  • Use both: Grid for the page, Flexbox inside cells.

Common mistakes

  • using Grid with fixed pixel widths for everything. Use fr, minmax and auto so it still works at other sizes.
  • using nested flexboxes with fixed widths to make a table-like layout. Items in different flex rows do not line up unless you use Grid.

Follow-ups

  • what is the difference between justify-content and justify-items in Grid?
  • What does 1fr mean exactly?
  • How do you make a grid item span two columns?

Tips

  • Say it in one line in an interview: Flexbox is content-out and one-dimensional, Grid is layout-in and two-dimensional, and the two are used together.
  • Finish with a concrete choice: header and footer in Grid, nav links in Flexbox.
  • Show that you can write grid-template-areas from memory for a header, sidebar, main and footer.