简体   繁体   English

绝对元素 z-index 内的固定元素不起作用

[英]Fixed element inside absolute element z-index does not work

I got two absolute elements with the same z-index .我得到了两个具有相同z-index absolute元素。 One of them has a fixed-position child which has an higher z-index than the parent.其中一个有一个fixed-position子节点,它的z-index比父节点高。

Why the child only overlaps the parent but not the other absolute element with a lower z-index .为什么子元素仅与父元素重叠,而不与具有较低z-index的另一个绝对元素重叠。 How to achieve that the child overlaps both lower z-index elements?如何实现子元素与两个较低的z-index元素重叠?

 .lower-element { background: green; width: 3em; height: 3em; position: absolute; z-index: 10; } .higher-element { background: blue; width: 100vw; height: 100vh; position: fixed; opacity: .5; z-index: 20; } .lower-element-1 { top: 5em; }
 <div class="lower-element"> <div class="higher-element"> </div> </div> <div class="lower-element lower-element-1"> </div>

position: fixed and position: absolute only usable with the​ top , bottom , left or right values. position: fixedposition: absolute仅可用与topbottomleftright的值。

 .lower-element { background: green; width: 3em; height: 3em; position: absolute; z-index: 10; } .higher-element { background: blue; width: 100vw; height: 100vh; position: fixed; opacity: .5; z-index: 20; top:5em; /* I added */ } .lower-element-1 { top: 5em; }
 <div class="lower-element"> <div class="higher-element"> </div> </div> <div class="lower-element lower-element-1"> </div>

Remove all your z-index and add only to .higher-element : z-index: -1;删除所有z-index并仅添加到.higher-element : z-index: -1;

 .lower-element { background: green; width: 3em; height: 3em; position: absolute; } .higher-element { background: blue; width: 100vw; height: 100vh; position: fixed; opacity: .5; z-index: -1; } .lower-element-1 { top: 5em; }
 <div class="lower-element"> <div class="higher-element"> </div> </div> <div class="lower-element lower-element-1"> </div>

By making .higher-element to be siblings with .lower-element , then set .higher-element with z-index: -1通过使.higher-element.lower-element成为兄弟,然后将.higher-element设置为z-index: -1

 .lower-element { background: green; width: 3em; height: 3em; position: absolute; z-index: 10; } .higher-element { background: blue; width: 100vw; height: 100vh; position: fixed; opacity: .5; z-index: -1; } .lower-element-1 { top: 5em; }
 <div class="lower-element"> </div> <div class="higher-element"> </div> <div class="lower-element lower-element-1"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM