LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
HTML & CSS

Flexbox Overflow, Wrapping and Common Layout Problems

A flex container keeps all its items on one line by default (flex-wrap: nowrap). When the items are too wide, they shrink first, down to their minimum size. If they still do not fit, they overflow the container.

Flexbox Overflow, Wrapping and Common Layout Problems

Quick Answer: A flex container keeps all its items on one line by default (flex-wrap: nowrap). When the items are too wide, they shrink first, down to their minimum size. If they still do not fit, they overflow the container. Use flex-wrap: wrap to let items move onto new lines, gap to space them, and align-content to place the lines. Free space is shared separately on each line, so flex-grow can stretch the last line. Long text inside a flex item needs min-width: 0 so the item is allowed to be narrower than its content.

S1. Why Do We Need to Understand Overflow and Wrapping?

Imagine a row of five product cards. Each card is 160px wide, and the container is 600px wide. Together the cards need 800px.

On a screen that is wide enough, nothing looks wrong. On a narrower screen, the cards stick out of the container, or the whole page starts to scroll sideways.

Would you write a media query for every possible number of cards per row? That grows quickly, and it still breaks when the content changes.

CSS Flexbox gives us a simpler tool: let the items wrap onto new lines. Let's first see exactly what happens when they cannot.

S2. The Problem: Items That Do Not Fit on One Line

Overflow = content that sticks out beyond the edge of its container.

Imagine a 600px wide flex container with five items. Each item is 160px wide and 40px tall, and each one has flex: 0 0 160px, which means it may not shrink. The container uses the default flex-wrap: nowrap.

Example 1: Five fixed-size items in one line

Assumption: In the output diagrams, one character is about 20px wide and one row is about 20px tall. A to E stand for the five items. A | in the middle of a row marks the right edge of the 600px container.

<!-- HTML -->
<div class="row">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
    <div class="item">D</div>
    <div class="item">E</div>
</div>

/* CSS */
.row {
    display: flex;
    width: 600px;
}

.item {
    flex: 0 0 160px;
    height: 40px;
    background: #E2E8F0;
}

Expected output

|AAAAAAAABBBBBBBBCCCCCCCCDDDD|DDDDEEEEEEEE|
|AAAAAAAABBBBBBBBCCCCCCCCDDDD|DDDDEEEEEEEE|

Items need 5 x 160 = 800px, and the container is 600px wide.
flex-shrink is 0, so the items cannot get smaller.
200px stick out past the right edge: half of D and all of E.

The page renders, but part of the content is outside its container. Nothing told the items they may move to a second line.

What goes wrong?

  1. Content sticks out: Items past the container edge may be cut off or overlap what comes next.
  2. Sideways scrolling: The page or a parent can get a horizontal scrollbar, which is hard to use on a phone.
  3. Query for every count: Developers add a media query for each number of items per row.
  4. Hidden problems: Adding overflow: hidden to a parent hides the items instead of fixing the layout.
  5. Script fixes: JavaScript counts the items and moves them into new rows, which adds code for something CSS can do.

Now think about tag lists, card grids and toolbars on one website. Each of them can run out of room, and each one needs a plan for it.

Remember: Flexbox is not broken. A flex container is single-line by default, so it keeps every item on one line. The real problem is that nobody allowed the items to wrap.

Animation 1: How a single line forces overflow

Five items, one line

The red highlights show the items that cannot shrink and cannot move.

🧱Items5 x 160px = 800pxCannot shrink
📦ContainerWidth: 600pxOne line only
⚠️Result200px past the edgeOverflow
 
.row (flex-wrap: nowrap) 800px of items · 600px of room
No wrapping allowed

The animation highlights the items, the container and the overflow that results.

S3. The Solution: Let Items Wrap onto New Lines

Flexbox gives us a simpler design.

wrapping = setting flex-wrap: wrap so a flex container can break its items into several lines when they do not fit on one.

Instead of forcing everything onto one line, let the container start a new line.

  • The browser puts as many items on a line as fit, using each item's size (its flex-basis, limited by any min and max size).
  • The next item that does not fit starts a new line, and each line is laid out on its own.
  • The HTML stays the same, and no media query is needed for this.

Now three items fit on the first line, and the other two move to the second line.

Example 2: The same items with wrapping

Assumption: This example uses standard CSS supported by all modern browsers, and uses the same HTML as Example 1.

/* CSS (same HTML as Example 1) */
.row {
    display: flex;
    flex-wrap: wrap;
    width: 600px;
}

.item {
    flex: 0 0 160px;
    height: 40px;
    background: #E2E8F0;
}

Expected output

|AAAAAAAABBBBBBBBCCCCCCCC      |
|AAAAAAAABBBBBBBBCCCCCCCC      |
|DDDDDDDDEEEEEEEE              |
|DDDDDDDDEEEEEEEE              |

