Aligning flex items
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:
.nav { display: flex; justify-content: space-between; }Things to know:
justify-contentonly 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 alongsideflex-start/flex-end; in a normal row they behave the same.- Use
gapfor fixed spacing between items andjustify-contentfor how leftover space is used.
align-items: the cross axis
align-items positions items across the main axis — vertically, in a row:
.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 fixedheight(orwidthin a column).centeris the famous one-line vertical centring.baselinealigns 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:
.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:
.header { display: flex; align-items: center; gap: 1rem; }
.header .signup { margin-inline-start: auto; } /* everything after it goes to the far end */Auto margins work on both axes:
/* 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:
.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:
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:
.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).
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.
<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><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; }
.header {
display: flex;
align-items: center; /* vertical centring despite the taller logo */
gap: 1.5rem;
}
.header .logo { margin-inline-end: auto; } /* pushes the links away from the logo */
.header .signup { margin-inline-start: auto; } /* and the button to the far end */
.card {
display: flex;
flex-direction: column; /* cards stretch to equal height in .cards */
}
.card-footer {
margin-block-start: auto; /* push to the bottom of the card */
display: flex;
justify-content: space-between;
align-items: center;
}
</style>Summary
justify-contentdistributes free space along the main axis: start, center, end, space-between, space-around, space-evenly.align-itemspositions items on the cross axis: stretch (default), start, center, end, baseline.align-selfoverrides the cross-axis alignment for a single item.margin: autoon a flex item absorbs free space — the cleanest way to push one item away from the others.- With wrapping,
align-contentdistributes the lines themselves along the cross axis.