Here the total height of all <div>
's are 900 pixels, but the jQuery function returns the height of the body as 577 pixels. (If I remove the body CSS, it's working).
Is there a solution for this problem?
$j(function() {
alert($j("body").height());
})
html, body {
height:100%;
}
<div style="height:200px">header</div>
<div style="height:500px">content</div>
<div style="height:200px">footer</div>
In JavaScript, you can use the clientHeight property, which returns an element's height, including its vertical padding. Basically, it returns the actual space used by the displayed content. For example, the following code returns 120, which is equal to the original height plus the vertical padding.
outerHeight() It returns the current height of the element including the padding, border and margin optionally.
Try setting the height of the html element to 100% as well. Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well. However the content of body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.
JavaScript – Get Height of an HTML Element in PixelsclientHeight property returns the height of the HTML Element, in pixels, computed by adding CSS height and CSS padding (top, bottom) of this HTML Element. Border and margin are not considered for clientHeight computation.
Simply use
$(document).height() // - $('body').offset().top
and / or
$(window).height()
instead of $('body').height();
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