Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the height of a body element

Tags:

jquery

css

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>
like image 793
Shiva Srikanth Thummidi Avatar asked Apr 30 '09 11:04

Shiva Srikanth Thummidi


People also ask

How do you find the height of an element?

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.

How do I get the height of an element in CSS?

outerHeight() It returns the current height of the element including the padding, border and margin optionally.

How can I make my body 100% height?

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.

How do I get the height of an HTML page?

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.


1 Answers

Simply use

$(document).height() // - $('body').offset().top

and / or

$(window).height()

instead of $('body').height();

like image 194
OderWat Avatar answered Oct 11 '22 15:10

OderWat