Skip to content
skillbld
HTML & CSS · 14-day planAll days
Day 10 of 14 30 min

Lay things out in a row or a column with Flexbox. Learn the flex container and its items, the main and cross axes, justify-content and align-items, gap and wrapping, and how items grow and shrink, then build a navigation bar, a row of cards and the famous perfectly centred box.

By the end of today you can

  • Turn a container into a flexbox and explain its main and cross axes
  • Arrange items with flex-direction, justify-content and align-items
  • Space items with gap and let them wrap onto new lines
  • Control growing and shrinking with flex-grow, flex-shrink and flex-basis
  • Build a navbar, a card row and a centred box

For years, putting things side by side in CSS was surprisingly hard. Flexbox fixed that. It arranges a container's children in a row or column, spaces them out and lines them up, in just a few lines.

Mind map · click a dot to fold, drag to pan

Your first flexbox

index.html · edit it, the preview updates
Page preview

Put display: flex on the container and its direct children (the items) line up in a row. gap adds space between them. Try removing display: flex to see the difference.

Two axes

Sketch
The two axes of a flex container. With flex-direction row, the main axis runs left to right through the items, and justify-content positions items along it: flex-start, center, flex-end, space-between. The cross axis runs top to bottom, and align-items positions items along it: stretch, flex-start, center, flex-end. With flex-direction column, the two axes swap.
index.html · edit it, the preview updates
Page preview
Property (on the container)ControlsCommon values
flex-directionWhich way the main axis runsrow, column
justify-contentPosition along the main axisflex-start, center, flex-end, space-between, space-around
align-itemsPosition along the cross axisstretch (default), flex-start, center, flex-end
gapSpace between items1rem
flex-wrapAllow new lineswrap

Edit the values in the code and watch. With flex-direction: column the axes swap: justify-content becomes vertical.

The perfectly centred box

index.html · edit it, the preview updates
Page preview

Three lines of CSS. This used to be a famous web-developer headache.

Growing and shrinking

index.html · edit it, the preview updates
Page preview
  • flex: 1: grow to share the free space equally.
  • flex: 2: take twice as big a share.
  • flex: 0 0 120px: don't grow, don't shrink, start at 120px (grow, shrink, basis).

Wrapping

index.html · edit it, the preview updates
Page preview

With flex-wrap: wrap, items move onto a new line when there's no room, so the same CSS works on a phone and a big screen.

A navigation bar

index.html · edit it, the preview updates
Page preview

margin-right: auto on the logo soaks up the free space, pushing the links to the right: a handy Flexbox trick.

Single-item tweaks

Items can override the container: align-self: flex-end moves one item, and order: -1 moves one to the front visually (keep the HTML order sensible for screen readers and keyboard users, though).

Recap

  • display: flex on the container lays out its children in a row (or a column).
  • justify-content: main axis. align-items: cross axis. gap: spacing.
  • flex: 1 shares space; flex: 0 0 120px fixes a size; flex-wrap: wrap allows new lines.
  • Centring: justify-content: center; align-items: center;.
  • Navbar trick: margin-right: auto on the first item.

Flexbox works in one direction at a time. Tomorrow: Grid, for rows and columns together.

Knowledge check

Test what you learned

5 questions · pick an answer to see if you're right

0/5
  1. 1

    Where do you write display: flex?

  2. 2

    With the default flex-direction: row, what does justify-content control?

  3. 3

    How do you centre one box both horizontally and vertically inside a container?

  4. 4

    What does flex: 1 on every item do?

  5. 5

    Cards in a flex row get squashed on a phone. What fixes it?

Finished day 10?

Mark complete and continue to Day 11: Grid

Was this lesson helpful?

Need a breather? Take a two-minute break

Keep going