简体   繁体   English

如何为子元素提供比父元素更高的z-index

[英]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.

相关问题 如何为一个父级的子元素赋予比另一个父级更高的z索引? - How can I give a child element of one parent a higher z-index than another parent? 如何在另一个具有较高 z-index 的元素的子元素前面获得具有较低 z-index 的元素的子元素? - How can I get a child of an element with lower z-index on front of a child of another element with higher z-index? 如何让子元素显示在其父元素后面(低于 z-index)? - How to get a child element to show behind (lower z-index) than its parent? 使z-index子级高于父级 - Make z-index child higher than parent 使孩子的z索引比父母高 - Make child to have higher z-index than parent 将子元素放置在z-index大于其父元素的元素上方 - Positioning a child element above an element having z-index greater than it's parent 当父级div的子级的z-index值高于两个值时,如何在其div上方堆叠父级div的值? - How to stack a parent div above a div whilst it's child has a higher z-index than both? 通过 z-index 和 position 将子元素移动到父元素下 - Move child element under the parent element by z-index and position 如何使parent1的子元素具有比父2的子元素更高的z索引(与其他父元素具有相同的z索引)? - How to make child elements from parent1 have a higher z-index than child elements from parent 2 (with same z-index as other parent)? 具有较高z索引的导航栏覆盖的元素 - Element covered by navbar with higher z-index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM