HomeGuideLabGlossary

Resolve flex item main sizes

Congrats, you’ve made it to the heart of Flexbox Land

The flex container set the stage, offering a certain amount of space to flex items. And it set the , which determines the main direction.

Available space for flex itemsflex-direction: rowmain direction

Each flex item specified its flex base size.

flex-basis: 100pxflex-basis: autoinline-size: auto1Bananaflex base size100 pixelsflex base sizemax-contentDepends on the amount of text, font size, etc.main direction

Flex items have been collected into one or more flex lines according in the right order.

􀆿Name.name.imageorder: 2order: 1Check order property firstWhen order is the same, look at who comes first in the box treemain direction

Flex items grow and shrink in each flex line separately

You could have one flex line with plenty of remaining space, and you’ll let the flex items grow.

112Ooh, there’s space left on this flex line!I’ll grow

And you could have another flex line which has no remaining space, and you’ll ask the flex item to shrink.

1122No more space left, I’m overflowing the flex line!I’ll shrink

These things will happen completely separately.

12Plenty of space,ask if flex items want to growFlex items overflow, ask if they’ll shrink

The rough steps of resolving the flexible size of flex items

Check if there’s any remaining space in a flex line.

1?

If there’s space left, give the flex items the chance to grow.

1Wanna grow?

If the flex items are overflow a flex line, ask them to shrink.

2Would you mind shrinking?

How to check if there’s remaining space

Add up the margin box sizes of all the flex items. That’s the same size you used when collecting flex items into flex lines in a previous chapter. If a flex item has an auto margin, treat that as 0 for now.

132++outer sizeouter sizeouter size

Add up the space taken by the gaps inserted by the flex container.

2gapgap+

Subtract these two from the length of the flex line.

123length of flex lineRemaining space

By default, flex items don’t grow

By default, the value of flex-grow is "0", which means the flex items "don’t want to" grow.

1Wanna grow?No, thank you!flex-grow: 0INITIAL PROPERTY VALUE

By default, flex items shrink

The initial value of flex-shrink is 1, which means the flex items agree to shrink and that they all shrink at the same rate (because they all have the same factor of flex-shrink)

11Could you shrink, please?No problemo!INITIAL PROPERTY VALUEflex-shrink: 1

Flex items grow and shrink from their flex base size.

You could have three flex items that all want to grow at the same rate.

flex-grow: 1flex-grow: 1flex-grow: 1Yeah,,pleaseMe, too,!!

But if they all asked for a different flex base size, they will end up with different sizes even if you add the same amount of growth to each.

40px150px123123150px

If you want to make sure all flex items end up with the exact same size, make sure they start from the same flex base size.

40px123123

Flex items will stop shrinking when they reach their minimum size.

The minimum size is set by whichever sizing property corresponds to the main axis. For example, in English for a row flex container the main axis is horizontal, so the corresponding property would be min-inline-size.

11min-inline-size: 170pxSorry

The initial value of minimum size properties like min-inline-size is auto. This means different things depending on the type of layout being used.

Apples and Bananasmin-inline-size: auto

In Flexbox layout, this auto min size is derived from the size of the content inside a flex item.

Apples and Bananasmin-inline-size: autoI can’t shrink anymore because of Bananas

Flex items will stop growing when they reach their maximum size.

Just like with the minimum size property, you can specify a maximum size property to limit how big your flex items can get.

max-inline-size

The initial value of maximum size properties is none. This means that unless you set a different value, the flex items will not stop growing.

INITIAL PROPERTY VALUEmax-inline-size: none

You can make your flex items inflexible and prevent any growing or shrinking

You can do this by making sure flex-grow: 0 and flex-shrink: 0 are set.

1flex-grow: 0flex-shrink: 0I won’t play alongI will not grow and I will not shrink

The flex shorthand property.

Instead of separately setting flex-grow, flex-shrink, and flex-basis, you can set all three properties at once with the flex shorthand.

flex-growflex-shrinkflex-basisflex

For example, flex: 0 1 auto sets flex-grow: 0, flex-shrink: 1, and flex-basis: auto. Which are all initial values of these properties.

flex: 0 1 autoflex-grow: 0flex-shrink: 1flex-basis: auto

flex “shortcuts”

You can say things like flex: none, which is the same as setting flex: 0 0 auto. This is useful when you want to make a flex item inflexible.

flex: noneflex-grow: 0flex-shrink: 0flex-basis: auto1I won’t growor shrink

Or, you can set flex: 1, which sets flex: 1 1 0. This is useful when you want to make sure all flex items

012

How the growing and shrinking works in detail