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

153
Visualizações
Get heights of loaded elements and add css rule

I have a number of differently sized elements on a page all with the same class of .margin-top-auto

I cannot know the heights of the elements until the page loads.

I need to add {margin-top = the height of each element} to these elements on page load.

Looking for a jQuery/Vanilla snippet that would accomplish this for me.

Many thanks.

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

0

You can get the element's offsetHeight:

const height = div.offsetHeight;

div.style.marginTop = height + 'px'

console.log(height)
#div{
  background-color:black;
  height:100px;
  width:100px;
}
<div id="div"></div>

To target multiple elements, you can do:

document.querySelectorAll('.margin-top-auto').forEach(e => e.style.marginTop = e.offsetHeight + 'px')
.margin-top-auto{
  height:100px;
  width:100px;
  background-color:black;
}

.row{
  display:flex;
}
<div class="row">
<div class="margin-top-auto"></div>
<div class="margin-top-auto"></div>
<div class="margin-top-auto"></div>
</div>

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