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

217
Visualizações
Increase parent div height when more child is added

I have a parent div with multiple child divs. Currently, the child divs are spaced around the parent div in order to fit the parent div equally and it works fine but what I'm trying to achieve is when more child divs are added the parent divs height increases in size but the child divs height stays the same. How can I achieve this? What it currently does when you add more child divs is the parent height stays the same but the child divs height will get shorter in order to fit the parent div but what I want is the other way around. When more child div is added I want the parent div to increase in height kinda like the parent div having a height of auto. How can I achieve this? Any help is appreciated. Thanks in advance.

.items {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  position: absolute;
  top: 6%;
  left: 15%;
  width: 70%;
  height: 60%;
  background-color: gold;
}

.data {
  width: 100%;
  height: 35%;
  background-color: #187bcd;
  border-top: 2px solid #000000;
}
<div class="items">
  <div class="data">

  </div>
  <div class="data">

  </div>
  <div class="data">

  </div>
  <div class="data">

  </div>
</div>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can apply a non-parentally-relative length unit (like em and px) to do that, if that's what you want:

.items {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  position: absolute;
  top: 6%;
  left: 15%;
  width: 70%;
  background-color: gold;
}

.data {
  width: 100%;
  height: 2em;
  background-color: #187bcd;
  border-top: 2px solid #000000;
}
<div class="items">
  <div class="data">

  </div>
  <div class="data">

  </div>
  <div class="data">

  </div>
  <div class="data">

  </div>
    <div class="data">

  </div>
  <div class="data">

  </div>
  <div class="data">

  </div>
  <div class="data">

  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Just remove the % height from your .items div, then set a fixed height on your child elems.

<button>Add div</button>
<div class="items">
    <div class="data"></div>
    <div class="data"></div>
</div>

<style>
    .items {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
        position: absolute;
        top: 6%;
        left: 15%;
        width: 70%;
        background-color: gold;
    }

    .data {
        width: 100%;
        height: 75px; /* Fixed height here */
        background-color: #187bcd;
        border-top: 2px solid #000000;
    }
</style>

<script>
    const button = document.querySelector('button');
    const div = document.querySelector('div.items');

    button.addEventListener('click', () => {
        const newDiv = document.createElement('div');
        newDiv.className = 'data';
        div.appendChild(newDiv);
    });
</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

min-height: (less than or equals the height of the child element)

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