CSS z-index 未正确堆叠
我在 CSS z-index 堆叠方面遇到问题。
HTML:
<ul>
<li><a href="#">Title 1</a></li>
<li class="dropMenu"><a href="#">Title 2</a>
<div class="containerDropDown">
<ul class="menu">
<li><a href="#">Lorem ipsum link</a></li>
<li><a href="#">Lorem ipsum dolor sit met link amet ipsum link</a></li>
<li><a href="#">Dolor sit amet link</a></li>
<li><a href="#">Lorem ipsum link</a></li>
<li><a href="#">Dolor sit amet link</a></li>
<li><a href="#">Lorem ipsum link</a></li>
</ul>
</div>
</li>
<li><a href="#">Title 3</a></li>
<li><a href="#">Title 4</a></li>
<li><a href="#">Title 5</a></li>
</ul>
CSS:
li.dropMenu {
z-index:100;
}
.dropMenu:hover {
padding-bottom:9px;
border:1px solid #575a5d;
border-bottom:0;
background-color:#434749;
position:relative;
-moz-box-shadow:0 0 5px #000;
-webkit-box-shadow:0 0 5px #000;
box-shadow:0 0 10px #000;
z-index:100;
}
.dropMenu:hover a {
padding:0 9px;
color:#8f6f4d;
}
.dropMenu ul {
width:198px;
left:-999em;
padding:16px 0 0 0;
border:1px solid #575a5d;
background-color:#434749;
position:absolute;
-moz-box-shadow:0 0 5px #000;
-webkit-box-shadow:0 0 5px #000;
box-shadow:0 0 5px #000;
}
.dropMenu:hover ul {
/*top:32px;*/
top:10px;
left:-1px;
z-index:20;
}
.dropMenu ul li {
display:block;
width:100%;
padding:0;
z-index:70;
}
.dropMenu:hover ul li a,
.dropMenu ul li a{
display:block;
padding:0 30px 22px 30px;
font-size:0.8em;
color:#d0cfcb;
background:transparent url("/images/background/dropDownMenu-arrow.gif") no-repeat 21px 4px;
}
我需要让 li.dropMenu
具有比其子 ul
更高的堆叠顺序。我尝试更改 z-index 但没有成功。有谁知道有什么解决办法吗?我正在尝试创建一个简单的下拉菜单,但子 UL 似乎始终位于父 li.dropMenu
之上。
我给子 ul
一个盒子阴影,但因为它总是在顶部,所以阴影会覆盖 li.dropMenu
I am having trouble with CSS z-index stacking.
HTML:
<ul>
<li><a href="#">Title 1</a></li>
<li class="dropMenu"><a href="#">Title 2</a>
<div class="containerDropDown">
<ul class="menu">
<li><a href="#">Lorem ipsum link</a></li>
<li><a href="#">Lorem ipsum dolor sit met link amet ipsum link</a></li>
<li><a href="#">Dolor sit amet link</a></li>
<li><a href="#">Lorem ipsum link</a></li>
<li><a href="#">Dolor sit amet link</a></li>
<li><a href="#">Lorem ipsum link</a></li>
</ul>
</div>
</li>
<li><a href="#">Title 3</a></li>
<li><a href="#">Title 4</a></li>
<li><a href="#">Title 5</a></li>
</ul>
CSS:
li.dropMenu {
z-index:100;
}
.dropMenu:hover {
padding-bottom:9px;
border:1px solid #575a5d;
border-bottom:0;
background-color:#434749;
position:relative;
-moz-box-shadow:0 0 5px #000;
-webkit-box-shadow:0 0 5px #000;
box-shadow:0 0 10px #000;
z-index:100;
}
.dropMenu:hover a {
padding:0 9px;
color:#8f6f4d;
}
.dropMenu ul {
width:198px;
left:-999em;
padding:16px 0 0 0;
border:1px solid #575a5d;
background-color:#434749;
position:absolute;
-moz-box-shadow:0 0 5px #000;
-webkit-box-shadow:0 0 5px #000;
box-shadow:0 0 5px #000;
}
.dropMenu:hover ul {
/*top:32px;*/
top:10px;
left:-1px;
z-index:20;
}
.dropMenu ul li {
display:block;
width:100%;
padding:0;
z-index:70;
}
.dropMenu:hover ul li a,
.dropMenu ul li a{
display:block;
padding:0 30px 22px 30px;
font-size:0.8em;
color:#d0cfcb;
background:transparent url("/images/background/dropDownMenu-arrow.gif") no-repeat 21px 4px;
}
I need to get li.dropMenu
to have a higher stacking order than its child ul
. I tried to change the z-index but with no luck. Does any one know of any solutions? I am trying to create a simple drop down menu but the child UL seems to be always on top of the parent li.dropMenu
.
I am giving a box-shadow to the child ul
but because its always on top the shadow goes over the li.dropMenu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在 CSS 中为要使用
z-index
的每个元素设置一个position
属性(static
除外)。You need to set a
position
attribute (other thanstatic
) in your CSS for each element you want to usez-index
on.您需要为
li.dropMenu
指定一个position
才能使z-index
正常工作。在这里尝试
更多信息:
http://reference.sitepoint.com/css/z-index
You need to give the
li.dropMenu
aposition
in order for thez-index
to work.Try
More info here:
http://reference.sitepoint.com/css/z-index
您遇到的问题是,绝对定位的元素是相对于其最近的祖先以非静态(即相对、绝对、固定)定位放置的。因此,您的子 z-index 仅在父元素的上下文中应用,而不是与它进行比较。解决方案:将父级的内容放在子级的同级中:
使用 CSS(注意,为了简洁起见,我删除了一些原始内容):
The problem that you have is that an absolutely-positioned element is placed relative to it's closest ancestor with a non-static (i.e., relative, absolute, fixed) positioning. So your child z-index is applied only within the context of the parent element, not in comparison to it. Solution: place the content of the parent in a sibling of the child:
With the CSS (note, I've removed some of the original for brevity):
z-index 仅适用于定位元素(位置:绝对、位置:相对或位置:固定)。
z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).
首先删除 z 索引,
然后您应该定位同级
a
和.containerDropDown
,或者删除.containerDropDown
> 并只需使用ul
即可。那么你只需要添加:
.dropMenu>a{position:relative;z-index:2}
这比尝试使子 z-index 小于父 z-index 要容易得多。
做了一个小提琴: http://jsfiddle.net/filever10/TbJt3/
start by removing the z-indices
then you should be targeting the siblings
a
and.containerDropDown
, or remove.containerDropDown
and just use theul
instead.then you just need to add:
.dropMenu>a{position:relative;z-index:2}
which is much easier than trying to make a child z-index less than parent.
made a fiddle: http://jsfiddle.net/filever10/TbJt3/