🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Flex-wrap

AI & DATA SCIENCE // flex-wrap

The flex-wrap property controls whether flex items are forced onto a single line, potentially shrinking to fit, or allowed to wrap onto multiple lines when they collectively exceed the container's size.

Syntax

flex-wrap: nowrap | wrap | wrap-reverse;

Deep Dive Course

nowrap, the default, keeps every flex item on a single line, shrinking items according to their flex-shrink value if they collectively don't fit, potentially causing overflow if items can't shrink enough. wrap instead allows items to flow onto additional lines once they no longer fit on the current one, similar to how words wrap onto a new line of text, and wrap-reverse does the same but stacks the wrapped lines in reverse order. This is commonly combined with flex-direction: row into the shorthand flex-flow, and is essential for building a responsive grid-like layout of flex items that should reflow onto multiple rows as available space shrinks.

1Understanding Flex-wrap

nowrap, the default, keeps every flex item on a single line, shrinking items according to their flex-shrink value if they collectively don't fit, potentially causing overflow if items can't shrink enough. wrap instead allows items to flow onto additional lines once they no longer fit on the current one, similar to how words wrap onto a new line of text, and wrap-reverse does the same but stacks the wrapped lines in reverse order. This is commonly combined with flex-direction: row into the shorthand flex-flow, and is essential for building a responsive grid-like layout of flex items that should reflow onto multiple rows as available space shrinks.

💡

Set flex-wrap: wrap whenever a row of flex items should be allowed to reflow onto multiple lines on smaller screens, rather than being forced to shrink indefinitely or overflow the container under the default nowrap.

editor.html
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
localhost:3000

2Practical Example

Here is a real-world application of Flex-wrap showing how it is used in production CSS code.

editor.html
.rigid-row {
  display: flex;
  flex-wrap: nowrap;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Flex-wrap:

1. Set flex-wrap: wrap for a responsive row of items, like cards or tags, that should reflow onto additional lines rather than shrinking indefinitely or overflowing on smaller screens

2. Combine flex-wrap: wrap with a gap declaration for consistent spacing between both items and the wrapped lines themselves

3. Remember flex-wrap's default, nowrap, can cause items to overflow their container if they collectively can't shrink enough to fit on a single line

⚠️

Tip: Set flex-wrap: wrap whenever a row of flex items should be allowed to reflow onto multiple lines on smaller screens, rather than being forced to shrink indefinitely or overflow the container under the default nowrap.

editor.html
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
localhost:3000

Examples

Example 01Basic Usage
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
Example 02Advanced Example
.rigid-row {
  display: flex;
  flex-wrap: nowrap;
}

Best Practices

  • Set flex-wrap: wrap for a responsive row of items, like cards or tags, that should reflow onto additional lines rather than shrinking indefinitely or overflowing on smaller screens
  • Combine flex-wrap: wrap with a gap declaration for consistent spacing between both items and the wrapped lines themselves
  • Remember flex-wrap's default, nowrap, can cause items to overflow their container if they collectively can't shrink enough to fit on a single line

Interview Question

Why might flex items overflow their container's boundaries under the default flex-wrap: nowrap, even though flexbox is generally designed to flexibly resize items?

Hint: Think about whether every flex item is actually capable of shrinking indefinitely to fit, or whether there's some floor below which an item refuses to shrink further.

Under nowrap, flexbox attempts to fit all items onto a single line by shrinking them according to each item's flex-shrink value and their content's own minimum size constraints, but an item generally can't shrink smaller than its own content's minimum intrinsic size, like the length of an unbreakable word or a fixed minimum width explicitly set on that item, without further explicit overrides like min-width: 0. If the combined minimum sizes of all the items on that forced single line still exceed the container's available width, flexbox has genuinely run out of room to shrink anything further, and the excess simply overflows the container's boundaries instead, since nowrap has explicitly told it not to solve the space problem by wrapping items onto additional lines. This is exactly the situation flex-wrap: wrap is designed to prevent, allowing items that don't fit on the current line to flow onto a new one instead, distributing the layout problem across multiple lines rather than forcing an potentially-impossible shrink onto a single one.

Exercises

MediumPractice using Flex-wrap in a real scenario.
View Solution
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

Frequently Asked Questions

Why might flex items overflow their container's boundaries under the default flex-wrap: nowrap, even though flexbox is generally designed to flexibly resize items?

Under nowrap, flexbox attempts to fit all items onto a single line by shrinking them according to each item's flex-shrink value and their content's own minimum size constraints, but an item generally can't shrink smaller than its own content's minimum intrinsic size, like the length of an unbreakable word or a fixed minimum width explicitly set on that item, without further explicit overrides like min-width: 0. If the combined minimum sizes of all the items on that forced single line still exceed the container's available width, flexbox has genuinely run out of room to shrink anything further, and the excess simply overflows the container's boundaries instead, since nowrap has explicitly told it not to solve the space problem by wrapping items onto additional lines. This is exactly the situation flex-wrap: wrap is designed to prevent, allowing items that don't fit on the current line to flow onto a new one instead, distributing the layout problem across multiple lines rather than forcing an potentially-impossible shrink onto a single one.

Related Functions

Display-flexFlex-shrinkAlign-content