Skip to the content

Lesson 21 of 296

Flexbox

What you will learn

  • What Flexbox is for
  • Containers, items and the two axes
  • The properties for the container
  • The properties for the items
  • How flex: 1 really works
  • Common layouts built with Flexbox
On this page

What is Flexbox?

Flexbox is a layout system for arranging items in one direction: a row or a column. It makes it easy to line things up, share space, and centre things. It is perfect for navigation bars, rows of buttons, cards in a row, and centring.

Container and items

You turn on Flexbox on a parent. Its direct children become flex items.

.nav {
  display: flex;
}
<nav class="nav">
  <a href="/">Home</a>
  <a href="/about">About</a>
  <a href="/contact">Contact</a>
</nav>

The three links now sit side by side in a row. Only the direct children are flex items. Their own children are not affected.

The two axes

  • The main axis is the direction the items are placed in. By default it goes left to right.
  • The cross axis is the other direction, at a right angle. By default it goes top to bottom.

flex-direction sets the main axis:

ValueMain axis
row (default)Left to right
row-reverseRight to left
columnTop to bottom
column-reverseBottom to top

Important: when you switch to column, the axes swap. justify-content then works vertically and align-items works horizontally.

Container properties

PropertyControlsCommon values
justify-contentPosition along the main axisflex-start, center, flex-end, space-between, space-around, space-evenly
align-itemsPosition along the cross axisstretch (default), center, flex-start, flex-end, baseline
gapSpace between items16px, 1rem 2rem (row gap, column gap)
flex-wrapWhether items can go onto a new linenowrap (default), wrap
align-contentHow the lines are spaced when there are several linesSame values as justify-content

justify-content examples

For a row of three boxes:

flex-start      [A][B][C]                    
center                    [A][B][C]          
flex-end                             [A][B][C]
space-between   [A]          [B]          [C]
space-evenly       [A]       [B]       [C]   

Perfect centring

The most famous Flexbox trick:

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100dvh;
}

The child is centred both horizontally and vertically.

Item properties

PropertyControls
flex-growHow much of the extra space this item takes. 0 = none (default).
flex-shrinkHow much it shrinks when there is not enough space. 1 = can shrink (default).
flex-basisThe starting size before growing or shrinking. auto (default) uses the item's width.
flexShorthand for the three above
align-selfOverrides align-items for this one item
orderChanges the visual order. Default 0.

How flex: 1 works

.item { flex: 1; }

This means flex-grow: 1; flex-shrink: 1; flex-basis: 0%. The starting size is zero, then all the space is shared by the grow numbers. If every item has flex: 1, they get equal widths.

.sidebar { flex: 0 0 250px; }
.content { flex: 1; }

The sidebar stays 250px (no grow, no shrink). The content takes all the rest.

.a { flex: 1; }
.b { flex: 2; }

.b gets twice as much space as .a.

Common layout: header bar

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}
<header class="header">
  <a href="/" class="logo">Acme</a>
  <nav>...</nav>
</header>

Logo on the left, menu on the right, both centred vertically.

Push one item to the end

.toolbar { display: flex; gap: 8px; }
.toolbar .logout { margin-left: auto; }

margin-left: auto takes all the free space on its left, so the logout button is pushed to the far right.

Wrapping cards

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

Each card wants to be 250px, can grow and shrink, and moves to a new line when there is not enough room.

A flex item that will not shrink

Flex items will not shrink smaller than their content by default (min-width: auto). Long words or wide content can overflow. Fix:

.item { min-width: 0; }

This is a very common hidden bug, especially with text truncation inside flex items.

A warning about order

order and row-reverse change only what you see. The keyboard Tab order and screen readers still follow the HTML order. Do not use them to fix content that is in the wrong order in your HTML.

Common mistakes

  • Putting display: flex on the item instead of the parent.
  • Mixing up justify-content and align-items, especially after switching to column.
  • Using margins between items instead of gap.
  • Forgetting flex-wrap: wrap, so items squash instead of moving to a new line.
  • Text truncation failing in a flex item because min-width: 0 is missing.

How to say it in an interview

"Flexbox is a one-dimensional layout system. You set display flex on the container and its direct children become items. justify-content aligns items along the main axis, align-items along the cross axis, and flex-direction decides which way the main axis goes. flex: 1 means grow 1, shrink 1, basis 0, so items with flex 1 share space equally. I use gap for spacing and flex-wrap to let items move to new lines."

Key words

WordMeaning
Flex containerThe parent with display: flex
Flex itemA direct child of a flex container
Main axisThe direction items are placed in
Cross axisThe direction at a right angle to the main axis
justify-contentAlignment on the main axis
align-itemsAlignment on the cross axis
gapSpace between items
flex-grow / shrink / basisHow an item grows, shrinks and starts

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.