Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

z-index not behaving as i'd expect

Tags:

html

css

z-index

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.)

like image 221
lawrence Avatar asked Dec 23 '08 19:12

lawrence


People also ask

Why Z Index property is not working?

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.

What do you do when Z index is not working?

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.

How do you do absolute positioning with Z index?

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).

Does Z Index work without position?

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.


2 Answers

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.

like image 145
Adam Bellaire Avatar answered Oct 05 '22 05:10

Adam Bellaire


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

like image 26
datasn.io Avatar answered Oct 05 '22 07:10

datasn.io