Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
The flex-grow property seems not work properly

I have a parent div and two children div under the parent. From my understanding, when I set the first child's flex-grow to zero, the width of this element is never change, but when I add some text to the second child, the width of first child decreased. But why?

document.querySelector('#button').addEventListener('click', function() {
  document.querySelector('.child2').appendChild(document.createTextNode('This is text. '));
})
.parent {
  width: 400px;
  height: 400px;
  background-color: #ccc;
  display: flex;
}

.child1 {
  width: 100px;
  flex-grow: 0;
  background: red;
}
<button id="button">Add text</button>

<div class="parent">
  <div class="child1">fdsafa</div>
  <div class="child2"></div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are right. flex-grow stops the width from changing if it is expanding. But if it is getting smaller it does not do anything. Thats what flex-shrink is for. Try flex-shrink on the child.

document.querySelector('#button').addEventListener('click', function() {
  document.querySelector('.child2').appendChild(document.createTextNode('This is text. '));
})
.parent {
  width: 400px;
  height: 400px;
  background-color: #ccc;
  display: flex;
}

.child1 {
  width: 100px;
  flex-shrink: 0;
  background: red;
}
<button id="button">Add text</button>

<div class="parent">
  <div class="child1">fdsafa</div>
  <div class="child2"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!