Sub topic 4
Distributing items along the main axis
This chapter assumes you know how to create a flex container, how the main axis and cross axis follow flex-direction, and the basic box model. It covers justify-content, the property that decides where items sit along the main axis and how leftover space is shared.
Free space is the raw material
justify-content works on free space. Free space is the container's main-axis size minus the combined main-axis size of its items, including their margins and any gap. The property does not resize items. It only decides where they sit and where the free space goes.
.row {
display: flex;
justify-content: flex-start; /* the default behavior */
}If the free space is zero, justify-content has no visible effect. The same is true when an item has a non-zero flex-grow, because that item absorbs the free space before justify-content is applied.
The values
| Value | Result |
|---|---|
| `flex-start` | Items pack at the start of the main axis. This is the default behavior. |
| `flex-end` | Items pack at the end of the main axis. |
| `center` | Items form one group in the middle, with equal free space on both sides. |
| `space-between` | The first item sits at the start and the last at the end. Free space is split equally between neighbors. |
| `space-around` | Each item gets equal free space on both sides, so the edge gaps are half the size of the gaps between items. |
| `space-evenly` | The gaps between items and at both edges are all equal. |
A demonstration with five small boxes:
.demo {
display: flex;
width: 30rem;
border: 1px solid #888;
}
.demo > div {
width: 4rem;
height: 3rem;
background: steelblue;
}
.demo.between { justify-content: space-between; }
.demo.around { justify-content: space-around; }
.demo.evenly { justify-content: space-evenly; }Five items at 4rem take 20rem, which leaves 10rem free. Here is how each value splits it:
space-betweenputs 2.5rem in each of the four gaps and nothing at the edges.space-aroundgives each item 1rem on each side. Every inner gap is 2rem and each edge gap is 1rem.space-evenlydivides the 10rem into six equal gaps of about 1.67rem.
Start and end follow the direction
The values flex-start and flex-end refer to the main axis, not to the left and right of the screen. They change meaning when the axis changes:
- With
flex-direction: rowin a left-to-right language,flex-startis the left edge. - With
flex-direction: row-reverse,flex-startis the right edge. - With
flex-direction: column,flex-startis the top, andjustify-contentnow controls vertical placement. - In a right-to-left language such as Arabic or Hebrew, a
rowstarts on the right.
CSS also has start, end, left and right. start and end follow the writing mode and ignore flex-direction, which can surprise you with row-reverse. left and right are physical and never flip. For most layouts, stick with flex-start and flex-end, since they track the flow of the items.
.reversed {
display: flex;
flex-direction: row-reverse;
justify-content: flex-start; /* items pack against the right edge in LTR */
}How gap and justify-content combine
gap sets a minimum space between neighbors. justify-content then distributes whatever free space remains after the gaps are counted.
.toolbar {
display: flex;
justify-content: space-between;
gap: 1rem;
}With this rule the items never come closer than 1rem. If the container is wide, space-between stretches the gaps beyond 1rem. If the container is narrow, the gaps stay at 1rem and the items begin to shrink or wrap. This makes gap a safe floor for responsive layouts.
Single items and special cases
A few edge cases are worth remembering:
space-betweenwith one item behaves likeflex-start, because there is no pair of neighbors to separate.space-aroundandspace-evenlywith one item center it.justify-content: stretch(and the initialnormal) does not stretch flex items. In a flex container both act likeflex-start. Useflex-growto make items fill space.- With
flex-wrap: wrap,justify-contentapplies to each line separately. A last line with fewer items underspace-betweenpushes its items to the edges, leaving a gap in the middle that can look odd in a card grid. A common fix is to useflex-startwithgap, or switch to Grid.
Negative free space and the centering trap
When items are wider than the container and cannot shrink further, the free space is negative. Then justify-content: center overflows equally on both sides. The start side of the overflow extends past the container's start edge, where the browser does not allow scrolling to. That content can become unreachable.
.tags {
display: flex;
justify-content: center;
overflow-x: auto;
}If the tags are wider than .tags, the first ones are clipped and cannot be scrolled to. Two fixes exist:
- Use the
safekeyword:justify-content: safe center;. When the content would overflow, it falls back to start alignment. Check current browser support before relying on it. - Use auto margins on the first and last items, or on a wrapper child. An auto margin never produces negative space, so the content stays reachable.
.tags {
display: flex;
overflow-x: auto;
}
.tags > :first-child { margin-left: auto; }
.tags > :last-child { margin-right: auto; }Common mistakes
- Nothing seems to happen. The items already fill the line, usually because of
flex-groworflex: 1. Remove the growth or check the computed sizes in the browser's developer tools. - Items move vertically instead. The direction is
column. Switch toalign-itemsfor the horizontal axis, or reconsider which property you need. - The container has no width to spare. A flex container with
display: inline-flex, or one that is itself a flex item, may shrink to fit its content, so there is no free space. Give it a width, or let it fill its parent. - Using
justify-contentfor a single item. If only one item should move, an auto margin on that item is more direct, as a later chapter shows.
Exercise
A row container is 600px wide and holds three items of 100px each. It uses justify-content: space-around with no gap. How wide is the gap between neighboring items, and how wide is the gap at each edge?
Show answer
The items take 300px, so 300px is free. space-around gives each of the three items an equal share on both sides, which is 300 / 3 = 100px per item, or 50px per side. Each edge gap is 50px, and each gap between neighbors is 50 + 50 = 100px.
Exercise
You write .row { display: flex; justify-content: space-between; } and give every child flex: 1. The children sit edge to edge with no gaps. Why does justify-content have no effect, and how could you restore the spacing without removing flex: 1?
Show answer
flex: 1 sets flex-grow to 1, so the items grow to consume all free space, leaving nothing for justify-content to distribute. Add gap (for example gap: 1rem) to restore spacing. The gap is reserved before the items grow, so they still fill the row but stay separated.