Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda