How do you get the rendered height of an element?
Let\'s say you have a
Try one of:
var h = document.getElementById('someDiv').clientHeight;
var h = document.getElementById('someDiv').offsetHeight;
var h = document.getElementById('someDiv').scrollHeight;
clientHeight
includes the height and vertical padding.
offsetHeight
includes the height, vertical padding, and vertical borders.
scrollHeight
includes the height of the contained document (would be greater than just height in case of scrolling), vertical padding, and vertical borders.
If you are using jQuery already, your best bet is .outerHeight()
or .height()
, as has been stated.
Without jQuery, you can check the box-sizing in use and add up various paddings + borders + clientHeight, or you can use getComputedStyle:
var h = getComputedStyle(document.getElementById('someDiv')).height;
h
will now be a string like a "53.825px".
And I can't find the reference, but I think I heard getComputedStyle()
can be expensive, so it's probably not something you want to call on each window.onscroll
event (but then, neither is jQuery's height()
).
offsetHeight
, usually.
If you need to calculate something but not show it, set the element to visibility:hidden
and position:absolute
, add it to the DOM tree, get the offsetHeight
, and remove it. (That's what the prototype library does behind the scenes last time I checked).
With MooTools:
$('someDiv').getSize().y
You can use .outerHeight()
for this purpose.
It will give you full rendered height of the element. Also, you don't need to set any css-height
of the element. For precaution you can keep its height auto so it can be rendered as per content's height.
//if you need height of div excluding margin/padding/border
$('#someDiv').height();
//if you need height of div with padding but without border + margin
$('#someDiv').innerHeight();
// if you need height of div including padding and border
$('#someDiv').outerHeight();
//and at last for including border + margin + padding, can use
$('#someDiv').outerHeight(true);
For a clear view of these function you can go for jQuery's site or a detailed post here.
it will clear the difference between .height()
/ innerHeight()
/ outerHeight()
It should just be
$('#someDiv').height();
with jQuery. This retrieves the height of the first item in the wrapped set as a number.
Trying to use
.style.height
only works if you have set the property in the first place. Not very useful!