IntermediateLesson 12 of 306 min read

CSS Flexbox basics: containers, axes, justify-content and align-items

Learn Flexbox from the ground up: flex containers, the main and cross axes, alignment, centring and gap.

In this lesson you will

  • Turn a parent into a flex container
  • Understand main and cross axes
  • Align and distribute items
  • Build a nav bar and centre a box

About 16 minutes

The layout tool you will use every day

Imagine arranging chairs along a wall. In the old days of CSS you measured every chair, nudged it, and rearranged them all when someone added one. Flexbox is like telling the chairs: “line up in a row, spread out evenly, and stay centred”. The browser handles the maths. A menu bar, a row of buttons, a card with a footer pinned to the bottom, a perfectly centred box: all are one or two lines of flexbox.

Container and items

Flexbox has two roles. The flex container is the parent. The flex items are its direct children. You switch it on with one line on the parent.

.row {
  display: flex;
}

The children, which used to stack vertically, now sit in a row. That is already useful. Everything else is controlling how that row behaves.

Press Run and see the three boxes stacked. Then add display: flex to .row and watch them line up.

<style>
 .row { /* add display: flex */ }
 .row div { background: #EFEAFD; border: 2px solid #5A28C8; padding: 14px; font-family: Arial; }
</style>
<div class="row"><div>One</div><div>Two</div><div>Three</div></div>

Write display: flex; inside the .row rule.

<style>
 .row { display: flex; }
 .row div { background: #EFEAFD; border: 2px solid #5A28C8; padding: 14px; font-family: Arial; }
</style>
<div class="row"><div>One</div><div>Two</div><div>Three</div></div>

Two axes

Everything in flexbox is described along two lines. The main axis is the direction items flow, a row by default. The cross axis runs at right angles to it. Understanding that single idea makes the rest easy to remember.

.row { flex-direction: row; }            /* default: left to right */
.col { flex-direction: column; }         /* top to bottom */
.rev { flex-direction: row-reverse; }

When you change to column, the axes swap: the main axis is now vertical and the cross axis is horizontal. Beginners get confused when justify-content seems to change direction. It does not. The axis changed.

justify-content: along the main axis

.row { justify-content: flex-start; }     /* pack at the start (default) */
.row { justify-content: center; }         /* in the middle */
.row { justify-content: flex-end; }       /* at the end */
.row { justify-content: space-between; }  /* first and last at the edges, equal gaps */
.row { justify-content: space-around; }   /* equal space around each item */
.row { justify-content: space-evenly; }   /* identical gaps everywhere */

space-between is the one you will use most: logo on the left, links on the right.

align-items: along the cross axis

.row { align-items: stretch; }      /* default: fill the height */
.row { align-items: center; }       /* vertically centred in a row */
.row { align-items: flex-start; }   /* top */
.row { align-items: flex-end; }     /* bottom */
.row { align-items: baseline; }     /* line up the text */

The famous centring trick

Centring a box both ways was a classic CSS headache. With flexbox it takes three lines.

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

Centre the box inside the grey area, both horizontally and vertically, using flexbox on the parent.

<style>
 .stage { background: #E7E3F5; height: 180px; /* centre the child */ }
 .box { background: #5A28C8; color: #fff; padding: 16px 24px; border-radius: 12px; font-family: Arial; }
</style>
<div class="stage"><div class="box">Centred!</div></div>

On .stage: display: flex; justify-content: center; align-items: center;

<style>
 .stage { background: #E7E3F5; height: 180px; display: flex; justify-content: center; align-items: center; }
 .box { background: #5A28C8; color: #fff; padding: 16px 24px; border-radius: 12px; font-family: Arial; }
</style>
<div class="stage"><div class="box">Centred!</div></div>

gap: space between items

.row { display: flex; gap: 16px; }
.grid-like { display: flex; row-gap: 24px; column-gap: 12px; }

The gap property puts space between items only, never before the first or after the last. It replaces the old habit of adding margins to children and then cancelling the last one.

Building a navigation bar

<header class="bar">
  <a class="logo" href="/">Skills</a>
  <nav class="links">
    <a href="/skills">Skills</a>
    <a href="/tools">Tools</a>
    <a href="/blog">Blog</a>
  </nav>
</header>
.bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
}
.links { display: flex; gap: 20px; }

Notice that .links is both a flex item of .bar and a flex container for its own links. Flex containers nest freely, and nested flexbox is how real layouts are built.

Good to know

Only direct children become flex items. A grandchild inside one of the children is not affected unless that child is a flex container too.

Common mistake

Setting display: flex on the wrong element. If nothing moves, check that you applied it to the parent of the items you want to arrange, not to the items themselves.

Build the bar: logo on the left, links on the right, everything vertically centred. Add a gap of 16px between the links.

<style>
 .bar { background: #14112E; color: #fff; padding: 12px 18px; font-family: Arial; }
 .bar a { color: #fff; text-decoration: none; }
 .links { }
</style>
<div class="bar"><a class="logo" href="#">Skills</a><nav class="links"><a href="#">Skills</a><a href="#">Tools</a><a href="#">Blog</a></nav></div>

Make .bar a flex container with justify-content: space-between and align-items: center, and .links a flex container with gap.

<style>
 .bar { display: flex; justify-content: space-between; align-items: center; background: #14112E; color: #fff; padding: 12px 18px; font-family: Arial; }
 .bar a { color: #fff; text-decoration: none; }
 .links { display: flex; gap: 16px; }
</style>
<div class="bar"><a class="logo" href="#">Skills</a><nav class="links"><a href="#">Skills</a><a href="#">Tools</a><a href="#">Blog</a></nav></div>

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What switches Flexbox on?

  1. flex: on on the child
  2. display: flex on the parent
  3. position: flex
  4. layout: flex

display: flex on the container.

Which property spreads items along the main axis?

  1. align-items
  2. justify-content
  3. gap
  4. order

justify-content works along the main axis.

What does align-items: center do in a row?

  1. Centres horizontally
  2. Centres vertically
  3. Reverses order
  4. Adds a gap

It centres items on the cross axis, which is vertical in a row.

What does gap do?

  1. Adds space between items only
  2. Adds a border
  3. Changes the order
  4. Hides items

Gap creates space between items, not at the outer edges.

What happens to the axes with flex-direction: column?

  1. Nothing
  2. Main and cross axes swap
  3. Items are removed
  4. It becomes grid

The main axis becomes vertical.

Summary

  • display: flex on the parent arranges its children.
  • justify-content is the main axis; align-items is the cross axis.
  • gap spaces items evenly.
  • Flex containers can nest.

Next lesson

Next: wrapping, growing and shrinking, the second half of Flexbox.

Questions

Why does justify-content seem to work vertically?

With flex-direction: column the axes swap, so justify-content acts vertically.

Flexbox or grid?

Flexbox is for one dimension (a row or a column). Grid is for two. You will learn both.

Do I need prefixes?

No. Flexbox works unprefixed in all current browsers.

Progress is saved in this browser.