I have been met with a bit of surprise today, I have the bellow HTML markup.
In my CSS I set the container to position: fixed; z-index: 3
, and then I provided a z-index of 1 to div#1
and div#3
giving div#2
z-index: 2
. My expectation is that when I move div#3
up that it will go behind div#2
but to my greatest surprise it always stays above it no matter the value of its z-index or that of div#2
, I am confused why this is, maybe I don't just understand the stacking context as much as I think I do. Any help? Below I have the css.
* {
box-sizing: border-box;
}
body, html, .container {
height: 100%;
width: 100%;
}
.container {
position: fixed;
z-index: 300;
}
#div1, #div2, #div3 {
opacity: 0.7;
padding: 10px;
}
#div1 {
border: 1px dashed #996;
background-color: #ffc;
height: 33.333%;
z-index: 1;
}
#div2 {
border: 1px dashed #900;
background-color: #fdd;
height: 33.333%;
z-index: 2;
}
#div3 {
border: 1px dashed #696;
background-color: #cfc;
height: 33.333%;
z-index: 1;
transform: translateY(-40px)
}
<div class='container'>
<div id="div1">DIV#1 </div>
<div id="div2">DIV#2</div>
<div id="div3">DIV#3</div>
</div>