Flex items request flex base size
Flex items get to say what size they’d like to be before any growing or shrinking happens
Flexbox is all about having flex items that grow and shrink. But if they are asked to shrink, what size do they shrink down from? There has to be a size that those flex items would be if they weren’t asked to shrink, right?
This size is called a flex base size, and is set with the flex-basis
property.
flex-basis
is a property that sets that requested size for each flex item.
The cool thing about it is that it always refers to the size in the main axis of the flex container.
You don’t need to figure out if you should use inline-size
/block-size
, or width
/height
. You just use this one property.
If you change the from
row
to column
, your flex items will still be requesting the same size.
The flex base size is used to check if a flex item will fit on a flex line in multi-line flex containers.
If the flex container doesn’t allow flex items to wrap, then all flex items end up on a single flex line regardless of this size.
If wrapping is allowed, then this size is used to figure out if each flex item can fit on a flex line. If it can’t, it wraps into a new flex line.
By default, flex items request a flex base size based on the content inside those flex items.
By default, flex-basis
has a value of auto
. This means “check my value of the size property which corresponds to the main direction”.
Because in English text in a line/row of text goes from left to right by default, the corresponding size would be set by the inline-size
property.
The default value of inline-size
is auto
.
When both flex-basis
and the main size property are auto
, then this size will be derived from the contents of the flex item.
Sizing a flex item based on its content.
A lot of the time, for a paragraph of text this will be the max-content
size.
For an image, the max-content
size would be the natural size of the image.
You can use this property to control how many flex items there are on a flex line.
Because this size affects if a flex item fits into a flex line, you can use it to ensure there’s an exact number of flex items in a flex line