Flexbox Alignment Explained: justify-content, align-items and the Main Axis
Flexbox is the layout tool most front-end developers reach for every day. It centres things, spaces navigation links, aligns icons with text and builds toolbars. Yet many people still try justify-content and align-items at random until something works. The confusion disappears once you understand one concept: the two axes.
Main axis and cross axis
A flex container lays out its children along a line called the main axis. The direction of that line is set by flex-direction:
row(the default): the main axis runs left to right (in left-to-right languages).column: the main axis runs top to bottom.row-reverseandcolumn-reverseflip the direction.
The cross axis is always perpendicular to the main axis. Everything about Flexbox alignment depends on which axis a property controls:
| Property | Axis | Controls |
|---|---|---|
justify-content | Main | Distribution of items along the line |
align-items | Cross | Alignment of items within the line |
align-self | Cross | Alignment of one item, overriding align-items |
align-content | Cross | Spacing between lines when items wrap |
So when you switch from row to column, justify-content suddenly controls vertical spacing and align-items controls horizontal alignment. This is the number-one cause of Flexbox confusion, and it’s easy to see in action in the flexbox generator above.
justify-content values
| Value | Effect |
|---|---|
flex-start | Pack items at the start |
center | Pack items in the middle |
flex-end | Pack items at the end |
space-between | First and last items at the edges, equal space between |
space-around | Equal space around each item (so edge gaps are half the middle gaps) |
space-evenly | Exactly equal space everywhere, including edges |
align-items values
| Value | Effect |
|---|---|
stretch (default) | Items stretch to fill the line’s cross size |
flex-start / flex-end | Align to one side of the line |
center | Centre on the cross axis |
baseline | Align the first line of text in each item |
baseline is underrated: it lines up text of different sizes perfectly, for example a large price next to a small “/month” label.
Centring anything
The famous one-liner:
.parent { display: flex; justify-content: center; align-items: center; }Grid offers an even shorter version, display: grid; place-items: center;, but the Flexbox approach is so common it is worth knowing by heart.
Wrapping and align-content
By default, flex items stay on one line and shrink to fit. flex-wrap: wrap lets them flow onto new lines. Once there are multiple lines, align-content controls the spacing between those lines, much like justify-content does for items. With a single line, align-content does nothing, which explains why it often seems broken.
gap
gap works in Flexbox in all modern browsers (Safari was last, in version 14.1, 2021). It replaces margin hacks for spacing items, and it never adds space at the outer edges. For tag lists and button groups that wrap, display: flex; flex-wrap: wrap; gap: 8px; is all you need.
How items size themselves: grow, shrink, basis
Each item has three sizing properties, usually set with the flex shorthand:
- flex-grow: how much of the leftover space the item takes (0 by default).
- flex-shrink: how much it gives up when space runs out (1 by default).
- flex-basis: its starting size before growing or shrinking (
autoby default, meaning its width or content size).
| Shorthand | Expands to | Meaning |
|---|---|---|
flex: 1 | 1 1 0% | Grow and shrink equally from zero; items end up the same size |
flex: auto | 1 1 auto | Grow and shrink from content size; bigger content gets more space |
flex: none | 0 0 auto | Fixed at content size |
flex: 0 0 200px | No grow, no shrink, 200px | A fixed-width sidebar |
The margin: auto trick
Auto margins absorb free space in Flexbox. In a navigation bar, giving the last item margin-left: auto pushes it (and everything after it) to the far right. This is cleaner than space-between when you want a logo on the left, links in the middle-left and a button on the right.
Classic gotchas
Text overflow in flex items. Flex items have min-width: auto, which means they won’t shrink below their content. A long word or URL can overflow the container. Add min-width: 0 to the item (and overflow-wrap: anywhere or text-overflow: ellipsis to the text).
Images stretching. With the default align-items: stretch, images in a row can stretch vertically. Set align-items: flex-start or give images align-self: center.
Order versus accessibility. The order property and row-reverse change the visual order but not the order screen readers and keyboard navigation follow. Keep the source order logical.
Percent heights in column layouts. Children of a column flex container often need flex: 1 and min-height: 0 to scroll properly inside a fixed-height area.
Flexbox versus Grid
Flexbox works along one axis and lets items’ content drive their size. Grid works in two dimensions and lets the container define the tracks. Use Flexbox for components (navbars, toolbars, media objects, button rows) and Grid for page layouts and galleries. We compare them in detail in our CSS Grid guide.