How to get a parent element to appear above child [duplicate]
Set a negative z-index
for the child, and remove the one set on the parent.
.parent {
position: relative;
width: 350px;
height: 150px;
background: red;
border: solid 1px #000;
}
.parent2 {
position: relative;
width: 350px;
height: 40px;
background: red;
border: solid 1px #000;
}
.child {
position: relative;
background-color: blue;
height: 200px;
}
.wrapper {
position: relative;
background: green;
height: 350px;
}
<div class="wrapper">
<div class="parent">parent 1 parent 1
<div class="child">child child child</div>
</div>
<div class="parent2">parent 2 parent 2
</div>
</div>
https://jsfiddle.net/uov5h84f/
Fortunately a solution exists. You must add a wrapper for parent and change z-index of this wrapper for example 10, and set z-index for child to -1:
.parent {
position: relative;
width: 750px;
height: 7150px;
background: red;
border: solid 1px #000;
z-index: initial;
}
.child {
position: relative;
background-color: blue;
z-index: -1;
color: white;
}
.wrapper {
position: relative;
background: green;
z-index: 10;
}
<div class="wrapper">
<div class="parent">parent parent
<div class="child">child child child</div>
</div>
</div>
Some of these answers do work, but setting position: absolute;
and z-index: 10;
seemed pretty strong just to achieve the required effect. I found the following was all that was required, though unfortunately, I've not been able to reduce it any further.
HTML:
<div class="wrapper">
<div class="parent">
<div class="child">
...
</div>
</div>
</div>
CSS:
.wrapper {
position: relative;
z-index: 0;
}
.child {
position: relative;
z-index: -1;
}
I used this technique to achieve a bordered hover effect for image links. There's a bit more code here but it uses the concept above to show the border over the top of the image.
http://jsfiddle.net/g7nP5/
You would need to use position:relative
or position:absolute
on both the parent and child to use z-index
.