Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is $(document).height() different from $(document.body).height()?

I have been googling for a while, but still could not find why
is $(document).height() different from $(document.body).height()?

What's more, could anyone tell me the corresponding relation between the height methods
in jQuery and the raw JavaScript ones?

For example, $(document).height() is equivalent to document.documentElement.scrollHeight.

like image 225
LeoYuan 袁力皓 Avatar asked Oct 06 '22 06:10

LeoYuan 袁力皓


1 Answers

Because the <body> element in your HTML is just another block element (much like a <div>), and as any block element, it can have dimensional styling (margin, padding, width, height, etc).

Considering that, the "size" of the <body> element does not need to correspond directly to the size of the document object (which corresponds to your whole HTML document as rendered, as a whole). One good reason is that the <body> element is also inside the <html> element, which is also a block element, and can have its own respective dimensionals.

If your <html> element, for example, had padding:10px, then your <body> element will definitely differ in effective size to the whole document entirely.

Now, don't get me wrong: document and document.body can have the exact same size (try checking StackOverflow's for example), but you have to understand that <body> is just another HTML block you can manipulate via CSS.

like image 68
Richard Neil Ilagan Avatar answered Oct 10 '22 19:10

Richard Neil Ilagan