Tutorial

CSS Flexbox and Grid: layouts that behave

Every layout on the modern web is built with two tools. Flexbox arranges things in one direction: a row or a column. Grid arranges things in two directions: rows and columns together. Learn which is which and half of CSS layout stops being mysterious.

Flexbox: the one-direction tool

Put display: flex on a parent and it becomes a flex container; its children become flex items, laid out along a main axis (default: a horizontal row). Two properties do most of the work:

  • justify-content: positions items along the main axis (start, center, end, or spaced out).
  • align-items: positions items along the cross axis, perpendicular to the main one.

The classic use: a navbar with the logo left and links right, vertically centered without any math:

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Three lines replaced what used to take floats, clearfixes, and line-height hacks. gap adds even spacing between items, no margins needed.

Grid: the two-direction tool

When you need control over rows and columns at once, reach for grid. Define the columns; the browser fills the rows:

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

1fr means one fraction of the available space, so three 1fr columns are three equal columns that shrink and grow with the container. Combine with a media query from the responsive tutorial and you have the standard responsive card grid:

@media (max-width: 768px) {
  .cards {
    grid-template-columns: 1fr;
  }
}

Which tool for which layout

LayoutUseWhy
Navbar, button rowFlexboxOne row of items; spacing along one axis
Centering one thingFlexboxjustify-content: center; align-items: center on the parent
Card grid, galleryGridTwo-dimensional: columns and rows together
Two-column article with sidebarGridExplicit column tracks, e.g. 2fr 1fr
Form with label/input pairsFlexboxEach row is one direction; stack rows normally

Rule of thumb: if you're thinking in one line, flexbox. If you're thinking in a table, grid.

Three confusions every beginner hits

  1. justify vs. align. justify-content works along the main axis, align-items along the cross axis. Change flex-direction to column and they swap roles, which is why people think they're broken. They're not; the axes moved.
  2. Forgetting flex-wrap. Flex items squeeze onto one line by default. If items overflow instead of wrapping, add flex-wrap: wrap to the container.
  3. Reaching for grid when flexbox would do. Grid is powerful, but a simple row of buttons doesn't need column tracks. Simpler tool, simpler code.

Worked example: a three card pricing row built twice

The markup stays the same for both versions. Three cards sit inside one parent with the class pricing.

<div class="pricing">
  <article class="plan">Basic</article>
  <article class="plan">Standard</article>
  <article class="plan">Premium</article>
</div>

The flexbox version

.pricing {
  display: flex;
  gap: 1rem;
}
.plan {
  flex: 1;
}

The container lays the cards out in a row. The gap property adds space between them. Setting flex to 1 on each card tells the cards to share the available width equally and to grow or shrink together. Alignment along the row comes from justify-content, and vertical alignment comes from align-items if the card content has uneven heights.

The grid version

.pricing {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

The container declares three equal columns up front. Each card lands in the next column, and a fourth card would start a second row. The row height follows the tallest card in that row, which keeps the card tops and bottoms aligned across the row without extra rules.

When a fourth card arrives, the two versions behave in ways you can predict. In the flexbox version the four cards squeeze onto one line and each card gets narrower, because flex items share one row until you add flex-wrap wrap. With wrapping turned on, the fourth card drops to a new line and may stretch or sit at the start depending on your flex settings. In the grid version the fourth card wraps to row two, column one on its own, and the column widths stay the same as the first row. If you want the fourth card centered or spanning, you place it with grid column rules. Choose flexbox when a single row that can adapt is the goal. Choose grid when the column structure should hold as items are added.

Quick decision table

Layout needBetter toolReason in brief
Nav barFlexboxOne row, space between logo and links
Card rowGrid for a fixed column count, flexbox for a fluid rowGrid holds columns as cards are added, flexbox adapts widths in one line
Full page layoutGridHeader, sidebar, main, and footer need rows and columns together
Form fields side by sideFlexbox for a pair, grid for a full formA pair is one row, a full form aligns labels and fields in columns

Frequently asked questions

Do flexbox and grid work in all browsers?

Yes, in every modern browser. They're over a decade old as specifications and safe to use without fallbacks for normal projects.

Should I still learn floats?

Only enough to recognize them in old code. Never use floats for layout in new work; flexbox and grid replaced them.

Can I mix flexbox and grid on one page?

Absolutely, and you should. Use grid for the page sections and flexbox inside the components. They compose cleanly.

Last verified: October 28, 2026, against MDN Web Docs. Both specifications are stable and universally supported.

Need layouts that just work?

We build responsive interfaces that behave on every screen.

Get a quote →

← All tutorials