html - z 索引为 -1 的元素位于祖 parent 的 div 之下

标签 html css css-position z-index absolute

z-index:-1 放入 absolute-child 中,使 div 在祖 parent 之下。如何让绝对子级位于父级 div 而不是祖父级 div 之下?

.grandparent{
  background:rgba(0, 128, 0, 0.89);
  width:500px;
}

.parent{
  position:relative;
  width:200px;
  height:200px;
  background:yellow;
}

.absolute-child{
  position:absolute;
  width:100px;
  height:100px;
  background:red;
  top:10px;
  right:-50px;
  z-index:-1;
}
 <div class="grandparent">
    <div class="parent">
      <div class="absolute-child"> absolute ch
      </div>
      <div class="siblings">siblings</div>
    </div>
 </div

最佳答案

这就是你想要的,将 position: relative; 添加到祖 parent ,这样 z-index: 0; 就可以了。

.grandparent{
  background:rgba(0, 128, 0, 0.89);
  width:500px;
      z-index: 0;
    position: relative;
}

.parent{
  position:relative;
  width:200px;
  height:200px;
  background:yellow;
}

.absolute-child{
    position: absolute;
    width: 100px;
    height: 100px;
    background: red;
    top: 10px;
    right: -50px;
    z-index: -1;
}
<div class="grandparent">
    <div class="parent">
      <div class="absolute-child"> absolute ch
      </div>
      <div class="siblings">siblings</div>
    </div>
 </div

关于html - z 索引为 -1 的元素位于祖 parent 的 div 之下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46558871/

相关文章:

css - 如何在 SCSS 中使用多个 @include?

html - &lt;input&gt; 不适用于 css 中的下拉菜单

html - 使用相对定位将文本覆盖在图像上

javascript - d3 classed() 不能按预期与多个类一起工作?

html - 如何在浏览器重新调整时调整背景图片

javascript - 获取相对于父级的非绝对元素的位置

html - 如何使按钮上方的 <Span> 可点击(作为按钮)?

css - 视频背景上的文字变得不透明

html - 为什么内部div显示在容器div下面?

html - SVG 位置 :absolute in IE 11