[英]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: fixed
和position: absolute
仅可用与top
, bottom
, left
或right
的值。
.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.