HomeGuideLabGlossary

Check for remaining flex line space in the main direction

Let’s see if there’s still some room left in each flex line

By default, flex items don’t grow to fill their flex lines along the main axis.

1No, thanks!Wanna grow?INITIAL PROPERTY VALUEflex-grow: 0

So there’s a good chance there’s some room left in each flex line.

If there’s still some room, the flex items will get a chance to grow their auto margins.

AA1margin-inline: auto

Note that this won’t grow the visible part of a flex item, just its margin. But according to the box model, the margin area belongs to the box just as much as the border, padding, and content areas. So technically we will be growing the margin box, or the “outer size” of the flex item.

AA1INNER SIZEOUTER SIZEContent boxMargin boxContent, Padding, Border, Margin

If there aren’t any auto margins, then all the space will be distributed according the value of on the flex container.

112233justify-content: space-betweenTake the remaining spaceAnd distribute it between the flex items

First, grab the length of the flex line

Flex lines always have the same length as the size of the flex container in the main axis. For example, if your flex container has a fixed size of 500px, then that’s the lenght of the flex line.

flex line length: 500pxinline-size: 500pxmain direction

Check the chapter on collecting flex items into flex lines for details on where this size comes from.

Subtract the size of each flex item’s margin box.

This is the “outer size” of the flex item, which is the main size we calculated before, plus any padding, border, and margin that each flex item might have.

1500px-flex line lengthOUTER SIZE

auto margins will get a chance to claim the remaining flex line space later. But for the sake of this calculation, we treat them as though they were zero.

1margin-inline-end: autoAOUTER SIZEDoesn’t count for now

Subtract the reserved space for gaps between flex items.

If the flex container set a gap to be inserted between flex items, then that space is already claimed by those gaps.

12Takes up space, so there’s less free space leftgap

If you have two flex items, there’s one gap. If we have three flex items, that’s two gaps. And so on.

1112223345One gapTwo gapsFour gaps

Subtracting these two things from the flex line length gives you the remaining flex line space.

Note that your flex items might be overflowing the flex container already.

Remaining space12

For example, if the flex items refuse to shrink.

1No remaining space...

In that case, this remaining space might be below zero.

1Remaining space-80px

Of course, this means there’s no more room for the flex items auto margins to claim.

1AThis margin is 0

And there’s no space for to distribute.

1No space to distribute

However, the flex items can still be aligned, for example to the center of the flex line. In this case they will overflow the flex line on both sides.

1justify-content: centerWe don’t need any remaining space to align flex items