How do I put the #stranger div in between the #mother and #child? Right now, the #stranger div covers both the #mother and #child!
<head>
<style>
#mother{
position:absolute;
display:block;
width:300px;
height:300px;
background-color:green;
padding:40px;
z-index:1000;
}
#child{
position:relative;
display:block;
width:180px;
height:180px;
background-color:yellow;
z-index:6000;
}
#stranger{
position:relative;
display:block;
width:300px;
height:600px;
background-color:black;
z-index:1500;
}
</style>
</head>
<body>
<h1>Z-index Test</h1>
<h1>How Do I put #stranger between #mother and #child?</h1>
<div id='mother'>
<div id='child'></div>
</div>
<div id='stranger'></div>
</body>
</html>
It’s because #child is nested inside of #mother. If #mother is lower than #stranger, #mother’s #child is lower than stranger, too. See this explanation of stacking context.
You would get the result I think you expect if your markup was like so:
Then they would all be in the same stacking context.