[英]How can I give a child element of one parent a higher z-index than another parent?
我有一个侧边栏,它分为两个具有相等Z索引的div。
第一个div, top
,有一个链接,显示另一个div hover
,当你在它悬停。
hover
向下延伸到底部div, bottom
,但是由于top
和bottom
具有相同的z索引,因此hover
被bottom
覆盖。
不管我给bottom
赋予z-index多高,都只会影响它在top
显示方式。 我怎样才能得到它掩盖了bottom
?
顺便说一句,我也想对bottom
做同样的事情,因此会有一个应该bottom-hover
在top
的bottom-hover
。
因此,给top
和bottom
不同的z索引不是一种选择。
这是一个小提琴来演示: http : //jsfiddle.net/tsnuh7q1/
的HTML:
<div class="top">top
<div class="hover">HOVER</div>
</div>
<div class="bottom">bottom<div>
CSS:
.top {
width: 200px;
height: 100px;
background: blue;
z-index: 3;
position: relative;
}
.hover {
z-index: 40;
width: 170px;
height: 300px;
position: absolute;
background: red;
left: 30px;
}
.bottom {
width: 200px;
height: 100px;
background: green;
z-index: 3;
position: relative;
}
子z-index
始终位于父级上下文中。
采取
#A { z-index: 1; }
#B { z-index: 2; }
#A * { z-index: 1000; }
#A
孩子将永远在#B
下,并且是孩子。 它们的z-index
的上下文是较低的层。
在为我自己的问题寻找解决方案时遇到了这个问题。 不由得放手一搏。
如果我正确理解您要做什么,为什么不这样做呢?
http://jsfiddle.net/tsnuh7q1/2/
.top, .bottom { width: 100%; height: 100px; background: lightblue; position: relative; } .bottom { background: green; } .hover { position: absolute; top: 0; left: 10%; display: none; width: 100px; height: 150px; background: red; } a:hover .hover { display: block; } .bottom .hover { top: initial; left: initial; right: 10%; bottom: 0; } .top:hover, .bottom:hover { z-index: 1; }
<div class="top">top <a href="#0">link<div class="hover">HOVER</div></a> </div> <div class="bottom">bottom <a href="#0">link<div class="hover">HOVER</div></a> <div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.