If you don't pay attention to stacking context, you can paint yourself into a z-index corner.
Red square is given
position:relative
in order to position the yellow square absolutely. But that raises it in the stacking order over non-positioned elements (static).
So we add
z-index:-1
to tuck it back behind. Only now it's impossible to raise the yellow square above the green one. It's trapped within the stacking context of its red parent element. (Pseudo-elements are content of their parent element — sometimes that's easy to lose track of.)
Stacking order can be unexpected if you're unaware of its peculiarities: Try declaring opacity on the .green square — anything below 1 — it renders it above non-positioned elements in the stacking order! (If you want to know more, Philip Walton wrote a helpful article .)
So how do we get yellow to overlap green? One solution is to remove the z-index from .red and add position:relative to both .green and .blue.