Find a course

Module 4: Page Layout with Flexbox and GridLesson 9 of 1212 min

Flexbox Layouts

Use Flexbox to line items up in a row or column, distribute space with justify-content and align-items, wrap items onto new lines and let them grow or shrink.

Step 1 of 14Learn

The end of layout hacks

For years, centering something vertically on a web page was a running joke among developers. Layouts were built with floats, tables and fragile tricks that broke as soon as content changed length.

Flexbox (the Flexible Box Layout) fixed that. It's designed for arranging items in one dimension: a row or a column. Navigation bars, rows of buttons, a card's header with a title on the left and an icon on the right, and centering anything are all classic Flexbox jobs.

It starts with one declaration on the parent:

.nav {
  display: flex;
}

The element with display: flex becomes a flex container, and its direct children become flex items. Immediately, the children line up side by side in a row, instead of stacking as block elements normally do.

Two ideas make Flexbox click:

  • The main axis is the direction items flow: left to right for a row (the default), top to bottom for a column. Change it with flex-direction: row or flex-direction: column.
  • The cross axis runs perpendicular to it.

Almost every Flexbox property controls alignment along one of these two axes. Get the axes straight and the rest follows.

Step 2 of 14Practice · Choose one

You add display: flex to a ul that contains five li elements. What happens?

Step 3 of 14Learn

Aligning items: justify-content and align-items

Two properties on the container handle most alignment.

justify-content distributes items along the main axis:

  • flex-start (default): packed at the start.
  • center: packed in the middle.
  • flex-end: packed at the end.
  • space-between: first item at the start, last at the end, equal gaps between.
  • space-around and space-evenly: space around every item.

align-items positions items along the cross axis:

  • stretch (default): items stretch to fill the container's height in a row.
  • center: centered across.
  • flex-start and flex-end: aligned to one edge.

Add gap for consistent space between items without margin tricks: gap: 1rem;.

The famous centering problem now takes three lines:

.hero {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}

A classic site header uses space-between to push the logo to the left and the menu to the right:

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

Remember: in a column, the axes swap. justify-content then works vertically and align-items horizontally.

Step 4 of 14Practice · Match the pairs

Match each layout goal with the Flexbox declaration that achieves it in a row container.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Logo on the far left, menu on the far right
  2. Center a group of buttons horizontally
  3. Vertically center items of different heights
  4. Equal space between items without margins
  5. Stack the items vertically instead

Step 5 of 14Practice · Spot the mistake

This CSS should center a card both horizontally and vertically inside .page. Tap the line that prevents it from working.

Step 6 of 14Learn

Wrapping, growing and shrinking

By default, flex items stay on one line, shrinking to fit if needed. For a row of cards that should drop onto new lines on small screens, allow wrapping:

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

Three properties on the items control how they share space:

  • flex-grow: how much an item grows to fill extra space. 0 means don't grow; items with 1 share leftover space equally, and an item with 2 gets twice as much extra as an item with 1.
  • flex-shrink: how much an item shrinks when space is tight. The default is 1.
  • flex-basis: the item's starting size before growing or shrinking, such as 250px or 30%.

The shorthand flex combines them. Common patterns:

.card { flex: 1 1 250px; }   /* start at 250px, grow and shrink as needed */
.sidebar { flex: 0 0 280px; } /* fixed 280px, never grows or shrinks */
.main { flex: 1; }            /* take all remaining space */

With flex-wrap: wrap and flex: 1 1 250px, cards automatically fit as many per row as the screen allows: four on a wide monitor, two on a tablet, one on a phone, with no media queries at all.

For extra control, align-self changes one item's cross-axis alignment, and order changes its visual position. Use order sparingly, because keyboard and screen reader users still follow the HTML order.

Step 7 of 14Note

Step 8 of 14Practice · Work it out

A flex container has 300 px of leftover space. Item A has flex-grow: 1 and item B has flex-grow: 2. How many extra pixels does item B receive?

Step 9 of 14Chart

Sharing 300 px of leftover space: flex-grow 1 and 2

Grow values are shares: 1 + 2 = 3 shares, so each share is 100 px.

Sharing 300 px of leftover space: flex-grow 1 and 2Donut chart with 2 categories: Item A (flex-grow: 1): 100 px (33.3%); Item B (flex-grow: 2): 200 px (66.7%).33%67%300 pxTotal
Sharing 300 px of leftover space: flex-grow 1 and 2
ItemExtra pixels
Item A (flex-grow: 1)100 px
Item B (flex-grow: 2)200 px

Step 10 of 14Note

Step 11 of 14Tap to reveal

Three Flexbox recipes

  1. Navigation bar

    nav ul { display: flex; gap: 1.5rem; list-style: none; } puts menu links in a row with even spacing and removes the bullets.

  2. Media object

    An image beside a block of text, as in a comment or product listing: .media { display: flex; gap: 1rem; align-items: flex-start; } with the image at a fixed width and the text taking the rest via flex: 1.

  3. Sticky footer

    Make body a column flex container with min-height: 100vh and give main flex: 1. The main area grows to fill the screen, so the footer sits at the bottom even on short pages.

Step 12 of 14Practice · True or false

justify-content always controls horizontal alignment, no matter what flex-direction is set to.

Step 13 of 14Compare

Main axis and cross axis

Aspectflex-direction: rowflex-direction: column
Main axisHorizontalVertical
justify-content alignsLeft to rightTop to bottom
align-items alignsTop to bottomLeft to right
Items line upSide by sideStacked

Step 14 of 14Practice · What would you do?

What would you do?

Sofia's product page has a row of four cards using display: flex. On phones the cards get squeezed into narrow, unreadable strips instead of moving to new lines. What should she add?