【发布时间】:2018-12-31 16:31:36
【问题描述】:
我正在尝试在两个嵌套元素之间呈现一个元素。这可能最好用一个例子来解释:
#parent {
position: fixed;
top: 0;
left: 0;
width: 200px;
height: 200px;
z-index: 0;
background-color: red;
}
#child {
position: fixed;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
z-index: 2;
background-color: blue;
}
#other {
position: fixed;
top: 25px;
left: 25px;
width: 50px;
height: 50px;
z-index: 1;
background-color: green;
}
<div id="parent">
<div id="child"></div>
</div>
<!-- I want to have this element in between the "parent" and "child". -->
<div id="other"></div>
在这种情况下,我希望将绿色(“#other”)元素呈现在(z 深度方向)红色父元素(“#parent”)和蓝色子元素(“#child”)之间。换句话说,我希望蓝色元素位于顶部。
据我了解,这是不可能使用 CSS 的 z 深度(就像我尝试过的那样),因为元素是嵌套的,但我似乎无法找到不同的方式。
如果可能的话,我想保持 HTML 的原样,并完全在 CSS 中完成。
提前致谢!
【问题讨论】:
-
为什么要把其他元素放在父元素之外
-
``
-
你可以在你的子元素中使用 CSS 的 :before 伪元素并尝试
标签: css css-position z-index