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

前端 未结 3 1587
我寻月下人不归
我寻月下人不归 2020-12-31 07:03

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

Thi

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 07:29

    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

    #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

提交回复
热议问题