Alignment and Gap
How justify-content, align-items, align-content and align-self split the work between the two axes, why auto margins help, and why gap beats margin.
Alignment and Gap
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `justify-content` distributes items (as a group) along the main axis, `align-items` aligns each item within its line on the cross axis, `align-self` overrides that for one item, and `align-content` positions whole lines on the cross axis.
- `align-content` does nothing in a single-line flex container. It needs `flex-wrap: wrap`, more than one line, and spare cross-axis space.
- There is no `justify-self` in flexbox, but an auto margin does the same job: `margin-left: auto` pushes an item, and everything after it, to the far end.
- `gap` (or `row-gap` and `column-gap`) puts space between items only, never at the outer edges. With `justify-content: space-between`, `gap` acts as a minimum spacing.
- `gap` replaces margin hacks for spacing a row of components. Margins still suit spacing in running content such as paragraphs.
Who does what
After sizing, flexbox places items. The names justify- and align- trace back to the two axes from the fundamentals lesson, and there is one simple memory aid: justify is main axis, align is cross axis.
| Property | Axis | What it moves |
|---|---|---|
justify-content |
main | the items together, as a group |
align-items |
cross | each item within its line |
align-self |
cross | one item, overriding align-items |
align-content |
cross | the lines, when there is more than one |
The -content ones deal with groups and the -items/-self ones deal with individual items, which Kevin Powell frames as the main axis focusing on groups of content and the cross axis on individual items.
justify-content and free space
justify-content only has work to do when there is free space on the main axis. Its values decide where that space goes:
.nav { display: flex; justify-content: space-between; }
flex-start, center and flex-end pack items at one place and leave the space on the other side. space-between puts the space between items with none at the ends. space-around and space-evenly add space at the ends too. They differ in how much: space-around gives each item an equal margin on both sides, so the end gaps are half the middle ones, and space-evenly makes every gap identical.
If an item has flex-grow, it takes the free space first, so justify-content has nothing left to distribute.
align-items, align-self and align-content
align-items sets the cross-axis position for every item. Its default is stretch. center is the famous one, flex-start and flex-end pin to the edges, and baseline lines up the first line of text in each item, which is what you want for items with different font sizes.
.row { display: flex; align-items: center; }
.row .badge { align-self: flex-start; } /* one item opts out */
align-content is where people trip. It positions lines, so it only matters when the container wraps onto several lines and has spare cross-axis space. In a Chromium test, with a 200px-tall container, align-content: center left an item in a single-line container at the top, and moved the same item down by 90px when flex-wrap: wrap was added. Think of it as justify-content for the cross axis, applied to rows of wrapped items.
No justify-self, but auto margins
Flexbox has align-self but no justify-self, and the reason is that on the main axis items sit next to each other, so moving one means moving what follows it. What you can do is claim free space with an auto margin:
.nav-links { display: flex; }
.nav-links .login { margin-left: auto; } /* pushes this item, and any after it, to the end */
Auto margins take their share of free space before justify-content runs. In a 400px row with two 50px items, margin-left: auto on the second put it at x = 350. margin: auto on an item in both directions centers it in the leftover space, in both axes.
gap: spacing that knows where the edges are
Before gap, spacing a row of items meant margins on each item, plus a rule to remove the margin on the last one. gap fixes this by putting space between items only:
.tags { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; } /* row-gap column-gap */
.buttons { display: flex; gap: 1rem; }
One value sets both directions. With wrapping, row-gap spaces the lines and column-gap spaces the items, and gap works in grid as well, so it is the same skill in both layouts. It has been usable in flexbox in every major browser since April 2021.
How does it combine with justify-content? The gap is a minimum, and any free space gets added on top. The numbers from a test: three 50px items with space-between and a 20px gap in a 300px container sat at 0, 125 and 250px. In a 180px container, which is too small for the 190px they need, they sat at 0, 70 and 140, so the gaps stayed at 20px and the last item overflowed the container.
Gap is the right tool for spacing a set of components. It is the wrong tool for spacing paragraphs and headings in running text, where margins that respond to context are better. That nuance is what Kevin’s runner-up video is about.
What the video covers
Kevin Powell’s “Flexbox Alignment & Justification Without the Guesswork” was published in September 2024 and runs about fourteen minutes. Its timestamps cover using devtools to try options, the main and cross axis, the main axis focusing on groups of content and the cross axis on individual items, changing flex-direction, align-content and flex-wrap, and using margins when you want justify-self. It does not have a chapter on gap, so that section and the numeric examples above are the prose’s own. I could not read a transcript, so I do not describe the demos beyond the chapter titles.
Where this fits
This finishes flexbox’s placement rules. The last lesson in the group, common flexbox patterns, puts sizing and alignment together in layouts you will actually build. The same alignment properties come back in grid, and choosing between flexbox and grid explains how they differ there.
Check yourself
A flex container wraps onto two lines and you want the lines pushed to the top of a tall container. Which property do you use?
`align-content: flex-start`. It positions the lines as a group on the cross axis. `align-items` aligns items within their own line, so it can't move the lines themselves.
You set `align-content: center` on a single-line, non-wrapping flex container. Nothing happens. Why?
`align-content` only applies to multi-line flex containers. A single-line container has one line that fills the cross size, so there is no extra space to distribute between lines.
How do you push the last item of a flex nav to the right edge while the others stay left?
Give that item `margin-left: auto`. Auto margins absorb all free space on their side before `justify-content` is applied, so the item and anything after it moves to the end.
Three 50px items sit in a 180px container with `justify-content: space-between; gap: 20px`. What happens?
The items take 150px and the two gaps need 40px, so 190px is needed in a 180px container. `gap` is a minimum, so the container overflows by 10px rather than squeezing the gap. With a 300px container, the items sit at 0, 125 and 250px, because spare space is added to each gap.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.