Line 1: three items need 3 x 160 = 480px. A fourth would need 640px, which is too much.
Line 2: D and E need 320px.
Everything is inside the 600px container, and nothing overflows.

Notice the difference. The items keep their size, and the container decides how many fit on each line.

Animation 2: The wrapping design

Items fill a line, then start a new one

Each line is laid out separately inside the same container.

1️⃣Line 1A, B and C480px of 600px
2️⃣Line 2D and E320px of 600px
📦ContainerWidth: 600pxNothing sticks out
 
 
 
.rowflex-wrap: wrap
.itemflex: 0 0 160px
Result2 lines · no overflow
Items moved to a new line

The animation highlights the two lines and the rule that creates them.

Compare both approaches

Single-line flex compared with wrapping
One line (nowrap) With flex-wrap: wrap
Items must stay on one line. Items move to new lines when they do not fit.
Fixed-size items can overflow. Fixed-size items stay inside the container.
The page can scroll sideways. The page keeps its width.
A media query is needed for each count. The browser decides how many fit on each line.
Overflow is hidden or scripted away. Layout intent is clear: this container may wrap.

The idea to remember

  • Default = flex-wrap: nowrap. One line, so items shrink first and may then overflow.
  • Wrap = flex-wrap: wrap. When an item does not fit, it starts a new line.
  • Per line = each line is its own row of items, and free space is shared on each line separately.
  • Allowed to shrink = a flex item cannot be narrower than its content unless you set min-width: 0.
  • Fix the cause = use wrapping or flexible sizes, not overflow: hidden, to solve overflow.

S4. Step-by-Step Code Breakdown

1. Know why items overflow

When the items are wider than the container, the browser has a negative amount of free space. It first shrinks the items that are allowed to shrink, by default flex-shrink: 1. An item stops shrinking at its minimum size, which by default is the size of its content. An item with flex-shrink: 0 does not shrink at all. If the items still do not fit, they overflow.

Properties for wrapping and spacing lines
Property and value Where it is set What it does
flex-wrap: nowrap Container The default. One line, so items may overflow.
flex-wrap: wrap Container Items that do not fit start a new line.
flex-wrap: wrap-reverse Container Items wrap, and new lines are added in the opposite cross direction.
flex-flow Container A shorthand for flex-direction and flex-wrap, for example row wrap.
gap Container Space between items and between lines.
align-content Container Places the lines on the cross axis. It only matters when there is more than one line.

2. Space wrapped items with gap

The gap property adds space between items on a line and between lines. It does not add space at the outer edges. A line fits only as many items as fit with their gaps included. Here, three items and two gaps need 480 + 40 = 520px, which fits. Four items and three gaps need 700px, which does not.

Example 3: Wrapping with a 20px gap

/* CSS (same HTML as Example 1) */
.row {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    width: 600px;
}

.item {
    flex: 0 0 160px;
    height: 40px;
}

Expected output

|AAAAAAAA BBBBBBBB CCCCCCCC    |
|AAAAAAAA BBBBBBBB CCCCCCCC    |
|                              |
|DDDDDDDD EEEEEEEE             |
|DDDDDDDD EEEEEEEE             |

Each gap is 20px, which is one character wide and one row tall.
Line 1 holds A, B and C: 3 x 160 + 2 x 20 = 520px.
The 20px row gap puts a blank row between the two lines.

3. See how growth works on each line

Free space is calculated for each line, so flex-grow shares it line by line. In this example each item has flex: 1 1 160px. The items grow to fill their line, so a line with fewer items gets wider items.

Example 4: Items that grow on a wrapped container

/* CSS (same HTML as Example 1) */
.row {
    display: flex;
    flex-wrap: wrap;
    width: 600px;
}

.item {
    flex: 1 1 160px;
    height: 40px;
}

Expected output

|AAAAAAAAAABBBBBBBBBBCCCCCCCCCC|
|AAAAAAAAAABBBBBBBBBBCCCCCCCCCC|
|DDDDDDDDDDDDDDDEEEEEEEEEEEEEEE|
|DDDDDDDDDDDDDDDEEEEEEEEEEEEEEE|

Line 1: free space = 600 - 480 = 120px, so each item grows by 40px to 200px.
Line 2: free space = 600 - 320 = 280px, so each item grows by 140px to 300px.
The last line has wider items than the first line.

4. Cap the growth of the last line

The wide last line in Example 4 is often not wanted. A max-width stops an item from growing beyond a limit, and the unused space stays empty. A cap of 200px matches the size items reach on a full line.

Example 5: Growing items with a maximum width

/* CSS (same HTML as Example 1) */
.row {
    display: flex;
    flex-wrap: wrap;
    width: 600px;
}

.item {
    flex: 1 1 160px;
    max-width: 200px;
    height: 40px;
}

Expected output

