Wrap Elements Inside of a Flexed Container with `flex-wrap`

Chris Achard
InstructorChris Achard

Share this video with your friends

Send Tweet
Published 3 years ago
Updated a year ago

To set the wrap settings of elements inside of a container, we can set the flex-wrap css attribute. The most common values are: nowrap, wrap, or wrap-reverse

Instructor: [0:00] We have our three boxes that are flexed all the way across the screen, but what if our container had a width to it? We could set a width to 150 pixels. Now it smooshes the boxes so that they fit within the container.

[0:14] To get around that, we can set the flex-wrap style. The default is nowrap. That gets us exactly what we have here. It tries not to wrap things and instead smooshes them to fit. We could set that to wrap. Now it wraps them so that they come one after another.

[0:32] This is a little hard to see with the sizes we have. Let's make a few more red boxes here. Now you can see it's wrapped them instead of just putting them in one row or trying to smoosh them.

[0:43] Remember the space between them is controlled by this justify-content. We could, for example, say flex-end for justify-content and take everything to the end here, and it still wraps normally.

[0:55] There's one more wrap we might use, which is wrap-reverse. Now we'll reverse the order of everything in the container, which is kind of a neat idea. It starts at the bottom, and then wraps upwards.