so i've got this, roughly:
<div id="A">
<ul>
<li id="B">foo</li>
</ul>
</div>
<div id="C">
...
</div>
These are positioned so that B and C overlap.
A has a z-index
of 90
, B has a z-index
of 92
, and C has a z-index
of 91
. But C shows up in front of B. What am i doing wrong? (Let me know if more detail is necessary.)
You set z-index on a static element By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.
To sum up, most issues with z-index can be solved by following these two guidelines: Check that the elements have their position set and z-index numbers in the correct order. Make sure that you don't have parent elements limiting the z-index level of their children.
Definition and Usage An element with greater stack order is always in front of an element with a lower stack order. Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).
z-index only works on positioned elements. If you try to set a z-index on a non-positioned element, it will do nothing. However, there is one exception - flex children can use z-index even if they're non-positioned.
Using z-index
is only relevant for elements in the same container. Since B is contained inside A, B's z-index will only apply when resolving other elements inside A. As far as C is concerned, both B and A are rendered at z-index 90. However if C is placed inside A, then B will render in front.
If an element has no position:relative / position:absolute / position:fixed style, it's position:static which is the default position style for all elements.
With an element who is position:static, z-index simply doesn't work. The browser would render the stack in the order of xml element and ignore z-index property.
For a situation like this to work, you have to add position:relative to all 3 elements, A, B, C.
To understand more about z-index and CSS stacking, head to here: http://www.tjkdesign.com/articles/z-index/teach_yourself_how_elements_stack.asp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With