There have been recent changes in this area.
Previously, getComputedStyle
was returning the Computed Values of an element, now it should return its Resolved Values.
For the bottom
property the rules to get this resolved value are:
A resolved value special case property like top
defined in another specification
If the property applies to a positioned element and the resolved value of the display property is not 'none'
or 'contents'
, and the property is not over-constrained, then the resolved value is the used value. Otherwise the resolved value is the computed value.
It sounds like your browser treats your elements as a positioned element and thus uses the used value (0px
) instead of the computed value (the keyword 'auto' or a computed <length-percentage> value).
I must admit I'm not quite clear as to why all the other browsers don't consider your sticky elements as positioned elements, I would have thought they were also, but they do agree that a relatively positioned element returns the resolve value '0px'
,
var el1 = document.querySelector('#one');
el1.innerText = getComputedStyle(el1).bottom;
var el2 = document.querySelector('#two');
el2.innerText = getComputedStyle(el2).bottom; // '0px' everywhere
#one, #two {
position: relative;
padding: 20px;
color: white;
}
#one {
bottom: 0px;
background: red;
}
#two {
bottom: auto;
background: blue;
}
<div id="one"></div>
<div id="two"></div>
While a non-positioned one returns the computed-value 'auto'
.
var el1 = document.querySelector('#one');
el1.innerText = getComputedStyle(el1).bottom;
var el2 = document.querySelector('#two');
el2.innerText = getComputedStyle(el2).bottom; // 'auto' everywhere
#one, #two {
position: static;
padding: 20px;
color: white;
}
#one {
bottom: 0px;
background: red;
}
#two {
bottom: auto;
background: blue;
}
<div id="one"></div>
<div id="two"></div>
Unfortunately, I don't think there is a way to get consistent values across browsers that did implement these changes, the ones that didn't, and Edge, unless if you can avoid positioning your elements, then you should get 'auto'
everywhere.