<style>
#shadow {
display: flex;
}
#backdrop {
position: absolute;
z-index: 1;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.5);
}
</style>
<div id="backdrop"></div>
<div id="shadow"></div>
<script>
const root = shadow.attachShadow({ mode: 'open' })
root.innerHTML = `
<div>asf</div>
<style>
div {
background: white;
z-index: 1;
}
</style>`
</script>
Consider the following HTML:
Details
The
divchild of the shadow root is allowed to have a z-index even though it is not usingposition. Turns out that flex items are allowed to use z-index and they are treated as though the element is positioned.When we calculate the stacking order for elements, we need to take into account flex children and treat them as though they are positioned. An example of doing that can be found in the calculation for stacking context.