justify-content
Distributes items along the main axis.
Flex alignment is where flexbox becomes useful for real UI. justify-content, align-items and gap decide how items sit in the available space.
.nav { display: flex; align-items: center; gap: 1rem; }
.nav__cta { margin-inline-start: auto; }
Layout
Flex alignment depends on the current flex-direction. In a row, justify-content works horizontally and align-items works vertically. In a column, those roles visually swap.
Alignment should solve a layout relationship: centered controls, separated navigation, equal-height items or compact button groups.
Good flex alignment removes the need for fragile margin nudges and vertical-align tricks.
Distributes items along the main axis.
Aligns items across the cross axis.
Creates consistent space between flex items.
Can push one item away from the rest inside a flex row.
Visual model
Good layout CSS is not a pile of positions. It is a set of relationships: flow, axis, tracks, placement, layering, sticking and rhythm.
Controls distribution along the main axis.
Controls alignment across the cross axis.
Controls distance between items.
Uses remaining space to separate one item.
Examples
.nav {
display: flex;
align-items: center;
gap: 1rem;
}
.nav__cta { margin-inline-start: auto; }
.nav a { margin-top: 7px; }
.nav .cta { margin-left: 243px; }
.nav { height: 63px; }
Rules that matter
Layout work is where real content, real devices and real users expose weak assumptions. The goal is a structure that explains itself in code and survives change.
Alignment properties make sense only after you know flex-direction.
gap belongs to the container and stays clean when items wrap.
Auto margins can push one group to the far side.
align-items center is usually better than height hacks.
Wrapped flex lines introduce align-content decisions.
Alignment and order are different ideas.
Production thinking
Alignment makes interfaces feel deliberate. A few clean flex rules can remove many brittle spacing fixes.
Do not use visual order to contradict reading order. Keyboard users should meet content in a logical sequence.
Create reusable alignment patterns for nav bars, cards, media objects and action rows.
Clean alignment supports usability, especially for calls to action and navigation that search visitors rely on.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these questions before moving on. If the answer is vague, change the lab layout and inspect the result.
Senior audit upgrade
A strong flexbox exercise is to build the same navigation twice: once as a row, once as a column, and explain which axis changed.
Use gap, align-items and an auto margin to place items in a horizontal bar.
Switch direction and explain how justify-content and align-items feel different.
Avoid magic margins when the alignment property can express the relationship.