Sub topic 4
Distributing items along the main axis
This chapter assumes you can write CSS selectors, understand the box model, and know that display: flex turns a container's direct children into flex items. It also assumes you know the main axis runs along flex-direction, which is horizontal by default.
What justify-content does
justify-content controls how a flex container places its items along the main axis. It only acts on free space. Free space is the container's main-axis size minus the combined size of all items and gaps. If items already fill the line, or if an item has flex-grow that absorbs the space, justify-content has no visible effect.
The values
| Value | Result |
|---|---|
| `flex-start` | Items packed at the start (default) |
| `flex-end` | Items packed at the end |
| `center` | Items packed in the middle |
| `space-between` | First and last item touch the edges, equal space between items |
| `space-around` | Equal space around each item, so edge gaps are half the inner gaps |
| `space-evenly` | Equal space between items and at both edges |
Worked example: a navigation bar
This bar puts the first and last items at the edges and keeps 1rem between neighbors.
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}With three children, the middle one sits halfway between the other two. With two children, they sit at opposite edges.
Direction changes what "start" means
flex-start follows the main axis, not the left side of the screen. With flex-direction: column, justify-content: center centers items vertically. With row-reverse, flex-start is the right edge. The container also needs spare height in a column layout, or there is nothing to distribute.
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}Here justify-content centers horizontally and align-items centers vertically. The min-height gives the cross axis extra room to center in.
Common errors
- No visible change. The items fill the line, or one has
flex-grow: 1. Check the container's width and the item sizes. - Wrong axis. In a column,
justify-contentacts vertically. Usealign-itemsfor the horizontal direction. - Overflow with
center. If items are larger than the container,centerpushes them past both edges, and the part before the start edge cannot be scrolled to. Useflex-wrap, or avoidcenterwhen content can overflow. - Uneven edges with
space-around. The outer gaps are half the inner gaps. Usespace-evenlyif you want them equal. - Wrapped lines. With
flex-wrap: wrap, each line is distributed separately, so the last line may look different from the others.
Exercise
A .row container uses display: flex; flex-direction: column; justify-content: center; and has a fixed height of 400px. Where do the items appear, and which property would you add to center them horizontally?
Show answer
The items are centered vertically in the 400px height, because the main axis is vertical in a column. Add align-items: center to center them horizontally on the cross axis.
Exercise
Three items fill the full width of a flex row exactly. You set justify-content: space-between. What changes?
Show answer
Nothing visible changes. There is no free space on the main axis, so there is nothing to distribute.