What happens if you set flex: 1 on all child elements in a flex container?

What happens when you apply:
flex: 1;
JavaScriptto all child elements inside a flex container.
β
What Happens If You Set flex: 1 on All Child Elements in a Flex Container?
π Short Answer
When you apply:
.child {
flex: 1;
}
JavaScriptto every child element inside a flex container, all children:
β Take equal width (or height)
β Automatically stretch to fill available space
β Ignore their intrinsic size (unless constrained)
β Create a responsive, fluid layout
In other words, every child becomes a flexible column or row that shares space evenly.
π Understanding flex: 1 in Detail
The flex property is shorthand for:
flex: <flex-grow> <flex-shrink> <flex-basis>;
JavaScriptSo when you write:
flex: 1;
JavaScriptIt expands to:
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;
JavaScriptLetβs break that down:
| Property | Meaning |
|---|---|
flex-grow: 1 | The item can grow and share available space |
flex-shrink: 1 | The item can shrink when space is tight |
flex-basis: 0% | Start from zero size and distribute space equally |
This combination is what makes flex: 1 so powerful for layouts.
π¦ Basic Example: Equal Width Columns
HTML
<div class="container">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
</div>
JavaScriptCSS
.container {
display: flex;
border: 2px solid #333;
}
.box {
flex: 1;
padding: 20px;
border: 1px solid #999;
text-align: center;
}
JavaScriptβ Result
All three boxes:
β Have exactly equal widths
β Fill the entire container
β Resize automatically when the screen changes
Even if the text lengths differ, each box occupies one-third of the container width.
π§ Why Does flex: 1 Make Elements Equal Size?
Because of:
flex-basis: 0%;
JavaScriptThis forces all items to start at zero width, and then they divide the available space equally using flex-grow: 1.
So if there are:
| Items | Space per Item |
|---|---|
| 2 | 50% each |
| 3 | 33.33% each |
| 4 | 25% each |
π Example With Unequal Content Lengths
HTML
<div class="container">
<div class="box">Short</div>
<div class="box">Much Longer Content Here</div>
<div class="box">Mid</div>
</div>
JavaScriptCSS
.container {
display: flex;
}
.box {
flex: 1;
padding: 16px;
border: 1px solid black;
}
JavaScriptπ Output Behavior
Even though the second box contains much more text, all three boxes remain the same width.
This is different from:
flex: auto;
JavaScriptwhich allows content size to influence width.
π What Happens on Small Screens?
Because flex-shrink: 1 is enabled by default in flex: 1, children:
β Shrink proportionally
β Avoid overflow when possible
β Remain equal-sized
Example:
.container {
display: flex;
width: 300px;
}
JavaScriptEven if three children exist, they compress equally to fit into the smaller space.
π Vertical Layout (Column Direction)
If you change the container direction:
.container {
display: flex;
flex-direction: column;
}
JavaScriptThen:
.child {
flex: 1;
}
JavaScriptEach child takes equal height, filling the container vertically.
π§ͺ Example: Equal Height Panels
HTML
<div class="column-container">
<div class="panel">Top</div>
<div class="panel">Middle</div>
<div class="panel">Bottom</div>
</div>
JavaScriptCSS
.column-container {
display: flex;
flex-direction: column;
height: 300px;
border: 2px solid #333;
}
.panel {
flex: 1;
border: 1px solid #999;
display: flex;
align-items: center;
justify-content: center;
}
JavaScriptβ Result
Each panel gets exactly 100px height, regardless of content.
π Comparison: flex: 1 vs Other Flex Values
| Value | Meaning |
|---|---|
flex: 1 | Equal space distribution |
flex: auto | Size based on content, then grow |
flex: none | No grow, no shrink |
flex: 2 | Grows twice as much as flex: 1 |
flex: 0 0 auto | Fixed width |
β Example: Mixed Flex Values
.container {
display: flex;
}
.box1 {
flex: 1;
}
.box2 {
flex: 2;
}
.box3 {
flex: 1;
}
JavaScriptResult
Total grow = 1 + 2 + 1 = 4
So widths:
| Box | Width |
|---|---|
| box1 | 25% |
| box2 | 50% |
| box3 | 25% |
π« What Happens to Intrinsic Widths?
When using flex: 1, intrinsic widths like:
width: 200px;
JavaScriptAre overridden unless:
flex-basis: auto;
JavaScriptor:
flex: 1 1 auto;
JavaScriptExample:
.box {
flex: 1;
width: 300px;
}
JavaScriptThe width is ignored because flex-basis: 0% takes priority.
π Example: Preserving Content Width Instead
If you want equal growth but respect base sizes:
.box {
flex: 1 1 auto;
}
JavaScriptNow each box starts at its content width and then grows proportionally.
π± Responsive Layout Example (Navbar)
HTML
<nav class="navbar">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</nav>
JavaScriptCSS
.navbar {
display: flex;
background: #222;
}
.navbar a {
flex: 1;
color: white;
padding: 14px;
text-align: center;
text-decoration: none;
}
JavaScriptβ Result
Each navigation link:
β Has equal width
β Fills the navbar
β Is responsive
β No media queries required
π§± Example: Dashboard Layout
HTML
<div class="dashboard">
<aside class="sidebar">Sidebar</aside>
<main class="content">Main Content</main>
<aside class="ads">Ads</aside>
</div>
JavaScriptCSS
.dashboard {
display: flex;
height: 100vh;
}
.sidebar,
.content,
.ads {
flex: 1;
padding: 20px;
border: 1px solid #ccc;
}
JavaScriptAll three columns take equal width.
π― Why Developers Love flex: 1
Because it:
β Eliminates manual width calculations
β Automatically adapts to screen size
β Creates clean, fluid grids
β Simplifies equal-height columns
β Works in both rows and columns
β Common Pitfalls
β 1. Unexpected Overflow
If children contain long unbroken text:
.child {
flex: 1;
}
JavaScriptThey may overflow.
Fix:
.child {
min-width: 0;
overflow-wrap: break-word;
}
JavaScriptβ 2. Wanting Fixed Size But Using flex: 1
Wrong:
.sidebar {
width: 300px;
flex: 1;
}
JavaScriptRight:
.sidebar {
flex: 0 0 300px;
}
JavaScriptβ 3. Forgetting Parent Must Be display: flex
Without:
.container {
display: flex;
}
JavaScriptflex: 1 does nothing.
π Real-World Use Case: Equal Cards Layout
HTML
<div class="cards">
<div class="card">Product 1</div>
<div class="card">Product 2</div>
<div class="card">Product 3</div>
</div>
JavaScriptCSS
.cards {
display: flex;
gap: 16px;
}
.card {
flex: 1;
padding: 20px;
background: #f5f5f5;
border-radius: 8px;
}
JavaScriptResult
All cards:
β Same width
β Same height (if align-stretch default applies)
β Responsive across screen sizes
π Deep Dive: How Browser Calculates Sizes with flex: 1
Step 1 β Set Base Size
Each itemβs base size is:
flex-basis: 0%;
JavaScriptSo all start at zero width.
Step 2 β Calculate Free Space
Browser computes:
Free Space = Container Width β Total Base Sizes
JavaScriptSince base sizes are zero:
Free Space = Container Width
JavaScriptStep 3 β Distribute Free Space
Each item gets:
Free Space Γ (flex-grow / total flex-grow)
JavaScriptSince all have flex-grow: 1:
Each = Free Space Γ· Number of Items
JavaScriptStep 4 β Apply Shrink if Needed
If content is too large:
flex-shrink: 1;
JavaScriptcauses proportional shrinking.
π flex: 1 vs width: 33.33%
| Feature | flex: 1 | width: 33.33% |
|---|---|---|
| Responsive to number of items | β | β |
| Supports wrapping | β | Limited |
| Works in column layouts | β | β |
| Handles overflow better | β | β |
| Cleaner CSS | β | β |
π Example With Wrapping
.container {
display: flex;
flex-wrap: wrap;
}
.child {
flex: 1;
min-width: 200px;
}
JavaScriptNow items:
β Stay equal width
β Wrap to next row when screen shrinks
π± Mobile-Friendly Grid Example
HTML
<div class="grid">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
</div>
JavaScriptCSS
.grid {
display: flex;
flex-wrap: wrap;
}
.cell {
flex: 1;
min-width: 150px;
padding: 20px;
border: 1px solid #333;
text-align: center;
}
JavaScriptResult:
β 4 columns on wide screens
β 2 columns on tablets
β 1 column on phones
All with the same flex: 1 rule.
π¨ Example: Equal Buttons in Footer
<footer class="footer">
<button>Cancel</button>
<button>Save Draft</button>
<button>Publish</button>
</footer>
JavaScript.footer {
display: flex;
}
.footer button {
flex: 1;
padding: 12px;
}
JavaScriptButtons become perfectly even in width β great for mobile UI.
π§ Key Takeaways
When you set:
.child {
flex: 1;
}
JavaScripton all children inside a flex container:
β
Each child gets equal space
β
Layout becomes fluid and responsive
β
Content size is ignored in favor of fairness
β
Columns or rows automatically resize
β
Parent space is fully utilized
π Final Summary
Setting flex: 1 on all child elements inside a flex container is one of the most powerful and commonly used Flexbox techniques. It causes every child to grow and shrink equally, starting from zero base size, and dividing all available space evenly among themselves.


