How do I get the height of a div's full content with jQuery? How do I get the height of a div's full content with jQuery? jquery jquery

How do I get the height of a div's full content with jQuery?


scrollHeight is a property of a DOM object, not a function:

Height of the scroll view of an element; it includes the element padding but not its margin.

Given this:

<div id="x" style="height: 100px; overflow: hidden;">    <div style="height: 200px;">        pancakes    </div></div>

This yields 200:

$('#x')[0].scrollHeight

For example: http://jsfiddle.net/ambiguous/u69kQ/2/ (run with the JavaScript console open).


If you can possibly help it, DO NOT USE .scrollHeight.

.scrollHeight does not yield the same kind of results in different browsers in certain circumstances (most prominently while scrolling).

For example:

<div id='outer' style='width:100px; height:350px; overflow-y:hidden;'><div style='width:100px; height:150px;'></div><div style='width:100px; height:150px;'></div><div style='width:100px; height:150px;'></div><div style='width:100px; height:150px;'></div><div style='width:100px; height:150px;'></div><div style='width:100px; height:150px;'></div></div>

If you do

console.log($('#outer').scrollHeight);

you'll get 900px in Chrome, FireFox and Opera. That's great.

But, if you attach a wheelevent / wheel event to #outer, when you scroll it, Chrome will give you a constant value of 900px (correct) but FireFox and Opera will change their values as you scroll down (incorrect).

A very simple way to do this is like so (a bit of a cheat, really). (This example works while dynamically adding content to #outer as well):

$('#outer').css("height", "auto");var outerContentsHeight = $('#outer').height();$('#outer').css("height", "350px");console.log(outerContentsHeight); //Should get 900px in these 3 browsers

The reason I pick these three browsers is because all three can disagree on the value of .scrollHeight in certain circumstances. I ran into this issue making my own scrollpanes. Hope this helps someone.


We can also use -

$('#x').prop('scrollHeight') <!-- Height -->$('#x').prop('scrollWidth')  <!-- Width -->