I am trying to make the black div
(relative) above the second yellow one (absolute). The black div
's parent has a position absolute, too.
#relative {_x000D_
position: relative;_x000D_
width: 40px;_x000D_
height: 100px;_x000D_
background: #000;_x000D_
z-index: 1;_x000D_
margin-top: 30px;_x000D_
}_x000D_
.absolute {_x000D_
position: absolute;_x000D_
top: 0; left: 0;_x000D_
width: 200px;_x000D_
height: 50px;_x000D_
background: yellow;_x000D_
z-index: 0;_x000D_
}
_x000D_
<div class="absolute">_x000D_
<div id="relative"></div>_x000D_
</div>_x000D_
<div class="absolute" style="top: 54px"></div>
_x000D_
Expected Result:
This question is related to
html
css
css-position
absolute
Just add the second .absolute div before the other .second div:
<div class="absolute" style="top: 54px"></div>
<div class="absolute">
<div id="relative"></div>
</div>
Because the two elements have an index 0.
How about this?
<div class="relative">
<div class="yellow-div"></div>
<div class="yellow-div"></div>
<div class="absolute"></div>
</div>
.relative{
position:relative;
}
.absolute {
position:absolute;
width: 40px;
height: 100px;
background: #000;
z-index: 1;
top:30px;
left:0px;
}
.yellow-div {
position:relative;
width: 200px;
height: 50px;
background: yellow;
margin-bottom:4px;
z-index:0;
}
use the relative div as wrapper and let the yellow div's have normal positioning.
Only the black block need to have an absolute position then.
I was struggling to figure it out how to put a div over an image like this:
No matter how I configured z-index in both divs (the image wrapper) and the section I was getting this:
Turns out I hadn't set up the background of the section to be background: white;
so basically it's like this:
<div class="img-wrp">
<img src="myimage.svg"/>
</div>
<section>
<other content>
</section>
section{
position: relative;
background: white; /* THIS IS THE IMPORTANT PART NOT TO FORGET */
}
.img-wrp{
position: absolute;
z-index: -1; /* also worked with 0 but just to be sure */
}
You have to put the second div on top of the first one because the both have an z-index of zero so that the order in the dom will decide which is on top. This also affects the relative positioned div because its z-index relates to elements inside the parent div.
<div class="absolute" style="top: 54px"></div>
<div class="absolute">
<div id="relative"></div>
</div>
Css stays the same.
try this code:
.absolute {
position: absolute;
top: 0; left: 0;
width: 200px;
height: 50px;
background: yellow;
}
I solved my z-index
problem by making the body wrapper z-index:-1
and the body z-index:-2
, and the other divs z-index:1
.
And then the later divs didn't work unless I had z-index
200+. Even though I had position:relative
on each element, with the body at default z-index
it wouldn't work.
Hope this helps somebody.
I was struggling with this problem, and I learned (thanks to this post) that:
div:first-child {_x000D_
opacity: .99; _x000D_
}_x000D_
_x000D_
.red, .green, .blue {_x000D_
position: absolute;_x000D_
width: 100px;_x000D_
color: white;_x000D_
line-height: 100px;_x000D_
text-align: center;_x000D_
}_x000D_
_x000D_
.red {_x000D_
z-index: 1;_x000D_
top: 20px;_x000D_
left: 20px;_x000D_
background: red;_x000D_
}_x000D_
_x000D_
.green {_x000D_
top: 60px;_x000D_
left: 60px;_x000D_
background: green;_x000D_
}_x000D_
_x000D_
.blue {_x000D_
top: 100px;_x000D_
left: 100px;_x000D_
background: blue;_x000D_
}
_x000D_
<div>_x000D_
<span class="red">Red</span>_x000D_
</div>_x000D_
<div>_x000D_
<span class="green">Green</span>_x000D_
</div>_x000D_
<div>_x000D_
<span class="blue">Blue</span>_x000D_
</div>
_x000D_
This is because of the Stacking Context, setting a z-index will make it apply to all children as well.
You could make the two <div>
s siblings instead of descendants.
<div class="absolute"></div>
<div id="relative"></div>
Source: Stackoverflow.com