CSS:z-index 不能在位置内工作:相对容器

标签 css

我想将 .wall-panel div 放在我的 img 后面(以便图像可见),但保留所有 fiddle 中的另一个定位。这在没有 position: relative 容器的情况下也有效,但是一旦进入容器,z-index 属性似乎什么都不做。

我做错了什么?

.container {
  position: relative;
  top: calc(100vh * 0.4);
  left: 0;
  width: 100%;
}

.image {
  z-index: 1;
}

.wall-panel {
  width: 100%;
  background-color: red;
  height: 200px;
  position: absolute;
  top: 0;
  z-index: 0;
}
<div class='container'>
  <img class='image' src='http://lorempixel.com/400/400/'>
  <div class='wall-panel' />
</div>
<div>
Some other text that positions under the container.
</div>

最佳答案

z-index 应该是 -1 这应该有效:

.container {
  position: relative;
  top: calc(100vh * 0.4);
  left: 0;
  width: 100%;
}

.image {
  z-index: 1;
}

.wall-panel {
  width: 100%;
  background-color: red;
  height: 200px;
  position: absolute;
  top: 0;
  z-index: -1;
}
<div class='container'>
  <img class='image' src='http://lorempixel.com/400/400/'>
  <div class='wall-panel'>
</div>
<div>
Some other text that positions under the container.
</div>
</div>

关于CSS:z-index 不能在位置内工作:相对容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48703120/

相关文章:

javascript - 当 html 文档高度不够时,菜单固定在滚动顶部的问题?

css - IE11不会从内网加载外部css

没有第一个 child 的CSS选择标签

php - preg_replace 删除样式文本/css 标签

css - 根据angular js中的某些条件模糊背景图像

css - 点击 :target 后隐藏链接

html - 交叉浏览 : OpenSans Chrome VS Safari

c++ - 如何使用 css 更改 QStackedWidget 中 QWidget 的背景颜色?

html - .class :hover not working in firefox?

html - 如何使用 z-index 将一个导航菜单堆叠在另一个导航菜单之上?