6
It is possible to know the z-index
of an empty element using javascript?
For example in this code:
CSS
#exemplo1 {
z-index:4;
}
#exemplo2 {
z-index:4;
position: relative;
}
HTML
<div id="exemplo1"></div>
<div id="exemplo2"></div>
It is possible to obtain the z-index
of exemplo1
, in this case 4
, although the position
is not closed?
What I have tested:
var e1 = document.getElementById('exemplo1');
var e2 = document.getElementById('exemplo2');
console.log(e1.style.zIndex); // nada
console.log(e2.style.zIndex); // nada
console.log(window.getComputedStyle(e1).getPropertyValue("z-index")); // auto no Chrome, 4 no IE11 e FF
console.log(window.getComputedStyle(e2).getPropertyValue("z-index")); // 4
If the computed style is returning
auto
, is because the rendering engine will not consider thez-index
for this element (will not change its z-index, nor create a new stacking context). In this case, what is the usefulness of getting the value as defined in the CSS?– mgibsonbr
@mgibsonbr, the curious thing is that the IE11 and the FF return
4
but Chrome returnsauto
. What I’m looking for is a uniform/consistent way to do this.– Sergio
This is a bug that affects all Webkit. It is documented but still in unconfirmed status: https://bugs.webkit.org/show_bug.cgi?id=122416
– user3813