I know that z-index only works with positioned elements, but I am wondering why this is the case. Is there a good reason for this behaviour or is it just one of those semi-arbitrary specification decisions?
I came across this issue when working with code like this:
HTML
<div>
<img class="not-positioned" src="http://placekitten.com/g/200/300">
<img class="positioned" src ="http://placekitten.com/g/400/500">
</div>
CSS
img {
border: 2px solid black;
}
.positioned {
position: relative;
left: -60px;
z-index: 1;
}
.not-positioned {
z-index: 99;
}
You'll notice that this works according to specification (the .not-positioned
image is behind the .positioned
image despite .not-positioned
's higher z-index value), but I am having a hard time understanding the rationale for this behaviour.