views:

3877

answers:

6

How do you get the rendered height of an element?

Lets say you have a <div> element with some content inside. This content inside is going to stretch the height of the <div>. How do you get the "rendered" height when you haven't explicitly set the height. Obviously, I tried:

var h = document.getElementById('someDiv').style.height;

Is there a trick for doing this? I am using jQuery if that helps.

FINAL THOUGHTS: In the end I went with jQuery height()
Please take the time to read all answers and comments

+10  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.

strager
+1 for adding value to the thread.
tyndall
+11  A: 

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!

Russ Cam
Someone downvoted my answer - why? The OP is using jQuery, so using the jQuery height command just makes the most sense
Russ Cam
+1 from me. I think I gave up on height() the other day because I was using height not height(). So I learned something.
tyndall
I did. I didn't see the part where the OP was using jQuery. My apologies. I can't remove the downvote until you edit your post, but I'll gladly it remove it if you edit it so it'll let me.
Paolo Bergantino
removed and +1'ed for your troubles. :)
Paolo Bergantino
One word - awesome! Thanks for everyone's participation. Lots of good info on this thread. This will allow me to center pages using CSS but use jQuery to make the overall height of the "container" div correct without venturing into CSS hackville. Thanks again.
tyndall
even if you're NOT using jQuery or cant or don't want to I strongly suggest anyone implementing this by using just offsetHeight goes ahead and downloads the jQuery source and searches for "height" to find the code they use. lots of crazy stuff going on in there !
Simon_Weaver
Absolutely agree with that Simon. It's always insightful to investigate how Javascript libraries accomplish a solution to a problem
Russ Cam
A: 

Have you set the height in the css specifically? If you haven't you need to use offsetHeight; rather than height

var h = document.getElementById('someDiv').style.offsetHeight;
Steerpike
offsetHeight is a property of the element itself, not of its style.
Shog9
A: 

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 lines last time I checked).

faB
interesting. i've never checked prototype. any idea why jQuery doesn't do that behind the scenes?
Simon_Weaver
A: 

With MooTools:

$('someDiv').getSize().y

ifwdev
+1  A: 

Definitely use

$('#someDiv').height()   // to read it

or

$('#someDiv').height(newHeight)  // to set it

I'm posting this as an additional answer because theres a couple important things I just learnt.

I almost fell into the trap just now of using offsetHeight. This is what happened :

  • I used the good old trick of using a debugger to 'watch' what properties my element has
  • I saw which one has a value around the value I was expecting
  • It was offsetHeight - so I used that.
  • Then i realized it didnt work with a hidden DIV
  • I tried hiding after calculating maxHeight but that looked clumsy - got in a mess.
  • I did a search - discovered jQuery.height() - and used it
  • found out height() works even on hidden elements
  • just for fun I checked the jQuery implementation of height/width

Here's just a portion of it :

Math.max(
Math.max(document.body["scroll" + name], document.documentElement["scroll" + name]),
Math.max(document.body["offset" + name], document.documentElement["offset" + name])
)

Yup it looks at BOTH scroll and offset. If that fails it looks even further, taking into account browser and css compatibility issues. In other words STUFF I DONT CARE ABOUT - or want to.

But I dont have to. Thanks jQuery!

Moral of the story : if jQuery has a method for something its probably for a good reason, likely related to compatibilty.

If you haven't read through the jQuery list of methods recently I suggest you take a look.

Simon_Weaver
Saw your other comment at the question level. I'm sold on jQuery. Have been for a little while. But this sealed the deal. I'm in love with the fact this simple thing is going to solve 100s of layout issues for me (mostly on Intranet sites where I know JavaScript is running).
tyndall