【问题标题】:Why a div with higher z-index is not visible (covered)?为什么 z-index 较高的 div 不可见(覆盖)?
【发布时间】:2014-12-30 02:53:17
【问题描述】:

this example 中,内部绿色 div 被中间蓝色 div 覆盖,至少在 Chrome 中,尽管它具有更高的 z-index。为什么会这样以及如何使它在蓝色 div 上可见?绿色 div 必须在外部黄色 div 内。从上到下:

1. #inner
2. #middle
3. #outer

【问题讨论】:

  • 您需要将#middle div 移动到#outer 中,以便z-index 工作jsfiddle.net/61cgeqs0/2
  • @Vitorino 谢谢,好提示!

标签: css css-position z-index


【解决方案1】:

您可以做到这一点,但您需要将 #middle div 移动到 #outer 内,以便 z-index 工作

div {
  position: absolute;
}
#outer {
  width: 300px;
  height: 100px;
  top: 20px;
  background: yellow;
}
#inner {
  width: 240px;
  height: 80px;
  top: 10px;
  left: 30px;
  background: green;
  z-index: 2;
}
#middle {
  width: 280px;
  height: 140px;
  left: 10px;
  z-index: 1;
  background: blue;
  top: -16px;
}
<div id="outer">
  <div id="inner"></div>
  <div id="middle"></div>
</div>

【讨论】:

  • 为什么会这样?为什么外部 div 不能有 z-index 并包含带有另一个 z-index 的 div?
  • z-index 仅适用于单个元素,而不适用于父子元素,例如,如果有 3 个单独的元素,您可以设置 z-index,但如果您有父子元素,则不允许子元素超过父母
猜你喜欢
  • 2018-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
相关资源
最近更新 更多