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.
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:
- CSS
- HTML
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;
}
<section>
<div>1</div>
<div>2</div>
<div>3</div>
</section>
<section>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</section>
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:
- CSS
- HTML
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;
}
<section>
<div>1</div>
<div>2</div>
<div>3</div>
</section>
<section>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</section>
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.
wrap-reverse
does the same thing as wrap
—but in reverse order.
Here's an example:
- CSS
- HTML
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;
}
<section>
<div>1</div>
<div>2</div>
<div>3</div>
</section>
<section>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</section>
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.