Explain it simply
Explain it simply
If container properties are the bus rules, item properties are each passenger's wishes. flex-basis is the starting size: "I need this much room". flex-grow answers "how many shares of any spare room do I take?" and flex-shrink answers "how much do I give up when it gets tight?"
order moves one passenger forward or back in the queue, and align-self lets one passenger sit differently from everyone else. All of these are written on the child item, not on the container.
Problem → Why → Solution
The problem: the content column squeezes the sidebar and overflows the screen
A layout has a sidebar with width: 200px and content with flex: 1. Once a <pre> block holding a long URL goes into the content, the content column grows wider than the container and spills out of it (on a full-width layout the page starts scrolling sideways), while the sidebar is squeezed far below 200px, down to the width of the word "Menu". If flex: 1 means "take only the remaining space", why does this happen?
.layout { display: flex; }
.sidebar { width: 200px; } /* flex-shrink is still 1 */
.content { flex: 1; } /* min-width is still auto */
/* <main class="content"><pre>https://example.com/very/long/url...</pre></main> */Why does this happen?
A flex item's min-width defaults to auto, which means "never shrink below the content's min-content size". The unbreakable URL has a huge min-content width, so the content column cannot shrink. Space runs out, and the only item still allowed to shrink, the sidebar with its default flex-shrink: 1, gets squeezed. The fix is min-width: 0 on the content and flex: 0 0 200px on the sidebar.
Core concept: start at the basis, then grow or shrink
The browser sizes flex items along the main axis in two steps. First it takes each item's flex-basis. With auto, the default, it uses the item's width (height in a column), and if there is none, the size of its content. Second, it subtracts all the bases, plus any gaps, from the container's size: a positive remainder is grown into, a negative one is shrunk away.
Growing is simple: free space is split in the ratio of the flex-grow values. In a 600px container, items with bases of 100px (grow 1) and 200px (grow 2) share 300px of free space 1:2, ending at 100 + 100 = 200px and 200 + 200 = 400px. Note that grow 2 means "take two shares of the free space", not "be twice as wide".
Shrinking is weighted by the basis, so large items give up more and small items do not vanish. Two 200px items in a 300px container overflow by 100px. With shrink 1 and 1 each loses 50px, ending at 150px; with shrink 1 and 3 they lose 25px and 75px, ending at 175px and 125px. An item with flex-shrink: 0 never shrinks. One more floor applies: a flex item's min-width defaults to auto, so it will not shrink below its content's smallest possible width unless you set min-width: 0 or give the item an overflow other than visible.
| Shorthand | Expands to | Meaning |
|---|---|---|
| flex: initial (default) | 0 1 auto | Does not grow, may shrink |
| flex: 1 | 1 1 0% | Equal shares, content ignored |
| flex: auto | 1 1 auto | Content size plus equal extra |
| flex: none | 0 0 auto | Fixed at its natural size |
- Start from flex-basis
- Free = container - bases - gaps
- Positive: share by grow ratio
- Negative: shrink by shrink x basis
- Clamp to min-width / max-width
If an item will not shrink, suspect min-width: auto before blaming flex-shrink.
General pattern
item {
flex: grow shrink basis; /* e.g. 1 1 240px */
min-width: 0; /* allow shrinking below content */
order: integer; /* visual order only, default 0 */
align-self: auto | flex-start | center | flex-end | stretch;
}Writing the flex shorthand with all three values is the clearest form. order shows the lowest number first, and align-self overrides the container's align-items for one item.
Example: pricing cards with a featured card
Assume the HTML has three cards, Basic, Pro and Team, all with the pricing-card class, and Pro also has featured-card. The container is a display: flex row 1000px wide.
.pricing-card {
flex: 1 1 280px;
}
.featured-card {
order: -1;
flex-grow: 1.3;
}Code breakdown
- Line 2
flex: 1 1 280px;Every card starts from a 280px basis. 3 x 280 = 840px leaves 160px of free space for the growing cards. In a container narrower than 840px, all cards shrink by the same amount.
- Line 5
.featured-card {The Pro card has both classes with equal specificity, so this later rule overrides flex-grow, while shrink and basis still come from .pricing-card.
- Line 6
order: -1;The other cards keep the default order 0, so -1 moves Pro to the visual front. Tab and screen-reader order stay Basic, Pro, Team, as in the HTML.
- Line 7
flex-grow: 1.3;The 160px of free space is split 1.3 : 1 : 1, so Pro gains about 63px and the others about 48.5px each. Pro is not 1.3 times as wide; it is only about 15px wider.
In a 1000px row the Pro card moves to the front (far left) at about 343px wide, followed by Basic and Team at about 328.5px each.When to use align-self and order
align-self aligns one item on the cross axis differently from the rest. For example, while the container keeps the default stretch, an ad box with align-self: flex-start is only as tall as its content instead of the whole column. That matters for sticky boxes, because a stretched box has no room left to stick in.
Use order with care
order only changes the visual order. It is fine for cosmetic changes such as moving a featured card forward, but where sequence carries meaning, such as form fields or step-by-step instructions, change the HTML instead.
In real projects
Where this shows up in real projects
The skeleton of most dashboards is a flex: 0 0 240px sidebar beside a flex: 1; min-width: 0 main area. Forget min-width: 0 and one wide table, chart or code block bursts the whole layout. Search bars typically give the input flex: 1 and the button flex: none, so the input takes all the remaining room while the button stays exactly as wide as its label.
Card rows combine a shorthand such as flex: 1 1 280px with flex-wrap to become responsive without any media query. Chat apps align your own bubbles right with align-self: flex-end and other people's left with flex-start. On mobile, order inside a media query is often used to lift a product photo above its text, which is safe only because the photo is decorative.
Common mistakes
Common mistakes
1. Forgetting min-width: 0
When a long URL, a <pre> or text with text-overflow: ellipsis inside a flex item shows no ellipsis or bursts the layout, give the item min-width: 0 (min-height: 0 in a column).
2. Expecting width alone to fix a size
A sidebar with only width: 200px still has flex-shrink: 1, so it shrinks when space is tight. For a truly fixed size, write flex: 0 0 200px.
3. Mixing up flex: 1 and flex: auto
For equal-width tabs, use flex: 1 (basis 0). With flex: auto (basis auto), a tab with a longer label ends up wider: in a 600px row, "Hi" and a long label become about 227px and 373px instead of 300px each.
Best practices
Best practices
- Prefer the flex shorthand with all three values over separate grow, shrink and basis properties, so nobody has to guess the defaults of the parts you left out.
- Pair flex: 1 with min-width: 0 on every main column that should take the remaining space.
- Every time you use order, tab through the page once with the keyboard.
Mini challenge
Mini challenge: sidebar, content and ad
Size three columns inside an 800px flex row using item properties only.
- .sidebar is exactly 200px and neither grows nor shrinks
- .content takes all remaining width and is not widened by the long URL
- The <pre> scrolls inside itself instead of overflowing sideways
- .ad starts at 160px, may shrink, never grows, and is not stretched to the row's height
<style>
.layout { display: flex; gap: 16px; width: 800px; }
/* TODO: .sidebar = exactly 200px, never grows or shrinks */
/* TODO: .content = all remaining width, even with the long URL */
/* TODO: pre = scroll inside instead of overflowing */
/* TODO: .ad = starts at 160px, may shrink, never grows, not stretched */
</style>
<div class="layout">
<nav class="sidebar">Menu</nav>
<main class="content">
<h1>Release notes</h1>
<pre>https://example.com/downloads/2026/a-very-long-unbroken-file-name.zip</pre>
</main>
<aside class="ad">Ad</aside>
</div>Expected: The sidebar is 200px, the content 408px (800 - 200 - 160 - two 16px gaps) and the ad 160px. The URL scrolls sideways inside the content, and the ad is only as tall as its "Ad" text.
Show hint
flex: 1 alone is not enough for the content; fix its min-width too. To stop the ad stretching, use align-self on the item rather than touching the container.
Show solution
<style>
.layout { display: flex; gap: 16px; width: 800px; }
.sidebar {
flex: 0 0 200px;
background: #f1f5f9;
}
.content {
flex: 1;
min-width: 0;
}
pre {
overflow-x: auto;
background: #0f172a;
color: #e2e8f0;
}
.ad {
flex: 0 1 160px;
align-self: flex-start;
background: #fef3c7;
}
</style>
<div class="layout">
<nav class="sidebar">Menu</nav>
<main class="content">
<h1>Release notes</h1>
<pre>https://example.com/downloads/2026/a-very-long-unbroken-file-name.zip</pre>
</main>
<aside class="ad">Ad</aside>
</div>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- An item starts at its flex-basis, gains free space by grow ratio and loses overflow by shrink x basis.
- flex: 1 means equal shares, flex: auto means content size plus extra, flex: 0 0 200px means a fixed size.
- When an item refuses to shrink, add min-width: 0 (min-height: 0 in a column).
- align-self aligns a single item on the cross axis; order changes only the visual order.
Basic concepts of Flexbox — MDN Web Docs