Understanding Flexbox Sizing, Min-Width and Max-Width
Quick Answer: A flex item starts from a base size (its flex-basis, or its width when the basis is auto). The container then shares the free space by growing or shrinking the items, and min-width and max-width have the final say as a floor and a ceiling. By default, a flex item cannot shrink below the size of its content, because min-width is auto. Set min-width: 0 to let it shrink further, and set max-width to stop it growing too large.
S1. Why Do We Need to Understand Flexbox Sizing?
Imagine a dashboard row with three panels. You give each panel flex: 1, expecting three equal columns. Then one panel contains a wide image, and the columns are no longer equal. The page may even scroll sideways.
In the previous article, we learned how gap and auto margins control the space around items. But before any spacing, the browser has to decide how wide each item actually is.
Would you fix this by setting a fixed width on every panel? That works for one screen size, but it breaks as soon as the container or the content changes.
CSS Flexbox solves this with a clear sizing model and two limit properties. Let's first see what happens when we ignore the limits.
S2. The Problem: Items That Do Not Size the Way You Expect
Automatic minimum size = the smallest size a flex item will shrink to on the main axis. By default (min-width: auto), it is set by the item's content.
Imagine a 450px wide row with three items, each set to flex: 1. The middle item contains a block that is 250px wide, such as an image or a table.
Example 1: Three flex: 1 items, one with wide content
Assumption: The 250px block stands in for content that cannot shrink, such as an image, a table or a long unbroken word. In the output diagrams, one character is about 10px wide.
<!-- HTML -->
<div class="row">
<div class="item">A</div>
<div class="item"><div class="wide">B</div></div>
<div class="item">C</div>
</div>
/* CSS */
.row {
display: flex;
width: 450px;
}
.item { flex: 1; }
.wide { width: 250px; }
Expected output
|AAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCC| flex: 1 alone would give 450px / 3 = 150px to each item. B cannot shrink below its 250px content. A and C share the remaining 200px: 100px each.
The page renders, but the columns are not equal. The item with the widest content wins, and the others are squeezed.
What goes wrong?
- Unequal columns:
flex: 1does not mean equal widths when an item's content has a larger minimum size. - Squeezed neighbours: The other items are forced into whatever space is left.
- Overflow: If the minimum sizes add up to more than the container, the row spills out and can cause sideways scrolling.
- Fixed-width patches: Developers give every item a fixed width, which then breaks on other screen sizes.
- Poor clarity: The CSS does not say how small or large an item is allowed to become.
Now think about many dashboards, card rows and toolbars on one website. Each one holds content of unknown size, such as user text, images and tables.
Remember: Flexbox is not broken. It protects the content by never shrinking an item below its automatic minimum size. The real problem is that nobody set the limits on purpose.
Animation 1: How content decides the size
Three equal items, unequal results
The red highlights show wide content overriding the sizes we expected.
The animation highlights the items and the content that overrides their sizes.
S3. The Solution: Set the Limits with min-width and max-width
Flexbox gives us a simpler design.
Flex base size = the starting size of an item, taken from flex-basis, or from width when flex-basis is auto.
min-width and max-width = limits on the size an item may end up with, whatever growing and shrinking suggest.
Instead of fixed widths, tell the items what their limits are.
min-width: 0removes the content-based floor, so the item can shrink below its content.min-width: 190pxsets a floor of your own, so the item never gets smaller than that.max-width: 100pxsets a ceiling, so a growing item stops at that size.- An
overflowvalue decides what happens to content that no longer fits.
Now the three items share the row equally, and the wide content stays inside its own item.
Example 2: Equal columns with min-width: 0
Assumption: This example uses standard CSS supported by all modern browsers. The wide content now scrolls inside its item instead of widening it.
.row {
display: flex;
width: 450px;
}
.item {
flex: 1;
min-width: 0;
overflow: auto;
}
.wide { width: 250px; }
Expected output
|AAAAAAAAAAAAAAABBBBBBBBBBBBBBBCCCCCCCCCCCCCCC| Each item is 450px / 3 = 150px wide. B's 250px content no longer widens the item. It scrolls inside B's 150px box.
Notice the difference. The HTML stays the same, and one limit on the items gives the equal columns we wanted.
Animation 2: The sizing design
Base size, shared space, final limits
The container shares the space, and the limits have the final say.
The animation highlights the sizing steps and the limits that settle the result.
Compare both approaches
| Without explicit limits | With min-width and max-width |
|---|---|
| Content sets the smallest size. | You set the smallest size. |
| Growing items can become very wide. | A max-width caps the growth. |
| Wide content can stop columns from being equal. | Equal columns hold, and the content scrolls or clips. |
| Fixed widths are used as a workaround. | Sizes stay flexible within safe limits. |
| Sizing intent is hidden. | Sizing intent is clear. |
The idea to remember
- Base size = where an item starts:
flex-basis, orwidthif the basis isauto. - Grow and shrink = the container shares free space (grow) or removes overflow (shrink).
- min-width = a floor. The default
autouses the content size. - max-width = a ceiling. It stops a growing item.
- Limits win = the final size is always clamped between min and max.
S4. Step-by-Step Code Breakdown
1. Follow the four sizing steps
The full algorithm has more detail, but this simplified version explains most of what you see. The container is assumed to be a row.
| Step | What happens | Controlled by |
|---|---|---|
| 1. Base size | Each item gets a starting size. | flex-basis, or width, or the content |
| 2. Free space | The container size minus all base sizes, gaps and margins. | The container and its items |
| 3. Grow or shrink | Positive free space is shared by growth. Negative free space is removed by shrinking. | flex-grow (default 0) and flex-shrink (default 1) |
| 4. Clamp | An item that goes past a limit is fixed at the limit, and the rest is shared again. | min-width and max-width |
2. See shrinking in action
With the default flex-shrink: 1, items that are too wide for the container shrink in proportion to their base sizes. Equal items shrink equally.
Example 3: Three 200px items in a 450px container
<!-- HTML -->
<div class="row">
<div class="item">A</div>
<div class="item">B</div>
<div class="item">C</div>
</div>
/* CSS */
.row { display: flex; width: 450px; }
.item { width: 200px; }
Expected output
|AAAAAAAAAAAAAAABBBBBBBBBBBBBBBCCCCCCCCCCCCCCC| Base sizes: 3 x 200px = 600px. The container is 450px. Negative free space: 150px, shrunk equally: 50px per item. Each item ends at 150px, not the 200px we wrote.
If you want the items to keep 200px, set flex-shrink: 0. They will then overflow the container instead of shrinking.
3. Set a floor with min-width
A min-width stops an item from shrinking below a size you choose. The other items give up more space instead.
Example 4: A floor on the middle item
/* HTML: same three items as Example 3 */
/* CSS */
.row { display: flex; width: 450px; }
.item { width: 200px; }
.b { min-width: 190px; }
Expected output
|AAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCC| Shrinking would give B 150px, but B's floor is 190px. B stays at 190px. A and C share the remaining 260px: 130px each.
4. Set a ceiling with max-width
A max-width stops a growing item at a size you choose. The free space it cannot use goes to the other growing items.
Example 5: A ceiling on the first item
/* HTML: same three items A, B, C */
/* CSS */
.row { display: flex; width: 440px; }
.item { flex: 1; }
.a { max-width: 100px; }
Expected output
|AAAAAAAAAABBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCC| An equal share would be 440px / 3 = about 147px each. A stops at its 100px ceiling. B and C share the remaining 340px: 170px each.
5. Understand the automatic minimum size
The surprise in Example 1 comes from min-width: auto. On the main axis, it makes an item refuse to shrink below its content. This is a safe default, but it can hide the reason for overflow. There are three common ways to deal with it.
- Set
min-width: 0on the item, so it may shrink below its content. - Set
overflowtohiddenorautoon the item. An item that clips its content no longer uses the content-based minimum. - Let long text break with
overflow-wrap: anywhere, so the content itself can become narrower.
6. Truncate text with min-width: 0
A common real use is a row with an icon, a long title and a button. The title should take the free space and cut off with an ellipsis instead of pushing the button away.
Example 6: A title that truncates
Assumption: The exact point where the text is cut depends on the font.
<!-- HTML -->
<div class="header">
<span class="icon"></span>
<span class="title">Understanding Flexbox Sizing in CSS</span>
<button class="btn">Open</button>
</div>
/* CSS */
.header { display: flex; width: 360px; }
.icon { width: 40px; flex-shrink: 0; }
.btn { width: 80px; flex-shrink: 0; }
.title {
flex: 1;
min-width: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Expected output
|IIIIUnderstanding Flexbox S…BBBBBBBB| The icon (40px) and the button (80px) keep their size. The title takes the remaining 240px and ends with an ellipsis. Without min-width: 0, the long text would push the button out.
S5. Architecture and Real-World Trade-Offs
Limits make a layout predictable when the content is not. They are useful in dashboards, sidebars, article layouts and card headers.
How the design fits a website
Imagine a website with a sidebar layout, a card header and an article column. Each one uses a different limit.
A focused sizing flow
Each website area uses the limit that suits its content.
The animation traces each website area to its own sizing limit.
Benefits for larger websites
- Predictability: The layout holds up when the content changes.
- No overflow surprises:
min-width: 0removes the most common cause of sideways scrolling in flex rows. - Readable lines: A
max-widthkeeps text columns from becoming too wide. - Flexible sizes: You set safe limits and let the container do the rest.
- Less patching: There is no need for fixed widths on every item.
Important trade-offs
min-width: 0can clip or hide content, so decide what should happen to it withoverflowortext-overflow.- A
max-widthcan leave free space unused, which then needs a home, for example withjustify-contentor an auto margin. - A large
min-widthcan cause overflow when the container is narrow. - If both
widthandflex-basisare set,flex-basiswins on the main axis. - In a column, the same ideas apply to
min-heightandmax-height.
Important: Flex sizing starts with a base size, shares the free space, and then lets min-width and max-width make the final decision. When a flex item has a surprising size, check the limits first.
S6. Common Mistakes and Best Practices
Mistake 1: Expecting flex: 1 items to always be equal
Wrong: Give three items flex: 1 and expect three equal columns whatever they contain.
Why it fails: An item cannot shrink below its content-based minimum, so wide content makes its column wider.
Better: Add min-width: 0 to the items, and decide how the content should behave.
Rule: Equal columns need a floor of zero.
Mistake 2: Using min-width: 0 without handling the overflow
Wrong: Add min-width: 0 and leave everything else alone.
Why it fails: The item can now be narrower than its content, so the content spills out and can overlap the next item.
Better: Add overflow: auto or hidden, or let the text wrap or truncate.
Rule: Decide where the extra content goes.
Mistake 3: Expecting a width to be fixed
Wrong: Write width: 200px on every item and expect each to stay 200px.
Why it fails: The default flex-shrink: 1 shrinks items when the container is too small, so the width is only a starting size.
Better: Use flex-shrink: 0 to stop shrinking, or min-width to set a floor.
Rule: In Flexbox, a width is a suggestion.
Mistake 4: Expecting max-width to remove the leftover space
Wrong: Set max-width on the only growing item and expect the row to look full.
Why it fails: The item stops at its ceiling, and the free space it could not use stays empty unless other items grow.
Better: Place the leftover space on purpose, with justify-content or an auto margin, or let another item grow.
Rule: A ceiling does not remove space. It moves the question.
Mistake 5: Setting width and flex-basis with different values
Wrong: Write width: 200px and flex-basis: 100px on the same item, and expect 200px.
Why it fails: When flex-basis is not auto, it replaces width as the base size on the main axis.
Better: Pick one. Use the flex shorthand with a basis for flexible items.
Rule: One base size per item.
S7. How This Comes Up in Interviews
Question 1: How does Flexbox decide the size of an item?
What the interviewer wants: They want to know whether you understand the sizing steps.
Strong answer:
"Each item starts from its flex base size, which is flex-basis, or width if the basis is auto. The container measures the free space, grows the items if there is extra, or shrinks them if there is too little. Then min-width and max-width clamp the result, and any freed-up space is shared again."
Question 2: Why does a flex: 1 item refuse to shrink?
Strong answer:
"Because min-width defaults to auto for flex items, which sets the smallest size from the content. If the content is wide, the item stays wide. I set min-width: 0, or an overflow value other than visible, and decide how the content should behave."
Question 3: What is the difference between width and flex-basis?
Strong answer:
"Both can give the starting size on the main axis. When flex-basis is auto, the item uses its width. When flex-basis is set to anything else, it wins over width. Either way, the result is only a starting point, because the item can still grow or shrink."
Question 4: How do you truncate long text in a flex item with an ellipsis?
Strong answer:
"I give the text item flex: 1 and min-width: 0, so it can shrink below its content. Then I add white-space: nowrap, overflow: hidden and text-overflow: ellipsis. The neighbouring items get flex-shrink: 0 so they keep their size."
What should different experience levels explain?
- Junior developer: Explain that items can shrink, and show
min-widthandmax-widthas limits. - Senior developer: Explain the base size, free space, growing, shrinking and clamping steps, and the automatic minimum size.
- Technical architect: Explain how sizing conventions, such as
min-width: 0on flexible text items, keep components stable across a design system.
S8. Hands-On Practice
Beginner: Watch items shrink
Create a 450px wide flex container with three items, each width: 200px. Observe their final widths, then add flex-shrink: 0 and observe again.
Expected result: First each item is 150px wide. Then each stays 200px and the row overflows the container.
Intermediate: Add a floor and a ceiling
Give the middle item min-width: 190px in the shrinking row. Then build a row of three flex: 1 items in a 440px container and give the first item max-width: 100px.
Hint: Work out the sizes by hand before you check them in your browser tools.
Advanced: Fix the unequal columns and truncate a title
Reproduce Example 1 with a 250px wide block in the middle item. Make the columns equal again with min-width: 0 and overflow: auto. Then build a row with an icon, a long title and a button, and make the title truncate with an ellipsis.
Expected result: The three columns are 150px each, and the long title ends with an ellipsis without moving the button.
S9. Next Steps and Community
Previous article: Gap, Auto Margins and Spacing in CSS Flexbox.
Next article: Nested Flex Containers and Complex Layout Structures.
Continue the series by learning how flex containers can be placed inside other flex containers, and how to combine them into larger and more complex layout structures.
Try the examples in your own HTML and CSS project. Change the container width, add wide content, try different limits, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.
Set the limits on purpose. Then the sizes follow.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.