Z-index With Different Parents
I'm having some trouble with the z-index. This is my simplified layout:
Solution 1:
First, make sure that overlapper
, child1
and child2
belong to the same stacking context.
That is, make sure container
doesn't create a stacking context:
container
must not be the root element.container
must have the default value forposition
,z-index
,opacity
andisolation
properties:position: static
orz-index: auto
opacity: 1
isolation: auto
Now, make overlapper
, child1
and child2
positioned elements, and add z-index
as you want.
#overlapper, #child1, #child2 {
position: relative;
padding: 30px;
height: 20px;
}
#overlapper {
z-index: 3;
background: red;
top: 60px;
margin-left: 20px;
}
#container {
margin-top: -40px;
}
#child1 {
z-index: 2;
background: green;
top: -40px;
}
#child2 {
z-index: 4;
background: blue;
margin-left: 40px;
}
<divid="overlapper">Overlapper</div><divid="container"><divid="child1">Child 1</div><divid="child2">Child 2</div></div>
Solution 2:
HTML
<div id="overlapper"></div>
<div id="container">
<div id="child1"></div>
<div id="child2"></div>
</div>
CSS
#overlapper, #child1, #child2
{
height: 100px;
width: 100px;
position: absolute;
}
#overlapper
{
background: red;
top : 40px;
left : 40px;
z-index: 1;
}
#child1
{
background: yellow;
z-index: 0;
}
#child2
{
top: 60px;
left : 60px;
background: black;
z-index: 2;
}
Post a Comment for "Z-index With Different Parents"