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

156
Views
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 answers
Answer question

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 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!