Flexbox basics

Topic 1 of 5 in Flexbox7 min readBeginnerFree course

Overview

Flexbox (the Flexible Box Layout) arranges items in one direction — a row or a column — and distributes space between them. It’s the go-to tool for navigation bars, toolbars, button groups, card footers, form rows, media objects and centring things. This lesson builds the mental model; the next few cover sizing, alignment and wrapping.

Turning flexbox on

HTML
<nav class="nav">
  <a href="/">Home</a>
  <a href="/courses">Courses</a>
  <a href="/pricing">Pricing</a>
</nav>
CSS
.nav {
  display: flex;
}

The element with display: flex becomes a flex container, and its direct children become flex items. Immediately:

  • the items sit side by side in a row,
  • each item is as wide as its content,
  • all items are as tall as the tallest one (they stretch),
  • inline items like <a> become blockified — width, height and padding now apply.

Only direct children are affected. A <span> inside one of the links is laid out normally inside that link.

The two axes

Everything in flexbox is described in terms of two axes:

With flex-direction: row, the main axis runs left to right and the cross axis runs top to bottom. With flex-direction: column, the main axis runs top to bottom and the cross axis runs left to right. justify-content works along the main axis; align-items works along the cross axis. Change flex-direction and the axes swap — so do the jobs of those two properties.

  • The main axis is the direction items are laid out in.
  • The cross axis is perpendicular to it.

Most flexbox properties are named after these axes, so once you know which axis is which, the rest follows.

flex-direction

CSS
flex-direction: row;             /* default: main axis follows the text direction */
flex-direction: column;          /* main axis runs top to bottom */
flex-direction: row-reverse;     /* right to left (in English) */
flex-direction: column-reverse;  /* bottom to top */

row follows the inline direction of the text — in a right-to-left language, a row runs right to left. Flexbox is logical by design.

A column flex container is extremely useful: stacked items with consistent gap, a card whose footer sticks to the bottom, a full-height page layout.

Two alignment properties to start with

CSS
.nav {
  display: flex;
  justify-content: space-between;   /* along the main axis */
  align-items: center;              /* along the cross axis */
}
  • justify-content distributes items and free space along the main axis.
  • align-items positions items along the cross axis.

In a row, justify-content is horizontal and align-items is vertical. In a column, they swap. That swap is the single most common flexbox confusion — when alignment “goes the wrong way”, check the direction. Aligning flex items covers every value.

gap

gap puts space between items, never at the edges:

CSS
.toolbar { display: flex; gap: 0.5rem; }

It’s cleaner than margins: no :last-child overrides, no doubled space, and it works in both directions when items wrap (row-gap and column-gap set them separately).

The default behaviour

A flex container with nothing else set behaves as if you’d written:

CSS
.container {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;          /* everything on one line */
  justify-content: flex-start;
  align-items: stretch;       /* items fill the cross axis */
}
.container > * {
  flex: 0 1 auto;             /* don't grow, may shrink, size from content */
}

Two consequences to remember:

  • Items shrink to fit (down to their minimum content width) rather than overflowing — until they can’t shrink any more.
  • Items stretch to the container’s height. That’s why flex makes equal-height columns trivial — and why an image in a flex row may stretch unexpectedly (use align-items: start or align-self to stop it).

Visual order is not source order

row-reverse, column-reverse and the order property change the visual order:

CSS
.featured { order: -1; }   /* appears first */

But keyboard focus and screen readers still follow the HTML order. If the visual order and the HTML order disagree, a keyboard user tabbing through the page sees focus jump around unpredictably. Use these only when order doesn’t matter to meaning (decorative reversal, or swapping an image and text whose reading order is unimportant), and prefer changing the HTML. Source order and reading order goes deeper.

inline-flex

display: inline-flex creates a flex container that itself behaves like an inline element — useful for a button with an icon and text that should sit in a line of text:

CSS
.button {
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
}

When to reach for flexbox: you have a set of items in one direction, and you want them to share space, line up, or be spaced evenly. For rows and columns at once, grid is usually the better fit.

Exercise

A toolbar and a vertical menu

Turn .toolbar into a horizontal row of buttons with 0.5rem between them, vertically centred. Then turn .menu into a vertical list of links (a column) with 0.25rem between them, where every link stretches to the full width of the menu. Finally, make the toolbar’s buttons run right-to-left without changing the HTML — and explain why that would be a bad idea for keyboard users if the order mattered.

HTML
<div class="toolbar">
  <button>Bold</button>
  <button>Italic</button>
  <button>Link</button>
</div>

<nav class="menu">
  <a href="#">Dashboard</a>
  <a href="#">My courses</a>
  <a href="#">Settings</a>
</nav>

<style>
  .menu { width: 14rem; }
  .menu a { padding: 0.5rem; background: #eaf0fc; }
  /* your code here */
</style>

Summary

  • display: flex makes an element a flex container; its direct children become flex items.
  • Flex items are laid out along the main axis (set by flex-direction); the cross axis is perpendicular.
  • justify-content aligns along the main axis and align-items along the cross axis — they swap meaning when the direction changes.
  • By default items sit in a row, don’t wrap, shrink if needed and stretch to the container’s cross size.
  • row-reverse, column-reverse and order change only the visual order — keyboard and screen-reader order still follow the HTML.