Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are z-index of child elements local to the parent?

Tags:

html

css

z-index

Okay so I have this test setup in my html

<div style="position:absolute;left:0px;top:0px;z-index:1;width:200px;height:200px;background-color:red;">
<div style="position:absolute;left:0px;top:0px;z-index:3;width:50px;height:50px;background-color:green;"></div>
</div>

<div style="position:absolute;left:0px;top:0px;z-index:2;width:100px;height:100px;background-color:blue"></div>

Where the I want the blue div to render in front of the red div, but I want the child div(green) of red to render in front of blue, looking at that code it seems it should work if z-index was global, but it seems to be local to the parent element, as the blue render above red, but the green doesn't render on top unless I move the green div up out of the parent? How can I use "global z-indexes" or whatever it is. It is vital to my actual project that it works this way

like image 459
Greg Hornby Avatar asked Feb 17 '13 05:02

Greg Hornby


People also ask

Is Z index relative to parent?

The z-index of elements inside of a stacking context are always relative to the parent's current order in its own stacking context.

Does Z Index apply to children?

You can change the z-index of any child elements all you want, but that won't do anything! The z-value of each of these elements is 6, and there's no way to change that without modifying the z-index of #parent. In technical terms, a stacking context is formed when a positioned element has a z-index.

Do child elements inherit Z index?

No, it isn't inherited.

Do kids have higher z index than parents?

This is impossible as a child's z-index is set to the same stacking index as its parent.


1 Answers

Z-indices are always local, or relative, to the closest positioned ancestor (in this case the parent). There is no global z-index property in CSS.

You will have to move the green div out of its parent.

like image 162
BoltClock Avatar answered Sep 24 '22 20:09

BoltClock