Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Flex Wrap with CSS Flexbox
📖 Scenario: You are creating a simple product gallery for a website. The gallery should show product boxes in a row. When the screen is too narrow, the boxes should wrap to the next line instead of shrinking too small.
🎯 Goal: Build a flex container that wraps its child product boxes onto multiple lines when needed, using CSS flex-wrap.
📋 What You'll Learn
Create a flex container with exactly three child boxes
Set the container to use flex layout
Add the CSS property flex-wrap: wrap; to allow wrapping
Style the boxes with a fixed width and height so wrapping is visible
💡 Why This Matters
🌍 Real World
Flexbox wrapping is used in real websites to create galleries, menus, and card layouts that adjust nicely on phones and desktops.
💼 Career
Understanding flex-wrap is essential for front-end developers to build flexible, user-friendly interfaces that work on all devices.
Progress0 / 4 steps
1
Create the HTML structure
Write the HTML code to create a <div> with class container that contains exactly three child <div> elements each with class box.
CSS
Hint
Use a <div> with class container and inside it add three <div> elements with class box.
2
Set up the flex container
In CSS, select the class .container and set its display property to flex.
CSS
Hint
Use display: flex; inside the .container CSS rule.
3
Add flex-wrap property
Add the CSS property flex-wrap: wrap; inside the .container CSS rule to allow the child boxes to wrap onto the next line when needed.
CSS
Hint
Write flex-wrap: wrap; inside the .container CSS block.
4
Style the boxes with fixed size
Add CSS rules for the class .box to set width to 10rem, height to 10rem, background color to #4CAF50, margin to 0.5rem, and text color to white.
CSS
Hint
Set fixed width and height with 10rem, background color to green #4CAF50, margin 0.5rem, and text color white inside .box.
Practice
(1/5)
1. What does the CSS property flex-wrap: wrap; do in a flex container?
easy
A. It allows flex items to move to the next line if they don't fit in one row.
B. It forces all flex items to stay in a single line, even if they overflow.
C. It hides flex items that don't fit in the container.
D. It centers all flex items horizontally.
Solution
Step 1: Understand the role of flex-wrap
The flex-wrap property controls whether flex items stay in one line or wrap to new lines.
Step 2: Identify the effect of wrap value
The value wrap allows 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 A
Quick 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
A. flex-wrap: nowrap;
B. flex-wrap: wrap;
C. flex-wrap: no-wrap;
D. flex-wrap: wrap-all;
Solution
Step 1: Recall valid values for flex-wrap
The valid values are nowrap, wrap, and wrap-reverse.
Step 2: Identify the correct syntax for wrapping
The correct syntax to allow wrapping is flex-wrap: wrap;. Other options are invalid or incorrect.
Final Answer:
flex-wrap: wrap; -> Option B
Quick 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?
What is the most likely reason the items stay in one line and shrink?
medium
A. The container width is too large to force wrapping.
B. You forgot to set flex-wrap to wrap.
C. The items have no margin or padding, so they don't wrap.
D. The container has no height set, so wrapping is disabled.
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 A
Quick 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
A. flex-wrap: wrap; flex-direction: row-reverse;
B. flex-wrap: reverse-wrap;
C. flex-wrap: nowrap; flex-direction: column;
D. flex-wrap: wrap-reverse;
Solution
Step 1: Understand wrap-reverse behavior
The value wrap-reverse makes wrapped lines stack in reverse order, with new lines above the first.
Step 2: Check other options for correctness
reverse-wrap is invalid. Using flex-direction: row-reverse reverses item order horizontally but not line stacking. nowrap disables wrapping.
Final Answer:
flex-wrap: wrap-reverse; -> Option D
Quick Check:
Wrap reverse stacks lines from bottom up [OK]
Hint: Use wrap-reverse to flip wrapped lines order [OK]