Table of Contents

Flex - flex-wrap (Single Line or Multi-Line)

About

A flex container can be either single-line or multi-line, depending on the flex-wrap property.

Example multi-line container

This three examples are based on a row of div an shows the effect of the flex-wrap value. They just differs in their flex-wrap and flex-direction value (Ie known as the flex-flow)

They share all:

<div class="flex">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
</div>
.flex {
  display: flex;
  margin:2rem;
  background: #9decf9;
}
.item {
  width: 80px;
  height: 20px;
  background-color: SkyBlue;
  border: 1px solid #e7e7e7;
  text-align: center;
  box-sizing: border-box;
}

default: nowrap

The default value is nowrap and does not take the width into account.

.flex {
  flex-direction: row;
  width: 300px;
  /* flex-wrap: nowrap; */
}

wrap a row

wrap a row constrained on width

.flex {
  flex-direction: row;
  flex-wrap: wrap; 
  width: 300px;
}

wrap a row constrained on viewport

Below we have included the code in a iframe with a size of 300px and the behavior is the same.

When it wraps:

.flex {
  flex-direction: row;
  flex-wrap: wrap; 
}

wrap-reverse a row

.flex {
  flex-direction: row;
  flex-wrap: wrap-reverse;
  width: 300px;
}

wrap-reverse a row-reverse

.flex {
  flex-direction: row-reverse;
  flex-wrap: wrap-reverse;
  width: 300px;
}

wrap a column

Note that a size via the height and width should be given

.flex {
  flex-direction: column;
  flex-wrap: wrap;
  width: 300px;
  height: 60px; 
}

wrap-reverse a column

.flex {
  flex-direction: column;
  flex-wrap: wrap-reverse;
  width: 300px;
  height: 60px; 
}

Management

Set

You can set flex-wrap also via the shorthand notation flex-flow property

Documentation / Reference