You can’t have an element outside the parent be between the parent and child. That’s because the sticky
position (like most positions) creates a new stacking context:
In other words, anything below a z-index of 50 will be below both elements, anything above 50 will be above both elements.
To achieve what you’re trying to do, the other elements must both be in a stacking context that is shared by element A
. Assuming the parent must remain sticky
, you can either move the child outside the parent, or put the new div inside the parent.
element A
is red, the sticky
div is blue, and the child is green.
<p>Scroll Down</p>
<div style="width: 200px; height: 200px; position: sticky; z-index: 10; background: blue; top:50px; margin-top: 300px"></div>
<!-- Child moved to main stacking context -->
<div style="position: sticky; width: 100px; height: 100px; z-index: 50; background: green; top:50px; margin-top: 200px"></div>
<!-- New div with z-index between -->
<div style="position: fixed; width: 150px; height: 150px; z-index: 11; background: red; top:50px;"></div>
<!-- Make some scrolling room -->
<div style="height:2000px; width:1px;"></div>
OR
<p>Scroll Down</p>
<div style="width: 200px; height: 200px; position: sticky; z-index: 10; background: blue; top:50px; margin-top: 300px">
<!-- New div with z-index between -->
<div style="position: fixed; width: 150px; height: 150px; z-index: 11; background: red; top:50px;"></div>
<div style="position: sticky; width: 100px; height: 100px; z-index: 50; background: green; top:50px; margin-top: 200px"></div>
</div>
<!-- Make some scrolling room -->
<div style="height:2000px; width:1px;"></div>
You could put the child inside the new div as well.