Is there a way to position a non-fixed div that scrolls with the page above a fixed div? I tried z-index but that doesn't work. The fixed div has a box inside that has a layout based on the parent wrapper's width percentage. Below is my code and jsfiddle. I would like the red box that's fixed to be below the blue box that scrolls.
HTML
<div class="holder">
<div class="wrapper">
<div class="redbox">
<p>
red box
</p>
</div>
</div>
</div>
<div class="bluebox">
<p>
blue box
</div>
CSS
body, html {
height: 1000px;
margin: 0;
padding: 0;
}
.holder {
width: 100%;
position: fixed;
top: 0;
left: 0;
}
.wrapper {
width: 80%;
margin: 50px auto 0 auto;
}
.redbox {
width: 100px;
height: 100px;
background: red;
}
.bluebox {
width: 500px;
height: 500px;
margin: 75px 0 0 0;
background: blue;
}
In Short: You need to add position: relative
to the .bluebox
class, to add it to the same stack-context as the fixed one.
W3Schools says:
Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky).
This is related to the so called stack-context in a web page. As noted in the MDN web docs the stack context order is as: