如何让子元素显示在其父元素后面(低于 z-index)?
我需要某个动态元素始终出现在另一个元素的顶部,无论它们在 DOM 树中的顺序如何。这可能吗?我尝试过 z-index
(使用 position:relative
),但它似乎不起作用。
我需要:
<div class="a">
<div class="b"></div>
</div>
<div class="b">
<div class="a"></div>
</div>
渲染时显示完全相同。出于灵活性目的(我计划分发一个需要此功能的插件),我真的希望不必诉诸绝对或固定定位。
无论如何,为了执行我想要的功能,我做了一个条件语句,其中重叠的子元素在遮挡其父元素的视图的情况下将变得透明。它并不完美,但它是一些东西。
I need a certain dynamic element to always appear on top of another element, no matter what order in the DOM tree they are. Is this possible? I've tried z-index
(with position: relative
), and it doesn't seem to work.
I need:
<div class="a">
<div class="b"></div>
</div>
<div class="b">
<div class="a"></div>
</div>
To display exactly the same when rendered. And for flexibility purposes (I'm planning on distributing a plugin that needs this functionality), I'd really like to not have to resort to absolute or fixed positioning.
For what it's worth, to perform the function I was wanting, I made a conditional statement where the overlapping child element would become transparent in the case it was blocking the view of its parent. It's not perfect, but it's something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
现在,您可以使用带有 CSS 的 3D 变换在现代浏览器中执行此操作。这在 2010 年还不可能,但现在已经可以了。
You can do this in modern browsers now using transform 3D with CSS. It wasn't really possible in 2010 but it is now.
如果元素形成层次结构,则不能以这种方式完成,因为每个定位元素都会创建新的 堆叠上下文< /a>,而z-index是相对于同一堆叠上下文的元素。
If the elements make a hierarchy, it cannot be done that way, because every positioned element creates new stacking context, and z-index is relative to the elements of the same stacking context.
我找到了一种通过创建模仿父元素的伪元素来将子元素实际放置在其元素后面的方法。对于下拉菜单之类的东西很有用 - 希望它能有所帮助,八年前的用户!
I figured out a way to actually put the child element behind its element by creating a pseudo-element which mimics the parent. Useful for stuff like dropdown-menus - hope it helps, user from eight years ago!
问题的答案
是:
堆叠上下文
定位堆叠上下文
,其 z-index 小于 0。注意:我说让子级成为一个定位堆叠上下文,
不仅仅是堆叠上下文,因为请注意z-index 仅对定位元素有效)
有关如何将元素视为堆叠上下文,请阅读这个,以及如何考虑元素定位元素,请阅读此。或者来自这个答案的精彩解释。
结论:
z-index
小于 0z-index
适用但是您必须理解 2 个术语堆叠上下文 和 定位元素
The answer for the question
is to:
stacking context
positioned stacking context
, whose z-index smaller than 0.Notice: I said make the child a positioned stacking context,
not just stacking context, because pay attention that z-index will only be valid for positioned element)
About how an element is considered a stacking context, read this, and how an element is considered a positioned element, read this. Or a great explanation from this answer.
Conclusion:
z-index
is smaller than 0z-index
from last step is applicableYet you have to understand 2 terms stacking context and positioned element
我已经成功使用以下内容。
z 索引:-1;
I've had success using the following.
z-index: -1;
为了让 z-index 工作,你需要的是
position:absolute
。您还需要设置
left
和top
css 属性以将元素放置在正确的位置。您可能需要使用 javascript 来完成此操作。what you need is
position: absolute
in order for z-index to work.also you will need to set the
left
andtop
css properties to position the element in the correct place. You will probably need to do this with javascript.