|AAAAAAAAAABBBBBBBBBBCCCCCCCCCC|
|AAAAAAAAAABBBBBBBBBBCCCCCCCCCC|
|DDDDDDDDDDEEEEEEEEEE          |
|DDDDDDDDDDEEEEEEEEEE          |

Line 1: each item grows by 40px and reaches the 200px maximum.
Line 2: each item would grow to 300px, but it stops at 200px.
The remaining 200px on line 2 stays empty.

If the columns must line up exactly, or the last line must follow the same grid, CSS Grid is a better fit than wrapped flexbox.

5. Let long content shrink inside an item

A flex item cannot become narrower than its content by default. A long name or an unbroken string can therefore push its neighbors out of the container. Setting min-width: 0 allows the item to be smaller than its content, and then text can be cut with an ellipsis.

Example 6: A user row with a long name

<!-- HTML -->
<div class="user">
    <div class="avatar">A</div>
    <div class="name">A very long display name that does not fit</div>
    <button class="follow">Follow</button>
</div>

/* CSS */
.user { display: flex; width: 300px; }

.avatar { width: 40px; height: 40px; flex-shrink: 0; }
.follow { width: 80px; flex-shrink: 0; }

.name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

Expected output

|AANNNNNNNNNBBBB|
|AANNNNNNNNNBBBB|

A is the avatar, N is the name and B is the button.
Free space for the name = 300 - 40 - 80 = 180px.
The name is cut with an ellipsis at 180px, and the button stays inside the row.

Setting overflow to a value other than visible also removes the automatic minimum size. Writing min-width: 0 as well makes the intent clear, and it is required when the item itself does not clip its content.

6. Place the lines with align-content

When a wrapping container is taller than its lines, align-content decides where the extra space goes. Its default behaves like stretch, which shares the extra space between the lines. Items with a fixed height do not stretch, so the space shows up as gaps below each line.

Example 7: A tall wrapping container

/* CSS (same HTML as Example 1) */
.row {
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    width: 600px;
    height: 160px;
}

.item {
    width: 160px;
    height: 40px;
}

Expected output

With align-content: flex-start
|AAAAAAAABBBBBBBBCCCCCCCC      |
|AAAAAAAABBBBBBBBCCCCCCCC      |
|DDDDDDDDEEEEEEEE              |
|DDDDDDDDEEEEEEEE              |
|                              |
|                              |
|                              |
|                              |

With the default align-content (stretch)
|AAAAAAAABBBBBBBBCCCCCCCC      |
|AAAAAAAABBBBBBBBCCCCCCCC      |
|                              |
|                              |
|DDDDDDDDEEEEEEEE              |
|DDDDDDDDEEEEEEEE              |
|                              |
|                              |

Extra cross space = 160 - 80 = 80px.
By default it is shared between two lines, so each line is 80px tall.
With flex-start, the lines stay together at the top.

S5. Architecture and Real-World Trade-Offs

Wrapping lets each part of a page respond to the space it has without a media query. This is useful in tag lists, card grids and toolbars.

How the design fits a website

Imagine a website with a tag list, a product grid and a list of users. Each one handles a lack of room in its own way.

A focused overflow-handling flow

Each website area uses the rule that fits its content.

🏷️Tag listTags wrap onto new lines
🛍️Product gridCards grow, but stop at a maximum
👤User rowA long name is cut with an ellipsis
 
 
 
.tagsflex-wrap: wrap · gap: 8px
.productflex: 1 1 160px · max-width: 200px
.namemin-width: 0 · text-overflow: ellipsis

The animation traces each website area to the rule that keeps it inside its container.

Benefits for larger websites

  • Fewer media queries: The browser decides how many items fit on a line.
  • Maintainability: Adding or removing items does not require new breakpoints.
  • Safe layouts: Content stays inside the container, even when the text or the count changes.
  • Clear intent: flex-wrap: wrap states that this container may use several lines.
  • Cleaner spacing: gap spaces items and lines without margins on the outer edges.

Important trade-offs

  • Each line is laid out on its own, so items on different lines do not line up in columns. Use CSS Grid when columns must align.
  • With flex-grow, a line with fewer items gets wider items, so the last line can look different.
  • The height of a line is set by its tallest item, so mixed heights create uneven lines.
  • Wrapping changes only the visual layout. The reading order and the keyboard order still follow the HTML order.
  • overflow: hidden on a parent hides overflow without removing it, and content can be lost.
  • Long unbroken strings may need overflow-wrap in addition to min-width: 0.
  • Wrapping and media queries work well together: use wrapping for the number of items per line, and media queries for larger changes in structure.

Important: Wrapping does not change how flexbox shares space. It only decides which items belong on which line. Inside each line, the same free-space rules apply, so check the size of the items and the line they land on when a layout looks wrong.

