[英]How to give a child element a higher z-index than the parent element
I've run into a small problem with z-indexing. 我遇到了一个z-indexing的小问题。
I have a header and inside that a link. 我有一个标题,里面有一个链接。 The header has a z-index of 5, and the link has a z-index of 15. When I open up a modal, the dark overlay should be above the header, so I gave it a z-index of 10. The problem is that the link within the header should be above the overlay, but it's behind it.
标题的z-index为5,链接的z-index为15.当我打开一个模态时,暗覆盖应该在标题之上,所以我给它的z-index为10.问题是标题中的链接应该在叠加层之上,但它在它后面。 I've even given the link a position of relative, but it does nothing.
我甚至给链接一个相对的位置,但它什么也没做。
Can anyone help me with this problem? 任何人都可以帮我解决这个问题吗?
z-index
is relative to the siblings within the same parent. z-index
相对于同一父节点中的兄弟节点。 you will have to take the <a>
out of the <header>
to create the desired effect. 你必须从
<header>
取出<a>
来创建所需的效果。
If the parent has lower z-index
than the modal, everything in it will be behind the modal, regardless of the children's z-index
如果父级的
z-index
低于模态,那么无论孩子的z-index
如何,其中的所有内容都将位于模态之后
Please also attach a jsFiddle or something so we can show you how to fix this. 还请附上一个jsFiddle或其他东西,以便我们可以告诉你如何解决这个问题。
If anyone else ever has this problem, here's a possible solution: http://jsfiddle.net/flobar/r6zeoc0y/ 如果其他人有这个问题,这里有一个可能的解决方案: http : //jsfiddle.net/flobar/r6zeoc0y/
What I did was I used the header
as a container. 我做的是我用
header
作为容器。 I then seperated the elements that needed to be below and above the overlay into seperate div
's with different z-index
. 然后,我将需要在叠加层下方和上方的元素分离成具有不同
z-index
单独div
。 I also used position: absolute;
我也用过
position: absolute;
to position them correctly within the header
. 在
header
正确定位它们。 Like so you don't run into the issue of the children inheriting the parent's z-index. 像这样你不会遇到继承父母的z-index的孩子的问题。
You can give the parent DIV z-index: inherit and your a element inside the parent div. 你可以给父DIV z-index:inherit和你父元素中的一个元素。 z-index higher then the overlay.
z-index高于叠加层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.