0

是否可以设置仅适用于某个“范围”的 z-index,例如仅影响某个元素的子元素。

我有一个 z-index 为 0 的 containerDiv。它包含一堆圆圈,它们应该以不同的深度相互叠加,但我不希望它们影响页面上的任何其他元素。

我在页面上有一堆其他元素(弹出窗口、下拉菜单等),它们的 z-index 为 1,我希望它们被放置在 containerDiv 及其所有子元素的顶部。由于我很懒,我最好避免必须根据具有最高 z-index 的圆圈来调整这些元素的 z-index 值......如果有某种方式可以让所有其他元素查看 containerDiv,那就太棒了并且所有它的孩子都具有相同的 z-index。

这可以用css实现吗?

4

1 回答 1

0

The answer depends on whether or not your other elements are descendants of the containerDiv or not. To answer the question: Yes, it's almost certainly possible, given a bit of shuffling of the markup.

But what you need to understand is the concept of stacking context:

http://www.w3.org/TR/CSS2/visuren.html#layers

Stacking context is not inherited the way other properties are: "A stacking context is atomic from the point of view of its parent stacking context; boxes in other stacking contexts may not come between any of its boxes." It's not like every element on the page with z-index:2 will be behind everything on the page with z-index:4. Z-index (combined with a position declaration) is typically (though not exclusively) used to resolve the stacking order when two elements share a containing element.

于 2012-06-28T02:43:48.293 回答