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

231
Views
set img flex-grow will override other flex child element

following is my err code, first "text1" is override

       <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <!--mobile friendly-->
        <meta name="viewport" content="width=device-width, user-scalable=yes">
        <style>
            .it {
                display: flex;
                flex-direction: column;
                width: 100px;
                height: 100px
            }
    
            .it *:first-child {
                flex-grow: 1;
                background-color: pink;
            }
    
            .it img {
                object-fit: contain;
            }
        </style>
    </head>
    <body>
    <div class="it">
        <img src="https://i.stack.imgur.com/rtDch.jpg"/>
    <!--    <div>img1</div>-->
        <div>text1</div>
    </div>
    
    <div class="it">
        <img src="https://i.stack.imgur.com/rtDch.jpg"/>
    <!--    <div>img2</div>-->
        <div>text2</div>
    </div>
    </body>
    </html>

enter image description here

but I expect show img + img name text

enter image description here

I know I can set img height to make text show, but I still wanna find way to make fill flexbox rest space

following is my code img.jpg

enter image description here

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

0

To be 100% sure we would need an working example. But most likely the problem is that you give 100px to your container combined with flex-grow and probably an image at least 100x100 if not bigger.

My best guess is that the rendered image takes at least 100px height and the .it div overflows (pushing the text out of reach).

You have some options to deal with this. The easiest would be to give the image fixed height rather than the container (but the best solution depends a lot on what you need)

(or at least give it a maxheight of 100px - 1 line of text)

Can you also change from .it *:first:child to .it > img ? Be aware that .it *:first:child will also trigger for nested children (so <div class="it"><div><span>this will also be catched</span></div></div>)


EDIT: I think something like this is what you are searching for:

.it {
    display: flex;
    flex-direction: column;
    width: 100px;
    height: 100px;
}

.it > div:first-child {
    display: flex;
    height: 0;
    flex: 1 1 0;
    justify-content: center;
    background-color: pink;
}
    
.it img {
    object-fit: contain;
}
<div class="it">
  <div>
    <img src="https://i.stack.imgur.com/rtDch.jpg"/>
  </div>
  <div>More text here</div>
  <div>text1</div>
</div>    
<div class="it">
  <div>
    <img src="https://i.stack.imgur.com/rtDch.jpg"/>
  </div>
  <div>text2</div>
</div>

Explication: In order to to force your image to only fill the remaining space wrap the image in a flex container and give it a height of 0. This way the other elements will definitely fill in the .it container space (since your image container has height 0). After give it flex-grow: 1 so it will grow to fill the remaining empty space (last 2 lines inside .it > div:first-child {...} are, of course, optional)

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!