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.
var height = document. getElementById('content'). clientHeight; That will not include any borders or scrollbars in the element, just padding.
The HTMLElement. offsetHeight read-only property returns the height of an element, including vertical padding and borders, as an integer. Typically, offsetHeight is a measurement in pixels of the element's CSS height, including any borders, padding, and horizontal scrollbars (if rendered).
For years, the answer was the following: html { height: 100%; } body { min-height: 100%; } This allows the HTML element to reference the parent viewport and have a height value equal to 100% of the viewport value.
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
;
//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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With