S6. Common Mistakes and Best Practices

Mistake 1: Expecting items to wrap by default

Wrong: Make a row of fixed-size cards with display: flex and expect extra cards to go to a second line.

Why it fails: The default is flex-wrap: nowrap. All items stay on one line, so they shrink, or overflow if they cannot shrink.

Better: Add flex-wrap: wrap to the container, and use gap for the spacing.

Rule: Items wrap only when the container says so.

Mistake 2: Expecting long text to shrink on its own

Wrong: Put a long unbroken name in a flex item and expect the item to become narrower than the name.

Why it fails: A flex item's automatic minimum size is the size of its content, so the item stays wide and pushes its neighbors.

Better: Set min-width: 0 on the item, and use overflow, text-overflow or overflow-wrap to handle the text.

Rule: An item must be allowed to be smaller than its content.

Mistake 3: Using flex-grow on wrapped items and getting a stretched last line

Wrong: Give every item flex-grow: 1 in a wrapping container and expect all lines to look the same.

Why it fails: Free space is shared on each line. A line with fewer items gives each of them a larger share.

Better: Add a max-width to the items, or use CSS Grid for a strict grid.

Rule: Growth works on one line at a time.

Mistake 4: Setting a container height and being surprised by gaps

Wrong: Give a wrapping container a fixed height and expect the lines to sit together at the top.

Why it fails: The default align-content shares the extra height between the lines, which spreads the lines apart.

Better: Use align-content: flex-start, or let the container take its height from its content.

Rule: Extra cross space goes to the lines, so place the lines on purpose.

Mistake 5: Hiding overflow instead of fixing it

Wrong: Add overflow: hidden to the container or the page to make the sideways scrollbar disappear.

Why it fails: The items are still too wide. They are only cut off, so people lose access to content.

Better: Find the item that is too wide, and then allow wrapping, allow shrinking, or limit its size.

Rule: Fix the cause of the overflow, not its symptom.

S7. How This Comes Up in Interviews

Question 1: Why do flex items overflow instead of wrapping?

What the interviewer wants: They want to know whether you understand the default behavior of a flex container.

Strong answer:

"A flex container is single-line by default, because flex-wrap is nowrap. The browser first shrinks the items down to their minimum size. If they still do not fit, they overflow. To get new lines I set flex-wrap: wrap."

Question 2: What is the difference between align-items and align-content?

Strong answer:

"align-items places the items inside each line on the cross axis. align-content places the lines themselves inside the container, and it only has an effect when there is more than one line and extra cross space."

Question 3: Why does text-overflow: ellipsis sometimes not work inside a flex item?

Strong answer:

"The flex item has an automatic minimum width equal to its content, so it never becomes narrow enough to clip the text. I set min-width: 0 on the item. The text also needs overflow hidden and white-space: nowrap for the ellipsis to show."

Question 4: How do you stop the last line of a wrapped flex layout from stretching?

Strong answer:

"Each line shares its own free space, so a short last line gives its items more room. I put a max-width on the items to cap their growth. If the layout needs a strict grid, I use CSS Grid instead."

What should different experience levels explain?

  • Junior developer: Explain flex-wrap: wrap and show items moving to a second line.
  • Senior developer: Explain shrinking down to the minimum size, per-line free space, and why min-width: 0 fixes content that will not shrink.
  • Technical architect: Explain when a team should use wrapped flexbox, when it should use Grid, and how conventions for overflow keep components safe in a design system.

S8. Hands-On Practice

Beginner: Fix the overflow

Create a 600px wide flex container with five items, each with flex: 0 0 160px. Observe the overflow. Then add flex-wrap: wrap and gap: 20px.

Expected result: First the items stick out of the container. After the change, three items sit on the first line and two on the second, with 20px between them.

Intermediate: Tame the last line

Change the items to flex: 1 1 160px and observe the second line. Then add max-width: 200px.

Hint: Work out the free space on each line before you open the page.

Expected result: First the items on the second line are 300px wide. With the maximum width, all five items are 200px wide, and the second line has empty space at the end.

Advanced: Build a safe user list

Build a list of user rows like Example 6, with an avatar, a name and a button. Test it with very long names, and with a browser zoom of 200%. Use your browser's developer tools to find which element is widest when something overflows.

Expected result: Names are cut with an ellipsis, buttons stay inside the row, and the page never scrolls sideways.

S9. Next Steps and Community

Previous article: Building Responsive Layouts Using Flexbox and Media Queries.

Next article: Flexbox Debugging, Browser Developer Tools and Best Practices.

Continue the series by learning how to find the cause of a layout problem quickly with your browser's developer tools, and how to follow a set of best practices that prevents most flexbox problems.

Try the examples in your own HTML and CSS project. Change the number of items, switch wrapping on and off, test long text, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.

Give items room to wrap. Let content shrink.

Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.