CSS
Which CSS Property Configures Multiple Lines in a Flex Container?
When using CSS Flexbox, items are arranged in a single row by default. But what if the container doesn’t have enough space to fit all the items in one row? That’s where the flex-wrap
property comes into play.
The flex-wrap
property allows flex items to wrap onto multiple lines when they exceed the width of the flex container. This is crucial for responsive layouts and ensuring content does not overflow.
What is the flex-wrap
Property?
The flex-wrap
property in CSS Flexbox determines whether the flex container should force all items into a single line or allow them to wrap onto multiple lines when needed.
Syntax:
.container {
display: flex;
flex-wrap: nowrap | wrap | wrap-reverse;
}
Property Values:
Value | Description |
---|---|
nowrap (default) | All flex items are forced into a single line, even if they overflow. |
wrap | Flex items wrap onto new lines when necessary. |
wrap-reverse | Similar to wrap , but items wrap in reverse order (bottom to top). |
Examples of flex-wrap
in Action
1. Default Behavior (nowrap
) – Items Stay in One Line
By default, flex-wrap
is set to nowrap
, meaning all flex items will be placed in a single row even if they overflow the container.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flex Wrap Example</title>
<style>
.container {
display: flex;
flex-wrap: nowrap;
background-color: lightgray;
padding: 10px;
}
.box {
width: 100px;
height: 100px;
background-color: steelblue;
color: white;
display: flex;
align-items: center;
justify-content: center;
margin: 5px;
font-size: 18px;
}
</style>
</head>
<body>
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>
</body>
</html>
Output:
All boxes will stay in one row even if they overflow the container.
2. Using flex-wrap: wrap
– Items Move to New Lines
When flex-wrap: wrap
is applied, items will move to the next line if there’s not enough space.
.container {
display: flex;
flex-wrap: wrap;
}
🔹 Behavior:
- The boxes will wrap onto the next line when the container runs out of space.
- This helps create a responsive layout.
✅ Best for: Image galleries, responsive cards, and grids.
3. Using flex-wrap: wrap-reverse
– Items Wrap in Reverse
The wrap-reverse
value makes items wrap onto multiple lines but in reverse order (from bottom to top).
.container {
display: flex;
flex-wrap: wrap-reverse;
}
🔹 Behavior:
- Items will still wrap, but the first row will appear at the bottom, and new rows will stack above it.
✅ Best for: Special layouts that require an inverted stacking order.
Combining flex-wrap
with flex-direction
The flex-wrap
property works together with flex-direction
.
Example:
.container {
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
🔹 Behavior:
- Items will wrap vertically instead of horizontally.
- This is useful when designing vertical navigation menus or sidebars.
When to Use flex-wrap
?
✅ Use flex-wrap: wrap;
when:
- You want a responsive layout.
- You have many elements inside a flex container.
- You need to prevent content overflow.
❌ Do NOT use flex-wrap: wrap;
when:
- You want all items forced into a single row (use
nowrap
instead). - You prefer a scrollable container rather than a wrapped layout.
Conclusion
The flex-wrap
property in CSS Flexbox is essential for controlling how flex items behave when they exceed the container’s width. It determines whether they stay in one row or wrap onto multiple lines.
✅ Key Takeaways:
- Use
flex-wrap: wrap;
to allow items to wrap when needed. - Use
flex-wrap: wrap-reverse;
if you want the wrapped items to appear in reverse order. - Combine
flex-wrap
withflex-direction
for better control over layouts.
By mastering flex-wrap
, you can create dynamic, flexible, and responsive layouts without relying on floats or complex CSS hacks.