HomeGuideLabGlossary

Allowing flex items to wrap

By default, the flex container is single-line and doesn’t allow flex items to wrap into another line.

The flex container will collect all flex items into the same flex line no matter how big those flex items say they want to be.

134Single-line flex container2

This might mean that the flex items will be forced to shrink.

123456Sorry, you’ll have to squeeze!

If the flex items can’t shrink anymore, they will overflow the flex container box. This is actually what you want when you create horizontally scrollable lists of cards, for example.

5312467This is fine, you can scroll to see us!

The line in which flex items are laid out is called a flex line.

These flex lines are like training wheels, they’re there to help the browser figure out where all the flex items should be laid out.

1Flex containerFlex lineFlex item

Flex lines are just a concept, they don’t exist in the DOM tree or in the box tree.

.flex-container.flex-itemFlex lineI’m imaginaryNo flex line

The browser uses the idea of flex lines to calculate the size and position of all the flex items, and then discards any information it has about those flex lines.

1122Figure out the layout with flex linesDisplay just the flex container and flex items

If the flex container doesn’t allow wrapping, that single flex line has the same size as the flex container box.

A lot of the time you’ll be working with flex containers that don’t allow wrapping. This means there’s only one flex line, and it has the same size of the flex container.

I’m the same size as you no matter what!Uh, sure, cool

The flex container controls if the flex items are allowed to wrap into a new flex line with .

By default, the flex container doesn’t allow its flex items to wrap, because : nowrap.

123flex-wrap: nowrapCan I wrap into another flex line?nowrap, sorry!

The flex container can allow flex items to wrap with : wrap.

That makes the flex container multi-line.

Flex items will get to say what size they’d like to have, and if they can’t fit on an existing flex line, they will be collected into a new flex line

2213Can I wrap into a new flex line?wrap ahead!flex-wrap: wrap3Awesome, thanks!

Aside from letting flex items wrap, wrap and wrap-reverse affect the direction in which flex lines will stack

That direction is called the cross direction.

321Cross directionFlex linesthis way, please!

With wrap, the flex lines will stack in the same direction as lines of text in a column of text.

The direction in which rows stack in a columnRow of textRow of textRow of textCross directionflex-wrap: wrap31

With wrap-reverse, the flex lines will stack in the opposite direction to lines of text in a column of text.

flex-wrap: wrap-reverseRow of text321The direction in which rows stack in a columnRow of textRow of textCross direction