The flex-wrap
property controls whether flex items are forced onto a single line or can wrap onto multiple lines if there isn't enough space. This is crucial for creating responsive layouts that adapt to different screen sizes.
Example 1: No Wrapping (Default)
HTML
<div class="flex-container no-wrap">
<div class="item">One</div> <div class="item">Two</div> <div class="item">Three</div>
<div class="item">Four</div> <div class="item">Five</div> <div class="item">Six</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.no-wrap {
flex-wrap: nowrap; /* This is the default; items will shrink to fit on one line. */
}
.item { width: 150px; } /* Items have a fixed width. */
Explanation
By default, flex-wrap
is nowrap
. This means all flex items will be forced to stay on a single line, even if it causes them to overflow the container or shrink.
Example 2: Wrapping onto Multiple Lines
HTML
<div class="flex-container wrap">
<div class="item">One</div> <div class="item">Two</div> <div class="item">Three</div>
<div class="item">Four</div> <div class="item">Five</div> <div class="item">Six</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.wrap {
flex-wrap: wrap; /* Allows items to wrap to a new line if needed. */
}
.item { width: 150px; }
Explanation
Setting flex-wrap: wrap
allows the flex items to wrap onto additional lines if they exceed the width of the container. Items will wrap from top to bottom.
Example 3: Reverse Wrapping
HTML
<div class="flex-container wrap-reverse">
<div class="item">One</div> <div class="item">Two</div> <div class="item">Three</div>
<div class="item">Four</div> <div class="item">Five</div> <div class="item">Six</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.wrap-reverse {
flex-wrap: wrap-reverse; /* Items wrap onto new lines from bottom to top. */
}
.item { width: 150px; }
Explanation
flex-wrap: wrap-reverse
behaves like wrap
, but new lines are added from the bottom of the container upwards instead of top-to-bottom. This creates a "stacked from the bottom" effect.
Example 4: A Responsive Grid of Cards
HTML
<div class="card-container">
<div class="card">Card 1</div> <div class="card">Card 2</div>
<div class="card">Card 3</div> <div class="card">Card 4</div>
</div>
CSS
.card-container {
display: flex;
flex-wrap: wrap; /* Ensures cards wrap on smaller screens. */
border: 2px solid #d9534f;
}
.card {
flex: 1 1 200px; /* Grow, shrink, with a base width of 200px. */
margin: 10px;
height: 100px;
background: #f7e6e5;
}
Explanation
This is a classic responsive pattern. flex-wrap: wrap
allows the cards to form a grid. As the viewport narrows, cards that don't fit will wrap to the next line automatically.
Example 5: Tag Cloud Layout
HTML
<div class="tag-cloud">
<span class="tag">HTML</span> <span class="tag">CSS</span> <span class="tag">JavaScript</span>
<span class="tag">Flexbox</span> <span class="tag">Grid</span> <span class="tag">Responsive</span>
<span class="tag">Web Design</span>
</div>
CSS
.tag-cloud {
display: flex;
flex-wrap: wrap; /* Allows tags to flow naturally onto multiple lines. */
padding: 10px;
border: 2px solid #5bc0de;
}
.tag {
background-color: #e8f7fc;
padding: 5px 10px;
margin: 5px;
border-radius: 4px;
}
Explanation
flex-wrap: wrap
is ideal for creating tag clouds or lists of chips. The tags will naturally flow and wrap based on the available width of the container, creating a clean and compact layout.
Example 6: Combining Wrap with Column Direction
HTML
<div class="multi-column-container">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div> <div class="item">6</div>
<div class="item">7</div> <div class="item">8</div> <div class="item">9</div>
</div>
CSS
.multi-column-container {
display: flex;
flex-direction: column; /* Main axis is vertical. */
flex-wrap: wrap; /* Allows wrapping into new columns. */
height: 150px; /* The container must have a fixed height for this to work. */
border: 2px solid #f0ad4e;
}
.item { height: 40px; width: 100px; }
Explanation
When you combine flex-direction: column
with flex-wrap: wrap
, the items will fill the container's height and then wrap into a new column. This requires a defined height on the flex container.
Example 7: Preventing Wrap for a Squeezed Layout
HTML
<div class="squeezed-layout">
<div class="item-no-shrink">Fixed</div>
<div class="item-grows">Stretches</div>
<div class="item-shrinks">This item shrinks</div>
</div>
CSS
.squeezed-layout {
display: flex;
flex-wrap: nowrap; /* Explicitly prevent wrapping. */
width: 400px;
border: 2px solid #333;
}
.item-no-shrink { flex-shrink: 0; } /* This item will not shrink. */
.item-grows { flex-grow: 1; } /* This item will grow to fill space. */
.item-shrinks { background-color: #eee; }
Explanation
Here, flex-wrap: nowrap
is used to force all items onto one line. This demonstrates how individual flex item properties (flex-shrink
, flex-grow
) interact with the container's wrapping behavior to create specific layout effects.