CSS Flexbox for beginners

Flexbox is the simplest way to put elements in a row or a column, align them and share out the space between them. This guide goes through all the core properties one by one, three layouts you will find on almost every site, and the mistakes beginners trip over.

What flexbox is

Flexbox (CSS Flexible Box Layout) is a CSS module for laying out elements in one direction: in a row or in a column. It solves tasks that used to need float and margin tricks: putting menu items side by side, centering a block, making a row of equal-height cards.

When you need a grid of rows and columns at once (for example, the skeleton of a whole page), CSS Grid is more convenient. For menus, rows of buttons, cards and centering, use flexbox.

Flex container and flex items

Flexbox is switched on for the parent element with display: flex. That element becomes a flex container, and all of its direct children become flex items:

<div class="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>
.container {
  display: flex;
}

The three blocks, which would otherwise stack one below another, line up in a row. The container's grandchildren (elements inside the children) do not become flex items — switch flexbox on for their own parent if you need it there.

display: inline-flex works the same way, but the container itself behaves like an inline element and only takes the width it needs.

Main and cross axes: flex-direction

Flexbox has two axes. Items are placed along the main axis; the cross axis is perpendicular to it. flex-direction sets the direction of the main axis:

  • row (default) — a row from left to right;
  • column — a column from top to bottom;
  • row-reverse and column-reverse — the same directions in reverse order.
.container {
  display: flex;
  flex-direction: column;
}

The axes give the other properties their meaning: justify-content always works along the main axis, align-items along the cross axis. Switch row to column and they seem to swap roles.

Aligning along the main axis: justify-content

justify-content distributes free space between items along the main axis:

  • flex-start (default) — items at the start;
  • center — in the center;
  • flex-end — at the end;
  • space-between — the first and last items touch the edges, the rest of the space is shared equally between items;
  • space-around — equal space around each item (so the edges get half as much as the gaps between items);
  • space-evenly — equal gaps everywhere, edges included.

The classic example is a site header with the logo on the left and the menu on the right:

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

Aligning across: align-items and align-self

align-items aligns items along the cross axis:

  • stretch (default) — items stretch to the full height of the line, which is why cards in one row have equal height for free;
  • flex-start, center, flex-end — at the start, in the center, at the end;
  • baseline — along the text baseline (handy when items contain text of different sizes).

A single item can get its own alignment: align-self overrides the container's align-items:

.item-special {
  align-self: flex-end;
}

Wrapping and gaps: flex-wrap, gap, align-content

By default (flex-wrap: nowrap) all items try to fit on one line and shrink. flex-wrap: wrap lets them move to a new line when there is not enough room. gap sets the space between items, with no extra margin on the outer ones:

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

align-content distributes lines of items rather than items, so it only works in a wrapping container. With nowrap it changes nothing — a common source of "why doesn't align-content work".

Item sizes: flex-grow, flex-shrink, flex-basis and flex

Three properties control how much space an item gets:

  • flex-basis — the starting size along the main axis (default auto: based on the content or width);
  • flex-grow — what share of the free space the item takes (default 0: it does not grow);
  • flex-shrink — how much the item shrinks when space runs out (default 1).

Usually you write the flex shorthand. The initial value is flex: 0 1 auto. flex: 1 means flex: 1 1 0%: items share all the space equally regardless of their content. With a base width, items grow from it and wrap when they don't fit:

.card {
  flex: 1 1 220px;
}

Here every card has a base width of 220px, stretches evenly into the free space and, together with flex-wrap: wrap, moves to a new line on a narrow screen.

Item order: order

order changes an item's place in the line: items with a lower value come first (all items default to 0). But this is a visual order only: screen readers and the Tab key follow the order in the HTML. If the sequence matters for meaning, change the markup itself.

Three common layouts

1. Header menu. The header is a flex container with justify-content: space-between and align-items: center (code above). The menu list needs display: flex too — its li items are children of the ul, not of the header. list-style: none removes the markers.

2. Centering a block. Two properties center the content on both axes:

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

3. A row of cards with the button at the bottom. Cards in a row have equal height (thanks to stretch), but the "Learn more" buttons end up at different heights because the amount of text differs. Make the card a flex column and give the button margin-top: auto — the automatic margin takes all the free space above it:

.card {
  flex: 1 1 220px;
  display: flex;
  flex-direction: column;
}

.card .more {
  margin-top: auto;
}

All three layouts are combined in the example below — open it in the editor and change the values.

Common mistakes

  • display: flex on the wrong element. Flexbox only affects direct children: to put menu items in a row, switch it on for the ul, not the nav.
  • Cards don't wrap. Without flex-wrap: wrap items squeeze into one line, and with flex: 1 (a 0% base width) they only wrap when their content no longer fits. Give them a base width: flex: 1 1 220px.
  • Long text stretches an item. A flex item's minimum width defaults to the width of its content (min-width: auto), so a long word or URL breaks the layout. min-width: 0 fixes it:
    .item {
      flex: 1;
      min-width: 0;
    }
  • order instead of proper markup. It only changes what the eye sees; the reading order still comes from the HTML.

What next

Flexbox sticks fastest with practice:

▶ Open in HTML Editor ⇩ Download the example .html

Sources and specification

See also