无法让 z-index 在另一个 div 内的 div 上工作
我正在尝试让下拉菜单起作用。它由一个 LI 和一个当 LI 悬停时出现的 DIV 组成。我希望将父项绘制在子项前面,以便我可以创建具有重叠边框的选项卡式效果。我无法让孩子被拉到父母后面。
下面的代码也有同样的问题:
<div id="test_1">
Test 1
<div id="test_2">
Test 2
</div>
</div>
和 CSS
#test_1 {
border:1px solid green;
width:200px;
height:200px;
background-color:#fff;
position:relative;
z-index:999;
}
#test_2{
border:1px solid red;
width:200px;
height:200px;
background-color:#fff;
position:relative;
top:-10px;
left:-10px;
z-index:900;
}
上面的代码将在 test_1 的前面绘制 test_2。我希望 test_2 绘制在 test_1 之后。我怎样才能让它做到这一点?感谢您的任何帮助。
I'm trying to get a drop down menu to work. It consists of an LI with a DIV that appears when the LI is hovered. I want the parent to be drawn in front of the child so that I can create a tabbed effect with the overlapping borders. I can't get the child to be drawn behind the parent.
The following code has the same problem:
<div id="test_1">
Test 1
<div id="test_2">
Test 2
</div>
</div>
And the CSS
#test_1 {
border:1px solid green;
width:200px;
height:200px;
background-color:#fff;
position:relative;
z-index:999;
}
#test_2{
border:1px solid red;
width:200px;
height:200px;
background-color:#fff;
position:relative;
top:-10px;
left:-10px;
z-index:900;
}
The above code will draw test_2 in FRONT of test_1. I want test_2 to be drawn BEHIND test_1. How can I get it to do that? Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定位的容器始终包含其子容器。容器的内容不能位于容器本身下方。请参阅我(大约 7 年前)制作的显示情况的此测试文件。
请特别注意,深蓝色 div 为
z-index:-100
,但不会出现在其z-index:3
父容器下方,也不会出现在不同的 < code>z-index:2 容器是它的“叔叔”(其父级的兄弟)。元素的
z-index
仅对于使用相同定位容器的其他元素有效。一旦您在test_1
上设置了position:relative
,您就会导致其z-index
与z-index 处于不同的世界
。您可以做的最接近的操作是设置test_2
的#test_2 { z-index:-1 }
使其显示在#test_1
内容下方(但不在其背景下方)。A positioned container always contains its children. You can't have the content of a container below the container itself. See this test file that I made (about 7 years ago) showing the situation.
Note in particular that the dark blue div is
z-index:-100
, but doesn't appear below itsz-index:3
parent container, nor below a differentz-index:2
container that is its "uncle" (sibling of its parent).The
z-index
of an element is only valid with respect to other elements using the same positioned container. Once you setposition:relative
ontest_1
you are causing itsz-index
to be in a different world than thez-index
oftest_2
. The closest you can do is set#test_2 { z-index:-1 }
to cause it to appear below the content of#test_1
(but not below its background).我可以给你一个关于 Bootstrap Modals 的常见例子。考虑一下您的 #test_1 是我的 .modal-body,您的 #test_2 是我的自定义
我只是必须确保#test_1
overflow
计算属性不是“auto”或“scroll”或“hidden”,否则,该模式会在中间剪切我的I can give you a common example about Bootstrap Modals. Consider that your #test_1 is my .modal-body and your #test_2 is my custom
<select>
box, OK? #test_2 is inside #test_1. Simple as that.I just have had to make sure #test_1
overflow
computed property is not "auto" or "scroll" or "hidden", otherwise, the modal would cut my<select>
box listing in the middle. My current.modal-body
overflow isinitial