The documentation reads:
Each stacking context is completely independent of its siblings: only descendant elements are taken into account when processing stacks.
This does not mean that the context of the element itself is not dependent on its brothers and sisters, but it does mean that they are children.
Note that stacking contexts live in "positioned elements." Positioned items are items with a position of "relative", "absolute" or "fixed" and z-index. Only positioned elements are installed. In this case, there are only two positioned elements. They themselves add contexts, as does their closest positional ancestor. However, these elements do not seem to have a (common) positional ancestor. Based on this, we can say the following:
These elements relate to the same positioned ancestor (stacking context): viewport. Therefore, z-indices give the expected results (upper value of the z index). An equal z-index appears in the reading order (last on top).
Creating another positioned ancestor violates this behavior, as Leonid shows. This is because it creates a new independent stack context.
Adding opacity to the second div also violates this behavior, since the elements that need to be redrawn (oddly enough) are treated as positioned elements (new stacking contexts):
<div class="overlay" style="display: block;"></div> <div style="opacity: 0.99;">...</div>
See this in action: http://codepen.io/anon/pen/ENrOGx
Still not getting it?
Read the Philip Waltons article: https://philipwalton.com/articles/what-no-one-told-you-about-z-index/ . He explains it very well.
PS. Who doesn't like the good STACKING question about STACK overflow?
source share