Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get inner HTML content height

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.

like image 302
Abhinash Majhi Avatar asked Aug 11 '16 18:08

Abhinash Majhi


People also ask

How do you find the inner content height?

var height = document. getElementById('content'). clientHeight; That will not include any borders or scrollbars in the element, just padding.

How do you find the height of an element in HTML?

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).

What does HTML height 100% mean?

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.


2 Answers

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;

like image 137
Matt Avatar answered Sep 28 '22 23:09

Matt


 //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);
like image 37
Sheo Dayal Singh Avatar answered Sep 28 '22 23:09

Sheo Dayal Singh