Flex wrap helps items in a container move to the next line when there is not enough space. This keeps your layout neat and readable.
Flex wrap in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
flex-wrap: nowrap | wrap | wrap-reverse;nowrap means all items stay in one line (default).
wrap lets items move to the next line if needed.
wrap-reverse wraps items but in reverse order vertically.
Examples
CSS
flex-wrap: nowrap;CSS
flex-wrap: wrap;CSS
flex-wrap: wrap-reverse;Sample Program
This example shows a flex container with five items. The flex-wrap: wrap; property lets items move to the next line if they don't fit in the container's width. Try resizing the browser window to see the wrapping effect.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Flex Wrap Example</title> <style> body { font-family: Arial, sans-serif; padding: 1rem; } .container { display: flex; flex-wrap: wrap; gap: 1rem; background-color: #f0f0f0; padding: 1rem; border: 2px solid #ccc; max-width: 20rem; } .item { background-color: #4a90e2; color: white; padding: 1rem; flex: 0 0 8rem; text-align: center; border-radius: 0.5rem; user-select: none; } </style> </head> <body> <h1>Flex Wrap Example</h1> <p>Resize the browser window to see items wrap to the next line.</p> <section class="container" aria-label="Flex wrap container"> <div class="item" tabindex="0">Item 1</div> <div class="item" tabindex="0">Item 2</div> <div class="item" tabindex="0">Item 3</div> <div class="item" tabindex="0">Item 4</div> <div class="item" tabindex="0">Item 5</div> </section> </body> </html>
Important Notes
Use flex-wrap with display: flex; on the container.
Combine flex-wrap with gap to add space between items.
Remember to test on different screen sizes to ensure good wrapping behavior.
Summary
Flex wrap controls if flex items stay in one line or wrap to new lines.
Use wrap to keep your layout neat on small screens.
Try resizing your browser to see how wrapping changes the layout.
Practice
1. What does the CSS property
flex-wrap: wrap; do in a flex container?easy
Solution
Step 1: Understand the role of
Theflex-wrapflex-wrapproperty controls whether flex items stay in one line or wrap to new lines.Step 2: Identify the effect of
The valuewrapvaluewrapallows items to move to the next line if they don't fit in one row, preventing overflow.Final Answer:
It allows flex items to move to the next line if they don't fit in one row. -> Option AQuick Check:
Flex wrap = wrap items to next line [OK]
Hint: Wrap means items jump to next line if needed [OK]
Common Mistakes:
- Thinking wrap hides items
- Confusing wrap with centering
- Assuming wrap forces one line
2. Which of the following is the correct CSS syntax to make flex items wrap onto multiple lines?
easy
Solution
Step 1: Recall valid values for
The valid values areflex-wrapnowrap,wrap, andwrap-reverse.Step 2: Identify the correct syntax for wrapping
The correct syntax to allow wrapping isflex-wrap: wrap;. Other options are invalid or incorrect.Final Answer:
flex-wrap: wrap; -> Option BQuick Check:
Correct syntax for wrap = flex-wrap: wrap; [OK]
Hint: Remember exact spelling: wrap, not no-wrap [OK]
Common Mistakes:
- Using 'no-wrap' instead of 'nowrap'
- Adding extra words like 'wrap-all'
- Confusing wrap with nowrap
3. Given this CSS and HTML, what will happen when the browser width is too small to fit all items in one row?
div.container {
display: flex;
flex-wrap: wrap;
width: 300px;
}
span.item {
width: 140px;
flex-shrink: 0;
height: 50px;
background: lightblue;
margin: 5px;
}<div class='container'>
<span class='item'>1</span>
<span class='item'>2</span>
<span class='item'>3</span>
</div>medium
Solution
Step 1: Calculate space needed for items in one row
Each item is 140px wide plus 5px margin on each side (total 150px). Two items fit in 300px width, but three do not.Step 2: Understand effect of
Because wrapping is allowed, the third item moves to the next line instead of overflowing.flex-wrap: wrap;Final Answer:
Items 1 and 2 stay in the first row; item 3 moves to the next line. -> Option CQuick Check:
Wrap lets items jump to next line when no space [OK]
Hint: Wrap moves extra items to new line when no space [OK]
Common Mistakes:
- Assuming all items stay in one line
- Thinking items get hidden
- Ignoring margin space in width calculation
4. You wrote this CSS but the flex items are not wrapping as expected:
What is the most likely reason the items stay in one line and shrink?
div.box {
display: flex;
flex-wrap: wrap;
width: 200px;
}
span.box-item {
width: 150px;
height: 40px;
background-color: coral;
}What is the most likely reason the items stay in one line and shrink?
medium
Solution
Step 1: Check the container width and item widths
The container is 200px wide, each item is 150px wide, so two items can fit side by side.Step 2: Understand wrapping behavior
Since two items fit in 200px, no wrapping is needed. Items stay in one line and shrink further if more items exist.Final Answer:
The container width is too large to force wrapping. -> Option AQuick Check:
Wrapping happens only if items exceed container width [OK]
Hint: Wrapping needs items wider than container width [OK]
Common Mistakes:
- Assuming margin is required for wrap
- Thinking height affects wrapping
- Forgetting container width limits
5. You want a flex container that wraps items but reverses the order of wrapped lines (the new lines appear above the first line). Which CSS property and value combination achieves this?
hard
Solution
Step 1: Understand
The valuewrap-reversebehaviorwrap-reversemakes wrapped lines stack in reverse order, with new lines above the first.Step 2: Check other options for correctness
reverse-wrapis invalid. Usingflex-direction: row-reversereverses item order horizontally but not line stacking.nowrapdisables wrapping.Final Answer:
flex-wrap: wrap-reverse; -> Option DQuick Check:
Wrap reverse stacks lines from bottom up [OK]
Hint: Use wrap-reverse to flip wrapped lines order [OK]
Common Mistakes:
- Using invalid property values
- Confusing flex-direction with wrap direction
- Forgetting wrap disables wrapping
