Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

z-index inherits parents z-index , or not?

Tags:

javascript

css

I have two absolute divs. One item on div A will show div B (on click + some javascript code). I want to have a higher Zindex on B than on A. (I want B above A)-

This item has its own zindex (lower than div A zindex). I thouhgt than zindex was inheritated by childrens from parents , but it seems it doesn't.

The question is ...? How can I get the 'computed' zindex for my 'item'

like image 919
civiltomain Avatar asked Apr 28 '14 19:04

civiltomain


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.

Can child Z index be higher than parent?

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

Does Z index affect children?

the z-index default is auto which states "Sets the stack order equal to its parents". However, using z-index on the child will move it out of the natural stack order and place it behind the background.

What does the Z index Property determine?

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.


1 Answers

No, it isn't inherited. You can see it in MDN article.

However, be aware that z-index sets the z-position relatively to the stacking context. And a positioned element with non auto z-index will create an stacking context.

That means that if you have

<div id="a">
    <div id="b"></div>
</div>
<div id="c"></div>

#a, #b, #c { position: absolute; top: 0 }
#a { z-index: 1; }
#b { z-index: 1000000; }
#c { z-index: 2; }

Then, #c will overlap #b, even though #b has higher z-index.

Therefore, z-index is not technically inherited, but z-index of ancestors does affect z-position.

I suggest reading What No One Told You About Z-Index

like image 52
Oriol Avatar answered Oct 04 '22 20:10

Oriol