In a flex container, how do justify-content: space-between, space-around, and space-evenly differ in where they put the leftover main-axis space?
answer
- leftover space on the main axis
- count the gaps, then divide
- edge gap: none, half, full
- negative free space triggers fallbacks
basics
~20 sspace-between puts all leftover main-axis space between the items and none at the edges; space-around gives each item an equal share on both of its sides, so edge gaps are half the inner gaps; space-evenly makes every gap, edges included, identical.
solid answer
~40 sAll three are `justify-content` values, so they only hand out the **free space** left on a flex line after the items' main sizes and any `gap` are subtracted. With n items and F free space: `space-between` creates n−1 gaps of F/(n−1) and puts nothing at the container edges, so the first and last items touch the edges. `space-around` gives every item F/n of space split equally on its two sides, which makes each edge gap F/2n — half of the F/n that ends up between two neighbours. `space-evenly` makes all n+1 gaps identical at F/(n+1). If the free space is negative, they stop distributing: `space-between` falls back to flex-start, and `space-around` and `space-evenly` fall back to center.
code
css · 7 lines.row { display: flex; width: 500px; }
.row > div { width: 100px; }
/* two 100px items => 300px of free space */
.between { justify-content: space-between; } /* 0 | 300 | 0 */
.around { justify-content: space-around; } /* 75 | 150 | 75 */
.evenly { justify-content: space-evenly; } /* 100 | 100 | 100 */go deeper
Be able to name the three values and say which one pins the first and last item to the container edges. Reaching for space-between on a header row is the everyday use an interviewer expects you to recognise.
Explain the arithmetic out loud: free space equals container size minus item sizes and gaps, then n-1, n, or n+1 shares. Be ready to compute the edge gap under space-around and say why it is half the inner gap.
Show that you debug by asking whether free space exists at all before blaming the value. Mention the negative-free-space fallbacks and that a centred overflow hides content past the start edge where no scroll can reach it.
Frame it as a resilience choice: distribution values give zero separation at the narrow end, so a design system should pair them with an explicit gap token rather than relying on leftover space to carry visual rhythm.
## What the property actually does `justify-content` is a container-level property that positions flex items along the **main axis** — the axis items are laid out along. It never resizes anything. Its whole job is to decide what happens to space the items did not use, so everything about it follows from one quantity: the free space on the line. ## Free space When the browser lays out a flex line it first resolves every item's main size, then adds up those sizes plus every item's main-axis margins, borders and padding, plus every `gap` between adjacent items. It subtracts that total from the line's main size. What is left is the free space. ```css .row { display: flex; width: 500px; } .row > div { width: 100px; } /* two items → free space = 500 - 200 = 300px */ ``` If the free space is zero — the items exactly fill the line — `justify-content` has nothing to give away and every value looks identical. This is the single most common reason it "does nothing". ## The three distribution values With **n** items and **F** free space: - **`space-between`** — F is split into the n−1 gaps *between* items: each gap is F/(n−1). Nothing is placed before the first item or after the last, so those items sit flush against the container's content edges. With the 500px example above and two 100px items: `0 | 300 | 0`. - **`space-around`** — every item is allotted F/n of space, split half before it and half after it. Two neighbouring items therefore contribute a half each, making the inner gap F/n, while the outer edges only get one half, F/2n. Same example: `75 | 150 | 75`. - **`space-evenly`** — the n+1 gaps (both edges plus every interior gap) are all equal at F/(n+1). Same example: `100 | 100 | 100`. The useful mental shortcut is to count the gaps: `space-between` counts n−1, `space-evenly` counts n+1, and `space-around` counts n but pays only half of it at each end. ## The packing values, for contrast `flex-start`, `flex-end` and `center` do not distribute at all — they push the whole group of items to one end or to the middle and leave the free space as one solid block. The initial value of `justify-content` is `normal`, which in a flex container behaves as `flex-start`, which is why items pack toward the main-start edge until you say otherwise. ## Negative free space When items overflow the line the free space is negative, and the specification defines explicit fallbacks so the result stays sane: - `space-between` behaves as `flex-start`, - `space-around` and `space-evenly` behave as `center`. `space-between` also behaves as `flex-start` when there is only one item, and `space-around`/`space-evenly` behave as `center` in that case. That last rule surprises people who assume a lone item under `space-between` will centre itself. The `center` fallback has a practical consequence: a centred overflowing row spills equally out of both ends, and the part that spills past the start edge cannot be scrolled to. The `safe` keyword (`justify-content: safe center`) exists precisely to force a `start` fallback in that case, though its browser support is more recent than the base values. ## Interaction with gap and auto margins `gap` is subtracted *before* the free space is computed, so `gap: 16px` with `space-between` means "at least 16px between items, plus whatever leftover space there is" — the two add up rather than competing. An `auto` margin on any item is greedier still: auto margins absorb all remaining positive free space before `justify-content` is consulted, which leaves the distribution values with nothing to hand out. ## Choosing between them `space-between` is the right pick when the outer items should anchor to the container edges — a header with a logo on the left and actions on the right. `space-evenly` gives the most even visual rhythm and is usually what designers mean by "spread these out". `space-around` produces that half-size edge gap that almost nobody asks for on purpose, so reach for it only when you actually want the items to look inset. In all three cases, add an explicit `gap` if the layout must keep a minimum separation at narrow widths, because at zero free space these values collapse to items touching each other.
- What does space-between do when the flex container holds only a single item?It behaves as `flex-start`, so the item sits against the main-start edge. The specification defines that fallback because there are no interior gaps to fill. `space-around` and `space-evenly` fall back to `center` for a single item, which is why swapping between the three values on a one-item container can look like a bug.
- Why does justify-content sometimes appear to have no effect at all?Because there is no positive free space to distribute. Either the items already fill the line — commonly because something is growing to consume the remainder — or they overflow it, in which case the distribution values fall back to `flex-start` or `center`. An `auto` main-axis margin on any item will also swallow the free space before `justify-content` sees it.
- How does adding gap change what space-between produces?`gap` is reserved before free space is calculated, so it acts as a floor: each interior gap ends up as the `gap` value plus its share of what is left. That makes `gap` the right tool when a layout must never let items touch, since the distribution values on their own give zero separation once free space runs out.
Think of seating people on a bench: space-between pins the end two to the armrests, space-evenly leaves the same elbow room everywhere including the armrests, and space-around gives each person a personal bubble so the bubbles merge in the middle but are cut in half at the ends.
saying these in an interview costs you the question
- Thinks space-around puts equal space everywhere, edges included
- Says justify-content moves items along the cross axis
- Expects justify-content to spread items that already overflow
- Claims space-between adds a gap after the last item
- Assumes justify-content resizes items rather than placing them