Aligning flex items

Topic 3 of 5 in Flexbox8 min readBeginnerFree course

Overview

Alignment is where flexbox earns its reputation. Vertical centring, equal spacing, pushing a button to the end of a row, keeping a footer at the bottom of a card — each is one or two properties. This lesson covers all of them, and the property names make sense once you remember the two axes from Flexbox basics.

justify-content: the main axis

justify-content decides what to do with free space along the main axis — the space left after items have been sized:

Six rows of three items showing justify-content values. flex-start packs items at the start; center packs them in the middle; flex-end packs them at the end; space-between puts the first and last items at the edges with equal space between; space-around gives each item equal space on both sides, so the edge gaps are half the inner gaps; space-evenly makes every gap, including the edges, equal. Gaps between items are usually better done with gap; justify-content handles the space that’s left over.

CSS
.nav { display: flex; justify-content: space-between; }

Things to know:

  • justify-content only has an effect when there is free space. If items grow (flex: 1) to fill the container, there’s nothing to distribute.
  • start/end (from the newer Box Alignment spec) work alongside flex-start/flex-end; in a normal row they behave the same.
  • Use gap for fixed spacing between items and justify-content for how leftover space is used.

align-items: the cross axis

align-items positions items across the main axis — vertically, in a row:

Five containers showing align-items values for three items of different heights. stretch: all items fill the container’s height. flex-start: items sit at the top. center: items are centred vertically. flex-end: items sit at the bottom. baseline: items are positioned so the first lines of their text line up. Override one item with align-self. stretch only works when the item has no fixed size in the cross axis.

CSS
.header { display: flex; align-items: center; }
  • stretch (default) makes items fill the cross size — equal-height cards for free. It has no effect on items with a fixed height (or width in a column).
  • center is the famous one-line vertical centring.
  • baseline aligns the first line of text in each item — ideal for a row mixing large and small text, like a heading next to a “View all” link.

align-self

Override the alignment for one item:

CSS
.toolbar { display: flex; align-items: center; }
.toolbar .tall-thing { align-self: stretch; }
.card-row img { align-self: start; }   /* stop an image stretching */

Auto margins

An auto margin on a flex item absorbs all the free space on that side. It’s the cleanest way to push items apart:

CSS
.header { display: flex; align-items: center; gap: 1rem; }
.header .signup { margin-inline-start: auto; }   /* everything after it goes to the far end */

Everyday flexbox patterns. A navigation bar where margin-inline-start: auto pushes the Log in and Sign up buttons to the far end. A media object with a fixed image and flexible text: img flex: none, text flex: 1. A footer at the bottom of a flex column: header, main with flex: 1, footer.

Auto margins work on both axes:

CSS
/* Card footer at the bottom, however long the text */
.card { display: flex; flex-direction: column; }
.card-footer { margin-block-start: auto; }

/* Centre one item in both directions */
.container { display: flex; }
.item { margin: auto; }

Auto margins are applied before justify-content, so they win when both are used.

align-content: aligning lines

When items wrap onto several lines (next lesson), each line is a separate flex line. align-content distributes those lines along the cross axis, using the same values as justify-content:

CSS
.tags {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;   /* pack lines at the top instead of stretching them */
  height: 12rem;
}

It has no effect on a single-line container.

place-content and place-items

Shorthands set both axes at once:

CSS
place-items: center;            /* align-items + justify-items */
place-content: center;          /* align-content + justify-content */
place-content: center start;    /* align-content: center; justify-content: start */

In flexbox, justify-items is ignored (there’s no per-item main-axis alignment — use auto margins), so place-items: center only sets align-items. To centre a single item in both directions with flexbox, use justify-content: center; align-items: center — or margin: auto on the item.

Safe alignment

Centring something larger than its container makes it overflow on both sides — and content that overflows the top or left of the page can’t be scrolled to. The safe keyword falls back to start when the item would overflow:

CSS
.dialog-wrapper {
  display: flex;
  align-items: safe center;
  justify-content: safe center;
}

Use it when centring content that might be taller or wider than its container, like a modal on a small screen.

Recipe summary

Goal CSS
Space items evenly across a row justify-content: space-between
Centre vertically in a row align-items: center
Push one item to the end margin-inline-start: auto on it
Footer at the bottom of a card card: flex-direction: column; footer: margin-block-start: auto
Line up text of different sizes align-items: baseline
Centre one item in a box container display: flex; item margin: auto

If alignment does nothing: check that there’s free space to distribute, that you’re on the axis you think you are (row or column?), and that the property belongs on the container (justify-content, align-items) or the item (align-self, margins).

Exercise

A pricing card footer and a site header

Build a .header row with the logo at the left, three nav links in the middle area and a “Sign up” button pushed to the far right using an auto margin — all vertically centred even though the logo is taller. Then make .card a column whose .card-footer (a price and a button) always sits at the bottom of the card even when cards in a row have different amounts of text, with the price and button spread to opposite ends of the footer.

HTML
<header class="header">
  <a class="logo" href="/">Academy</a>
  <a href="#">Courses</a>
  <a href="#">Pricing</a>
  <a href="#">Blog</a>
  <a class="signup" href="#">Sign up</a>
</header>

<div class="cards">
  <article class="card">
    <h3>Starter</h3>
    <p>Three courses.</p>
    <div class="card-footer"><strong>$9</strong><button>Choose</button></div>
  </article>
  <article class="card">
    <h3>Pro</h3>
    <p>Every course, certificates, projects, mentoring sessions and lifetime updates.</p>
    <div class="card-footer"><strong>$29</strong><button>Choose</button></div>
  </article>
</div>

<style>
  * { box-sizing: border-box; }
  .logo { font-size: 1.75rem; font-weight: 700; }
  .cards { display: flex; gap: 1rem; }
  .card { flex: 1; border: 1px solid #d6d6d2; padding: 1rem; border-radius: 8px; }
  /* your code here */
</style>

Summary

  • justify-content distributes free space along the main axis: start, center, end, space-between, space-around, space-evenly.
  • align-items positions items on the cross axis: stretch (default), start, center, end, baseline.
  • align-self overrides the cross-axis alignment for a single item.
  • margin: auto on a flex item absorbs free space — the cleanest way to push one item away from the others.
  • With wrapping, align-content distributes the lines themselves along the cross axis.