HomeGuideLabGlossary

aligns and distributes flex items in the main direction

works in the main direction of the flex container.

Remember, the main direction is set by the value of on the flex container.

Main directionflex-direction

So in English, when you have a : row flex container, `justify-content will be aligning and distributing flex items horizontally.

Main directionflex-direction: row132

is applied at the very end of Flexbox layout.

At this point, flex items and flex lines have been sized, including any stretching.

132Everything has its final sizeStretching done

And, after flex items auto margins got a chance to claim remaining space first.

A123

By default, the flex items are aligned flush with the main-start side of the flex container

All flex items are smushed together.

213Only one flex item on this flex line, nothing to smushSmushing together the two flex items on this flex line

Then the gaps are inserted between flex items, if the flex container asked for any.

213Gaps only go between flex items, so no gap here

Then that entire group is placed flush with the main-start edge of the flex container.

21main-startMain direction3

If you have a : row flex container and you’re writing in English, then that’s the left side of the flex container’s box.

main-startIn this situation, main-start is on the leftflex-direction: row

If you have a : row-reverse flex container and you’re writing in English, then that’s the right side of the flex container’s box.

flex-direction: row-reverseIn this situation, main-start is on the rightmain-start

Figuring out if there’s any remaining space on a flex line

Sum up the margin box sizes of all the flex items on a particular flex line.

1122+

Add to it the sum of the gaps inserted between the flex items.

+21+

Remember that if any of the flex items had margin: auto, then the margin boxes of those flex items would’ve grown by now to fill the entire flex line. So there won’t be any remaining space for to distribute.

1A2I took everything already

Even if there’s no remaining space, the alignment values are still applied.

12justify-content: flex-endflex-direction: rowmain-end

Distribution values like space-between need remaining space to distribute. If there’s no remaining space, they each have a fallback alignment they’ll apply.

For example, space-evenly will center all the flex items just like : center.

12justify-content: space-evenlyNo space left to evenly distribute.Going to apply justify-content: center instead.

If the flex container is multi-line, works separately on each flex line.

By default, you’ll have a single-line flex container because it has : nowrap. In that case the flex container has a single line the size of the flex container. And the alignment and distribution happens in that line.

12justify-content: centerflex-wrap: nowrapmain directionSome classic old-fashioned center-aligned content in a single-line flex container

If you have : wrap or : wrap-reverse, and you do have multiple flex lines, then this alignment will be applied to each flex line separately.

flex-wrap: wrapjustify-content: centermain direction123Alignment happens separately in each flex lineHereAnd here