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

241
Views
how to get exact content height inside the div using javascript?

I am trying to get exact content height inside the div .I am able to get exact value when there is no image inside the container . but if there is an image inside it give wrong result why ?

I am using clientHeight and offsetHeight both gives wrong output.

here is my code https://codesandbox.io/s/cold-browser-dxj51?file=/index.html:907-919

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      .container {
        padding: 20px;
        width: 200px;
        background-color: #eeeeee;
      }
      .logo img {
        width: 100%;
      }
      .logo {
        padding-bottom: 30px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="logo">
        <img src="1.png" />
      </div>
      <div class="abc">
        This is a static template, there is no bundler or bundling involved!
      </div>
    </div>
    <script>
      console.log(document.querySelector(".container").clientHeight);
      console.log(document.querySelector(".container").offsetHeight);
    </script>
  </body>
</html>

Expected output : 154px+ 20px+20px =(194px)

myoutput : 142px

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that your javascript is not waiting the image's full load before checking its height. You can use the event listener "load" to succesfully fire the script you want, once dependent resources have finished loading:

window.addEventListener("load", event => {
    console.log(document.querySelector(".container").clientHeight);
    console.log(document.querySelector(".container").offsetHeight);
});

The code above will give you your desired output.

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!