Skip to main content

flex-wrap in CSS – How to Wrap Overflown Flex Items

flex-wrap specifies whether browsers should wrap overflown flexible items onto multiple lines.

The flex-wrap property accepts the following values:

  • nowrap
  • wrap
  • wrap-reverse

Let's discuss the three values below.

What Is flex-wrap: nowrap in CSS Flexbox?

nowrap is flex-wrap's default value. It forces all items within a flexible container into a single line (that is, row-wise or column-wise direction).

In other words, nowrap tells browsers not to wrap a flexible container's items.

note

Suppose the total width (or height) of all the items in a flexible container is greater than the flexbox's width (or height). In such a case, nowrap will cause the elements to overflow out of the container.

Here's an example:

section {
width: 130px;
display: flex;
flex-wrap: nowrap;
background-color: orange;
margin: 10px;
padding: 7px;
}

div {
background-color: purple;
color: white;
margin: 5px;
padding: 10px;
border-radius: 5px;
}

Try Editing It

The snippet above used nowrap to force browsers to lay out the flexible containers' items in a single line.

What Is flex-wrap: wrap in CSS Flexbox?

wrap moves all overflown items within a flexible container to the next line.

In other words, wrap tells browsers to wrap a flexible container's overflown items.

Here's an example:

section {
width: 130px;
display: flex;
flex-wrap: wrap;
background-color: orange;
margin: 10px;
padding: 7px;
}

div {
background-color: purple;
color: white;
margin: 5px;
padding: 10px;
border-radius: 5px;
}

Try Editing It

We used wrap to wrap the flexible containers' overflown items to the next line.

What Is flex-wrap: wrap-reverse in CSS Flexbox?

wrap-reverse moves all overflown items within a flexible container to the next line in reverse order.

note

wrap-reverse does the same thing as wrap—but in reverse order.

Here's an example:

section {
width: 130px;
display: flex;
flex-wrap: wrap-reverse;
background-color: orange;
margin: 10px;
padding: 7px;
}

div {
background-color: purple;
color: white;
margin: 5px;
padding: 10px;
border-radius: 5px;
}

Try Editing It

We used wrap-reverse to wrap the flexible containers' overflown items to the next line in reverse order.

Overview

This article discussed what a CSS flex-wrap property is. We also discussed the three values it accepts.

Your support matters: Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.

Join CodeSweetly Newsletter