如何让父元素出现在子元素上方
我有两个嵌套的 CSS 元素。我需要让父元素位于子元素的顶部,即 z 轴上方。仅设置 z-index 是不够的。
我无法在子项上设置负 z 索引,这会将其设置为实际页面上的页面容器下方。这是唯一的方法吗?
.parent {
position: relative;
width: 750px;
height: 7150px;
background: red;
border: solid 1px #000;
z-index: 1;
}
.child {
position: absolute;
background-color: blue;
z-index: 0;
color: white;
top: 0;
}
.wrapper
{
position: relative;
background: green;
z-index: 10;
}
<div class="wrapper">
<div class="parent">
parent parent
<div class="child">
child child child
</div>
</div>
</div>
I have two nested CSS elements. I need to get the parent to be on top, that is, above in z-axis, of the child element. Just setting z-index is not sufficient.
I can't set a negative z-index on the child, that'll set it to below the page container on my actual page. Is this the only method?
.parent {
position: relative;
width: 750px;
height: 7150px;
background: red;
border: solid 1px #000;
z-index: 1;
}
.child {
position: absolute;
background-color: blue;
z-index: 0;
color: white;
top: 0;
}
.wrapper
{
position: relative;
background: green;
z-index: 10;
}
<div class="wrapper">
<div class="parent">
parent parent
<div class="child">
child child child
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为子级设置一个负的
z-index
,并删除父级上设置的一个。https://jsfiddle.net/uov5h84f/
Set a negative
z-index
for the child, and remove the one set on the parent.https://jsfiddle.net/uov5h84f/
幸运的是,存在解决方案。您必须为父级添加一个包装器并更改此包装器的 z-index(例如 10),并将子级的 z-index 设置为 -1:
Fortunately a solution exists. You must add a wrapper for the parent and change z-index of this wrapper, for example 10, and set z-index for the child to -1:
其中一些答案确实有效,但设置
position:absolute;
和z-index: 10;
似乎足以达到所需的效果。我发现以下内容就是所需的全部内容,但不幸的是,我无法进一步减少它。HTML:
CSS:
我使用这种技术来实现图像链接的边框悬停效果。这里有更多的代码,但它使用上面的概念来显示图像顶部的边框。
http://jsfiddle.net/g7nP5/
Some of these answers do work, but setting
position: absolute;
andz-index: 10;
seemed pretty strong just to achieve the required effect. I found the following was all that was required, though unfortunately, I've not been able to reduce it any further.HTML:
CSS:
I used this technique to achieve a bordered hover effect for image links. There's a bit more code here but it uses the concept above to show the border over the top of the image.
http://jsfiddle.net/g7nP5/
您需要在父级和子级上都使用
position:relative
或position:absolute
才能使用z-index
。You would need to use
position:relative
orposition:absolute
on both the parent and child to usez-index
.由于您的 div 是
position:absolute
,因此就位置而言,它们并不是真正嵌套的。在你的 jsbin 页面上,我将 HTML 中的 div 顺序切换为:并且红色框覆盖了蓝色框,我认为这就是你正在寻找的。
Since your divs are
position:absolute
, they're not really nested as far as position is concerned. On your jsbin page I switched the order of the divs in the HTML to:and the red box covered the blue box, which I think is what you're looking for.
破解了它。基本上,发生的情况是,当您将 z-index 设置为负数时,它实际上会忽略父元素,无论它是否定位,并位于下一个定位元素的后面,在您的情况下,该元素是您的主容器。因此,您必须将父元素放在另一个定位的 div 中,并且您的子 div 将位于其后面。
解决这个问题对我来说是一个救星,因为我的父元素特别无法定位,以便我的代码正常工作。
我发现所有这些对于实现此处指示的效果非常有用: 仅使用 CSS ,将鼠标悬停在 上时显示 div
Cracked it. Basically, what's happening is that when you set the z-index to the negative, it actually ignores the parent element, whether it is positioned or not, and sits behind the next positioned element, which in your case was your main container. Therefore, you have to put your parent element in another, positioned div, and your child div will sit behind that.
Working that out was a life saver for me, as my parent element specifically couldn't be positioned, in order for my code to work.
I found all this incredibly useful to achieve the effect that's instructed on here: Using only CSS, show div on hover over <a>
款式:
本体:
style:
body: