outerHeight(true) gives wrong value
Asked Answered
N

11

23

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

Necessaries answered 22/4, 2012 at 14:41 Comment(1)
Long shot, but I think you might need .outerHeight({margin: true});Skipp
V
36

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! :)

Vervet answered 14/1, 2014 at 21:45 Comment(2)
Thank you so much for this - I was starting to pull my hair out and couldn't figure out why it was giving me an incorrect height. FTR, Firefox was doing it too.Buccaneer
this is a correct answer but you have to change your function to $(window).on('load', function() { ... }); as .load no longer existArrestment
H
10

I had the same problem. I get the correct height in Chrome by using setTimeout to wait a bit:

    setTimeout ( function () {
        var ht = $('.my-class').outerHeight( true );
    }, 1);

I hope that helps!

Hughie answered 18/2, 2013 at 6:57 Comment(0)
T
6

I've run into same problem.

Firefox outerheight via console.log: 430 Chrome outerheight via console.log: 477

Real outerheight (firebug layout): 820

After opening Firebug or removing statusbar it sets correct value (820) in console.log (and hence the code works as it should).

The issue was due to images. Even though you are running it on document ready, not all images might be loaded yet, and therefore the calculations are wrong.

I am using desandro's plugin and it has fixed my issue. https://github.com/desandro/imagesloaded

Tutu answered 3/2, 2013 at 11:15 Comment(2)
This solved a similar problem perfectly for me. Thanks Zoran.Alita
just ran into this problem, a container with an image in it caused the problem. I defined a height for the container in CSS, that didn't help. But what solved is #container img { max-width: 100%; }. That means, the image overflowing the container was causing the problem.Oology
S
4

I think you might need

.outerHeight({margin: true});

As per here:

The margin can be included by passing an options map with margin set to true.

Skipp answered 22/4, 2012 at 14:45 Comment(1)
That gives me the same value as outerHeight(true)Necessaries
G
4

Inner hidden, fixed and absolute elements are not properly accounted for using the outerHeight.

Try the scrollHeight property:

$('element').prop('scrollHeight')
Gigolo answered 31/10, 2017 at 12:59 Comment(1)
Seems to me like this is a bug that's still happening 5 years later. If 'scrollHeight' can provide us with the correct height without having to wait for $(window).load(), then it seems to me that .outerHeight(true) and .innerHeight() should also be able to do the same.Lout
R
3

the one that worked exactly as it should for me was a combine of both @Matt Reilly & @Lasha answers

$(window).load(function() {
    setTimeout(function() {
        // all the logic in here
    }, 1);
});

i used to put the code in in $(document).ready(function(){...}); but it gets wanky sometimes so the above works perfectly.

Ratepayer answered 27/5, 2016 at 22:41 Comment(0)
M
0

If the window is loaded and the document ready and you have accounted for floats and you still get this issue then consider looking for an element with the same id. Be certain you are getting the right element with your jQuery selector. I forgot I had another element in the dom with the exact same id. Consider using jQuery to change the background color to verify.

Mcbryde answered 7/7, 2015 at 16:32 Comment(0)
B
0

Tried every single one of these solutions. Finally opened up inspector and realized my p still had a margin-top. As always thanks Normalize...

Boffin answered 24/2, 2016 at 5:50 Comment(0)
S
0

According to the docs, outerHeight isn't guaranteed to be accurate when:

  • the page is zoomed
  • the element or its parent is hidden
Subrogate answered 21/4, 2018 at 21:16 Comment(0)
C
0

A little hack. Place the code inside jquery's on load, scroll and resize functions like this:

$(window).on('load scroll resize', function(){
    //outerHeight code
});
Counterreply answered 13/9, 2018 at 12:8 Comment(1)
It works, but there's a caveat here.. when using this with a fixed positioned element, the layout shifts, which is not what you want.Jerid
A
0

If you select by class, you may select multiple elements at once, and if the first element has a wrong height, it will be the height you'll get with outerHeight() (that was my issue personally)

Ator answered 31/3, 2021 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.