getComputedStyle reporting different heights between Chrome/Safari/Firefox and IE11
Asked Answered
G

3

12

This has been driving me a little batty all day and I haven't been able to find where anyone else has documented this discrepancy.

window.getComputedStyle(el).height

See http://jsfiddle.net/ZwF9H/6/ for the demo.

What I am expecting is that window.getComputedStyle() should return the same computed height value between all browsers. Internet Explorer 11 is doing something different. (Actually, IE 9 and 10 are as well, but IE 11 was the first one I could get the dev tools to work in.)

For all other browsers, the computed height is the height set in the css whether it be in the stylesheet or inline on the textarea element.

IE11 is ignoring the box-sizing: border-box declaration and subtracts the padding and margin, which I think is incorrect.

Is this a bug, am I doing something wrong, is it a know fact that IE11 returns computed values differently?

Gap answered 31/10, 2013 at 22:47 Comment(1)
Did you get some more info about this problem and how to get around it?Rachael
B
7

I had the same problem with IE11, where it was ignoring the box-sizing: border-box; and thus it was subtracting padding from the border-box height, giving me smaller height values than Chrome reported.

I found that getBoundingClientRect().height did report the proper height (properly observing box-sizing: border-box;) in IE11 and Chrome. So that has solved the problem for me.

Bathysphere answered 9/8, 2016 at 23:33 Comment(0)
G
1

Element.height specifies the height within the content area, not including the padding or border. There's more information about it here (https://developer.mozilla.org/en-US/docs/Web/CSS/height).

I'd suggest using jQuery's $el.outerHeight() if you have that option available.

Gardal answered 12/7, 2014 at 20:38 Comment(0)
C
0

Use polyfill

There are two polyfills which fixes this bug:

  1. jonathantneal / Polyfills-for-IE8

https://github.com/jonathantneal/Polyfills-for-IE8/blob/master/getComputedStyle.js

  1. jonathantneal / polyfill

https://github.com/jonathantneal/polyfill/blob/master/polyfills/getComputedStyle/polyfill.js

It looks like it fixes the problem.

Demo with 1st polyfill

//SO says that I must add code here, but it's too long

Demo with 2nd polyfill

//look at demo
Cuttler answered 6/4, 2016 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.