2

var test = document.getElementById('test');
console.log(test.offsetHeight);
<div style="height: 30px;" id="test">
  <p>ffffffffff</p>
  <p>gggggggggg</p>
  <p>aaaaaaaaaa</p>
  <p>dddddddddd</p>
</div>
As you can see, the content in test div is overflow and more than 30px: so how to get accurate height?
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
user3345456
  • 41
  • 1
  • 5
  • 2
    Possible duplicate of [Get div height with plain JavaScript](http://stackoverflow.com/questions/15615552/get-div-height-with-plain-javascript) – KarelG May 09 '17 at 06:58

5 Answers5

2

var test = document.getElementById('test');
alert(test.scrollHeight);
<div style="height: 30px;" id="test">
  <p>ffffffffff</p>
  <p>gggggggggg</p>
  <p>aaaaaaaaaa</p>
  <p>dddddddddd</p>
</div>

try scrollHeight

Durga
  • 15,263
  • 2
  • 28
  • 52
1

You can use either .clientHeight or .offsetHeight;.

What is the difference?

.clientHeight includes padding.

.offsetHeight includes padding, scrollBar and borders.

var clientH = document.getElementById('test').clientHeight;
var offsetH = document.getElementById('test').offsetHeight;


console.log(clientH);
console.log(offsetH);
<div style="height: 30px;" id="test">
  <p>ffffffffff</p>
  <p>gggggggggg</p>
  <p>aaaaaaaaaa</p>
  <p>dddddddddd</p>
</div>
loelsonk
  • 3,570
  • 2
  • 16
  • 23
0

Try this:

window.getComputedStyle(document.getElementById('test')).height
0

try:

var test = document.getElementById('test');
alert(test.clientHeight);
0

Try this.

var offsetHeight = document.getElementById('test').offsetHeight;
Vivek ab
  • 660
  • 6
  • 15