18

I want to get inner html height by JavaScript. Suppose I have

<div>Content</div>

either if the div font size has increased or many divs nested inside the parent, how do I get the height of parent div?

edit: even if the nested div has border and padding too.

Abhinash Majhi
  • 499
  • 1
  • 4
  • 16

7 Answers7

23

The accepted answer is incorrect in the case that the parent element has a height of 0, and you want the height of the content within it (which is what I believe the OP was asking for). In that case, scrollHeight is the property you want.

const divInnerContentHeight = document.querySelector('div').scrollHeight;

Using clientHeight or offsetHeight would return 0;

Matt
  • 988
  • 9
  • 14
13

If it must be in vanilla JS...

var height = document.getElementById('content').clientHeight;

That will not include any borders or scrollbars in the element, just padding. If you want to include borders and scrollbars you may use offsetHeight instead.

Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46
  • 3
    `clientHeight` include just the padding, and `offsetHeight` includes padding, borders, and scrollbars – Rainbow May 09 '18 at 15:09
2

Use clientHeight and getElementsByTagName div to get spcific div height

document.getElementsByTagName('div')[0].clientHeight

http://codepen.io/nagasai/pen/XKoxZw

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
1

Something like this?

<div id="MyDiv">Content</div>

console.log(document.getElementById("MyDiv").offsetHeight);
mituw16
  • 5,126
  • 3
  • 23
  • 48
1

Include padding and border in your height calculation:

document.getElementById('myDiv').offsetHeight;
Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46
1

you can you content(ele) scrollheight to height of content.

ele.scrollHeight
Ali Raza
  • 1,026
  • 13
  • 20
0
 //We can do his using JQuery

    //Returns the element's height
    $('#content').height();
   // Returns the height with padding
    $('#content').innerHeight();
   // Returns the height with padding and border
    $('#content').outerHeight();
   // Returns the height with padding,border and margin
    $('#content').outerHeight(true);
Sheo Dayal Singh
  • 1,591
  • 19
  • 11