简体   繁体   English

使孩子的z索引比父母高

[英]Make child to have higher z-index than parent

Basically I'm looking to do this, but this doesn't work. 基本上,我正在寻找这样做,但这是行不通的。 How can I achieve the desired result without having to move it to a different place in the DOM? 我如何才能获得所需的结果而不必将其移到DOM中的其他位置?

<div id="parent">
  <div id="child">
  </div>
</div>

#parent {
  z-index: -1
}
#child {
  z-index: 999999;
  position: absolute
}

Here it is: 这里是:

#parent {
    position: relative;
    z-index: 1;
}

#child {
    position: relative;
    z-index: 5;
}
  1. Child is always on top of the parent by default; 默认情况下,子级始终位于父级之上;
  2. z-index requires non-static position property for proper working. z-index需要非静态position属性才能正常工作。

Try setting relative position on the parent, and absolute position on the child with offset values all set to 0 . 尝试在父级上设置relative位置,并在子级上设置偏移值都设置为0 absolute位置。

#parent {
  position: relative;
}
#child {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

1.) The parent needs to have position: relative (or fixed, or absolute) 1.)父母需要有以下position: relative (或固定的或绝对的)

2.) The child needs to have position: absolute and defined width/height, and/or optionally settings for top/bottom/right/left as desired. 2.)孩子需要有以下position: absolute和定义的宽度/高度,和/或根据需要可选地设置上/下/右/左。

In this simple configuration you don't need any z-index at all - the child is above the parent. 在这种简单的配置中,您根本不需要任何z-index-子级位于父级之上。 In more complex situations you can use z-index. 在更复杂的情况下,可以使用z-index。

 #parent { position: relative; background: blue; width: 600px; height: 500px; } #child { position: absolute; background:red; width: 400px; height: 300px; left: 100px; top: 100px; } 
 <div id="parent"> <div id="child"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM