Alignment in Flexbox: justify-content, align-items, gap and auto Margins
Placing flex items on both axes: justify-content on the main axis, align-items and align-self on the cross axis, align-content for wrapped lines, baselines, safe alignment against data loss, gap, and auto margins for pushing and centering.
kvi5hnuCode Sprout team member10 min readOn this page
Once items are sized (article 62), flexbox decides where they go: how leftover space on the main axis is shared, where each item sits across the line, and how wrapped lines are spread out. These box alignment properties come from a spec shared with grid and, partly, with block layout, so the same names work in Part 10 too.
| Property | Set on | Aligns | Axis |
|---|---|---|---|
justify-content | container | all items, by sharing free space | main |
align-items | container | each item within its line | cross |
align-self | item | one item within its line | cross |
align-content | container | the lines, when items wrap | cross |
gap | container | fixed space between items and lines | both |
There's no justify-items or justify-self in flexbox: they're ignored, because items on the main axis are placed as a group. To move one item along the main axis, use an auto margin.
justify-content: the main axis
justify-content decides what happens to free space on the main axis, the space left after the items are sized. If an item can grow, it takes the free space first, and justify-content has nothing left to share, unless the item is stopped by its max-width.

| Value | Effect |
|---|---|
flex-start | pack items at main-start; the default behavior (normal) |
flex-end | pack at main-end |
center | pack in the middle |
space-between | first and last items at the edges, equal space between the others |
space-around | equal space on both sides of each item, so edges get half a gap |
space-evenly | equal space everywhere, edges included |
start, end | pack at the start or end of the writing direction, ignoring flex-direction: *-reverse |
The positions in the diagram were measured in Chrome. With only one item, space-between puts it at the start, while space-around and space-evenly center it.
flex-start follows the flex direction, so in row-reverse it packs items on the right. start follows the writing mode instead, so in row-reverse it still packs on the left in English. Most of the time they're the same.
align-items and align-self: the cross axis
align-items sets each item's position across its line. align-self overrides it for one item.
| Value | Effect |
|---|---|
stretch (the default behavior, normal) | items fill the line's cross size, if their cross size is auto |
flex-start, start | at cross-start |
flex-end, end | at cross-end |
center | in the middle |
baseline, first baseline | items lined up by the baseline of their first line of text |
last baseline | lined up by their last line |
stretch is why flex items in a row are all as tall as the tallest one, which makes equal-height cards easy. It only stretches items whose height is auto, and it respects max-height: tested in Chrome, an item with max-height: 30px in a 100px-tall container stops at 30px.
Baseline alignment lines up text, not boxes. With items of different font sizes or padding, align-items: center centers the boxes but leaves the text at different heights; baseline puts the first line of each item's text on the same line. Tested in Chrome, with the default font and line-height: next to an item with 30px text and 10px of top padding, a 16px item is moved down 24px so their baselines match. The exact shift depends on the font's metrics.
.price-row { display: flex; align-items: baseline; gap: 0.5rem; } /* "€24" big, "/month" small, on one baseline */
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.controls { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; margin-bottom: 0.75rem; }
select { font: inherit; }
.box {
display: flex;
justify-content: var(--jc, flex-start);
align-items: var(--ai, stretch);
block-size: 10rem;
padding: 0.4rem;
gap: 0.3rem;
border: 2px dashed #8a3b12; border-radius: 8px;
}
.box > div { padding: 0.4rem 0.6rem; background: #f4efe6; border: 1px solid #8a3b12; border-radius: 6px; }
.big { font-size: 1.6rem; }
.pad { padding-block-start: 1.5rem !important; }
Compare center and baseline in align-items: with center the boxes are centered and the words sit at three heights; with baseline the words line up.

The red line in the last two panels is the shared baseline: the boxes are at different heights so that their text isn't.
align-content: wrapped lines
With flex-wrap: wrap, a container can have several flex lines. align-content shares the free space on the cross axis between the lines, with the same values as justify-content, plus stretch, the default behavior, which makes the lines taller to fill the container.
It only matters when the container is taller than its lines. And it does nothing in a single-line container: tested in Chrome, in a 100px-tall nowrap container, align-content: center leaves the item at the top, but with flex-wrap: wrap, even with one line, it moves it to the middle.
The difference between the two cross-axis properties is a common confusion:
align-itemspositions items within each line.align-contentpositions the lines within the container.

Inside each line the items are still placed by align-items; align-content only moved the lines.
Safe alignment
Centering can cause data loss: if an item is larger than its container, center makes it overflow on both sides, and the part overflowing at the start, past the left or top edge, can't be reached by scrolling. Tested in Chrome: two 80px items centered in a 100px container start at −30px.
The safe keyword falls back to start when the content would overflow:
.menu { display: flex; flex-direction: column; justify-content: safe center; }
With safe center, the same items start at 0 and overflow only at the end, where scrolling can reach them. safe and unsafe work in all browsers since 2024; unsafe is the current default behavior for these properties.

Overflow past the start edge is the problem safe solves: a scroll container can only scroll toward the end.
gap
gap sets fixed space between items, and between lines when they wrap. It's added before free space is shared, so it combines with justify-content:
.toolbar { display: flex; gap: 0.5rem; }
.tags { display: flex; flex-wrap: wrap; row-gap: 0.4rem; column-gap: 0.6rem; }
gap doesn't add space at the container's edges, doesn't collapse, and doesn't need :last-child resets, unlike margins between items. It works for flexbox in all browsers since 2021.
Auto margins
An auto margin on a flex item takes all the free space on its side, before justify-content runs. That's the way to move single items:
.nav .logout { margin-inline-start: auto; } /* pushed to the far end; the others stay at the start */
.card .actions { margin-block-start: auto; } /* in a column, pushed to the bottom */
.box .content { margin: auto; } /* centered on both axes */
Tested in Chrome: in a 400px row of three 50px items, margin-left: auto on the second puts it at 300px, so the first stays at the start and the other two move to the end. A single item with margin: auto is centered on both axes. When several items have auto margins, the free space is shared equally between the margins.
Auto margins also beat alignment on the cross axis: an item with margin-block: auto is centered vertically, whatever align-items says. And unlike center, auto margins are safe: when there's no free space, they become 0, so the item doesn't overflow at the start. (On the main axis, the container's justify-content still applies to the overflow, so don't combine them with center.)

In each case the auto margin swallows all the free space on its side, which is why one declaration is enough.
Centering
The shortest ways to center one thing in a flex container:
.overlay { display: flex; justify-content: center; align-items: center; } /* center everything */
.overlay { display: flex; } .overlay > .dialog { margin: auto; } /* center one child */
Grid's place-items: center (article 70) is shorter still. And block containers now accept align-content too, so a plain <div> with a height and align-content: center centers its content vertically, without flexbox or grid.
A card with its button at the bottom
A classic use of these properties together: cards in a row are stretched to the same height, and each card is a column whose action is pushed to the bottom:
.cards { display: flex; gap: 1rem; } /* align-items: stretch, so equal heights */
.card { display: flex; flex-direction: column; flex: 1; }
.card .button { margin-block-start: auto; align-self: start; } /* bottom, and not stretched across */
align-self: start keeps the button its natural width: in a column, the cross axis is horizontal, so stretch would make it as wide as the card.
What's current
- All flexbox alignment values, including
space-evenly,start,endandlast baseline, work in all browsers. safeandunsafe: Chrome 115, Firefox 63, Safari 17.6.gapfor flexbox: all browsers since 2021.align-contentalso works in block containers (Chrome 123, Firefox 125, Safari 17.4), so vertical centering no longer needs flexbox.
Common questions
Why doesn't justify-content do anything?
There's no free space: an item has flex-grow, or the items already fill the line. Or you meant the other axis: in a column, justify-content is vertical.
Why doesn't align-content center my items?
It aligns lines and needs flex-wrap: wrap. To center items within a single line, use align-items: center.
How do I push one item to the right in a row?
margin-inline-start: auto on it. justify-self doesn't work in flexbox.
Why are my flex items all the same height?
align-items defaults to stretching. Use align-items: start, or align-self on one item.
What's the difference between flex-start and start?
flex-start follows the flex direction, so it flips in row-reverse; start follows the writing mode.
Summary
justify-contentshares free space on the main axis: pack at the start, center or end, or space items out.align-itemsandalign-selfplace items across their line: stretch, start, center, end or baseline.align-contentspaces wrapped lines, and does nothing withoutflex-wrap.safealignment prevents overflow past the start edge.gapgives fixed space between items and lines.- Auto margins take free space on their side: push items apart, push to the bottom, or center.
Practice questions
In a 600px row, four 100px items have justify-content: space-evenly. Where does the first item start?
Answer
At 40px. The free space is 600 − 400 = 200px, shared equally among five gaps, so each is 40px.
Write CSS for a header row where the logo is at the start, and the search box and account button are together at the end.
Answer
.header { display: flex; align-items: center; gap: 1rem; }
.header .search { margin-inline-start: auto; }
The auto margin before the search box takes all the free space, pushing it and the button to the end.
A wrapping container has the rule below and is 20rem tall, with two short lines of items. The lines aren't centered. What's wrong?
.grid { display: flex; flex-wrap: wrap; align-items: center; }
Answer
align-items centers items within each line; the lines themselves are stretched to fill the container. Use align-content: center to center the lines.
A vertical menu is centered with justify-content: center, but on short screens its first items are cut off and can't be scrolled to. Fix it.
Answer
.menu { justify-content: safe center; }
safe falls back to the start when the items don't fit, so the overflow is only at the end, where a scrolling menu (overflow: auto with a limited height) can reach it.
Previous: Flex Sizing: flex-basis, flex-grow, flex-shrink and the flex Shorthand
Next: Visual Order and Accessibility: order, reading-flow and Reversed Directions

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.