Stretching flex lines with : stretch
After an initial round of sizing flex items and flex lines, the flex container can make the flex lines stretch along the cross axis to fill the flex container box
Imagine you’re writing a novel. You have a blank page, with a paragraph of text on it. The paragraph takes up maybe a third of the height of the page.
The size of the text is big enough for you to read. But the lines of text are spaced so close to one another that the paragraph is tiring to read.
You change the paragraph text settings, and double the line spacing. Now, the text is easier to read. There’s more space between each line of text.
Now imagine that instead of doubling the line spacing, you instead told your writing app to “stretch the lines of text vertically so that they fill the entire height of the page”.
Before your paragraph was taking up a third of the page, and now it’s taking up the entire height of the page. But you have the same number of lines of text. It’s just these lines of text got super tall, and now there’s lots of spacing between the text of each line.
This is basically what : stretch
does. But, instead of stretching lines of text, you’re stretching flex lines with flex items in them.
doesn’t do anything if the flex container is single-line, which is the default.
The flex container is single-line by default because the initial value of the property is
nowrap
. This means that flex items are not allowed to wrap, and instead are all to be collected in a single flex line.
A single-line flex container only has a single flex line, and that flex line has the same size as the flex container box. So there’s nothing to align or stretch.
does do something if the flex container is multi-line.
A flex container is considered multi-line if it allows its flex items to wrap into multiple flex lines. That can be set with : wrap
or : wrap-reverse
.
It doesn’t matter if the flex items actually wrap. You could have a flex container with : wrap
and a single flex item that doesn’t need to wrap. That flex container is still considered multi-line.
You’re not stretching the flex items, just the flex lines.
Flex items can be stretched to fill their flex line along the cross axis. In fact, they do by default.
But that behavior is controlled by another property, align-self
and .