Increase the z-index
of the header instead of decreasing the one of the content:
.container {
display: flex;
flex-direction: column;
}
.header {
background-color: green;
box-shadow: 5px 10px rgba(0, 0, 0, 0.5);
height: 60px;
width: 100%;
z-index:1;
}
.body {
height: 300px;
width: 100%;
background-color: red;
}
<div class='container'>
<div class='header'>
</div>
<div class='body'>
<p onClick="(function() {
alert('Testing');})()">
TESTING
</p>
</div>
</div>
Or add z-index:0
to the container to keep the .body
inside its stacking context and avoid it being behind the container (the issue you were facing)1
.container {
display: flex;
flex-direction: column;
position:relative;
z-index:0;
}
.header {
background-color: green;
box-shadow: 5px 10px rgba(0, 0, 0, 0.5);
height: 60px;
width: 100%;
}
.body {
height: 300px;
width: 100%;
background-color: red;
z-index:-1;
}
<div class='container'>
<div class='header'>
</div>
<div class='body'>
<p onClick="(function() {
alert('Testing');})()">
TESTING
</p>
</div>
</div>
To better see the initial issue simply add a background to the container and you will notice how the .body
is placed behind:
.container {
display: flex;
flex-direction: column;
background:yellow;
}
.header {
background-color: green;
box-shadow: 5px 10px rgba(0, 0, 0, 0.5);
height: 60px;
width: 100%;
}
.body {
height: 300px;
width: 100%;
background-color: red;
z-index:-1;
}
<div class='container'>
<div class='header'>
</div>
<div class='body'>
<p onClick="(function() {
alert('Testing');})()">
TESTING
</p>
</div>
</div>
1: For those with 'z-index: auto', treat the element as
if it created a new stacking context, but any positioned descendants
and descendants which actually create a new stacking context should be
considered part of the parent stacking context, not this new one.ref