Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

outerHeight(true) gives wrong value

Tags:

I want the height of a container element, including margins and paddings.

When I hover over the element in Chrome development tool, I get the value that I'm looking for but when I use jQuery $('element').outerHeight(true); I get a much smaller value.

The element contains a jQuery carousel (in a container with position:relative and items position: absolute), could that have something to do with it?

Thanks in advance

like image 448
Viktor Avatar asked Apr 22 '12 14:04

Viktor


People also ask

Does outerHeight include margin?

According to JQuery docs outerHeight(true) function returns the total height of the element including padding border and margins.

What is outer height?

Definition and Usage. The outerHeight property returns the outer height of the browser window, including all interface elements (like toolbars/scrollbars).

How do you find the height of an element with padding?

Using jQuery, you can get element height including padding with $(ele). outerHeight() , get element height including padding, border and margin by $(ele). outerHeight(true) .

Which jQuery method is used to obtain the outer height of an element the height includes border also?

jQuery | outerHeight() Method The outerHeight() method in jQuery is used to find the outer height of the specified element. Outer height of an element includes padding and border.


1 Answers

I have experience this issue several times, and the best solution, without the use of any plugins or unruly setTimeout functions, is to use hook into the browser's onLoad event. With jQuery, it's done like so:

$(window).load(function(){ ...outerHeight logic goes here... });

This could be totally separate from your standard $(function(){ ...code... }); so all of your other properly working code doesn't have to wait until every single element on the page has loaded.

Why this happens in the first place:
Chrome has a different rendering algorithm than Firefox, which causes it to trigger the onLoad event before all the elements on the page are completely drawn/displayed and available for jQuery to select and retrieve heights. setTimeout() will work most of the time, but you don't want to develop a dependency on something so blind by nature – who knows, in the future this "quirk" in Chrome could be fixed! :)

like image 131
Lasha Avatar answered Sep 21 '22 04:09

Lasha