Why is div.red in front of the green div when I remove the z-index from .wrapperRed?
Since .red no longer has a parent z-index to restrict it.
e.
Before: .red has a z-index of 5 in the parent z-index of 1.
After: .red has a global z-index of 5.
NB In both cases, before and after .wrapperRed always behind .green . But, when it is not bound, .green appears before .green (which is 100% width and height .wrapperRed ).
You can see this more easily if you give the parent and child divs different background colors and make the child div smaller than the parent.
For comparison:
.wrapperRed { height: 200px; width: 200px; position: absolute; background-color: red; z-index: 1; } .yellow { position: absolute; height: 75%; width: 75%; background-color: yellow; z-index: 5; } .green { height: 200px; width: 200px; background-color: green; position: absolute; top: 100px; left: 100px; z-index: 1; }
<div class="wrapperRed"> <div class="yellow"> </div> </div> <div class="green"></div>
from:
.wrapperRed { height: 200px; width: 200px; position: absolute; background-color: red; } .yellow { position: absolute; height: 75%; width: 75%; background-color: yellow; z-index: 5; } .green { height: 200px; width: 200px; background-color: green; position: absolute; top: 100px; left: 100px; z-index: 1; }
<div class="wrapperRed"> <div class="yellow"> </div> </div> <div class="green"></div>
Rounin
source share