Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda