【问题标题】:How to get a parent element to appear above child [duplicate]如何让父元素出现在子元素上方[重复]
【发布时间】:2009-11-27 01:38:19
【问题描述】:

我有两个嵌套的 CSS 元素。我需要让父元素位于子元素的顶部,即 z 轴上方。仅设置 z-index 是不够的。

我无法在子级上设置负 z-index,这会将其设置在我实际页面上的页面容器下方。这是唯一的方法吗?

http://jsbin.com/ovafo/edit

.parent {
  position:  relative;
  width: 750px;
  height: 7150px;
  background: red;
  border: solid 1px #000;
  z-index: 1;
}
.child {
  position: absolute;
  background-color: blue;
  z-index: 0;
  color: white;
  top: 0;
}
.wrapper
{
  position: relative;
  background: green;
  z-index: 10;
}
<div class="wrapper">
  <div class="parent">
    parent parent
    <div class="child">
      child child child
    </div>
  </div>
</div>

【问题讨论】:

    标签: css z-index


    【解决方案1】:

    为孩子设置一个否定的z-index,并删除对父母的设置。

    .parent {
        position: relative;
        width: 350px;
        height: 150px;
        background: red;
        border: solid 1px #000;
    }
    .parent2 {
        position: relative;
        width: 350px;
        height: 40px;
        background: red;
        border: solid 1px #000;
    }
    .child {
        position: relative;
        background-color: blue;
        height: 200px;
    }
    .wrapper {
        position: relative;
        background: green;
        height: 350px;
    }
    <div class="wrapper">
        <div class="parent">parent 1 parent 1
            <div class="child">child child child</div>
        </div>
        <div class="parent2">parent 2 parent 2
        </div>
    </div>

    https://jsfiddle.net/uov5h84f/

    【讨论】:

    • 谢谢,但我不能在孩子上设置负 z-index,这会将其设置在我实际页面上的页面容器下方。这是唯一的方法吗?
    • 也许您可以通过将z-index设置为页面中的其他元素来进行试验,最终获得您希望它们显示的顺序。
    • 根据经验,不要在布局中使用 z-index。仅将它用于非常特定的场景,例如在这种情况下,您会发现它更易于管理。
    • @Toskan :如果您设置父级没有 z-index 但在您设置 z-index 的某处的 DOM 进一步向上的情况,或者您不介意子元素以堆叠顺序存在于 html 元素下方。学习;实验;此答案很容易解决您的问题,但您没有给它机会,因为您没有了解此答案中实际发生的情况,并期望它开箱即用。
    • 有一篇关于z-index的精彩文章:philipwalton.com/articles/what-no-one-told-you-about-z-index
    【解决方案2】:

    幸运的是,存在解决方案。您必须为父级添加一个包装器并将此包装器的 z-index 更改为例如 10,并将子级的 z-index 设置为 -1:

    .parent {
        position: relative;
        width: 750px;
        height: 7150px;
        background: red;
        border: solid 1px #000;
        z-index: initial;
    }
    
    .child {
        position: relative;
        background-color: blue;
        z-index: -1;
        color: white;
    }
    
    .wrapper {
        position: relative;
        background: green;
        z-index: 10;
    }
    <div class="wrapper">
        <div class="parent">parent parent
            <div class="child">child child child</div>
        </div>
    </div>

    【讨论】:

    【解决方案3】:

    其中一些答案确实有效,但设置 position: absolute;z-index: 10; 似乎非常强大,只是为了达到所需的效果。我发现以下是所需的全部,但不幸的是,我无法进一步减少它。

    HTML:

    <div class="wrapper">
        <div class="parent">
            <div class="child">
                ...
            </div>
        </div>
    </div>
    

    CSS:

    .wrapper {
        position: relative;
        z-index: 0;
    }
    
    .child {
        position: relative;
        z-index: -1;
    }
    

    我使用这种技术为图像链接实现了带边框的悬停效果。这里还有一些代码,但它使用上面的概念来显示图像顶部的边框。

    http://jsfiddle.net/g7nP5/

    【讨论】:

      【解决方案4】:

      您需要在父级和子级上都使用position:relativeposition:absolute 才能使用z-index

      【讨论】:

      • 谢谢,我更新了示例,似乎没有帮助。
      • 抱歉,我没有看到您要求的全部内容。您绝对不能将子元素设置在父元素后面。您只能对兄弟元素这样做。
      【解决方案5】:

      由于您的 div 是 position:absolute,因此就位置而言,它们并没有真正嵌套。在您的 jsbin 页面上,我将 HTML 中 div 的顺序切换为:

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

      红色的盒子覆盖了蓝色的盒子,我认为这就是你要找的。

      【讨论】:

        【解决方案6】:

        破解它。基本上,发生的情况是,当您将 z-index 设置为负数时,它实际上会忽略父元素,无论它是否定位,并位于下一个定位元素的后面,在您的情况下是您的主容器。因此,您必须将父元素放在另一个定位的 div 中,而您的子 div 将位于其后面。

        解决这个问题对我来说是一个救命稻草,因为我的父元素特别无法定位,以便我的代码工作。

        我发现所有这些对于实现此处指示的效果非常有用:Using only CSS, show div on hover over <a>

        【讨论】:

          【解决方案7】:

          风格:

          .parent{
            overflow:hidden;
            width:100px;
          }
          
          .child{
            width:200px;
          }
          

          正文:

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

          【讨论】:

          • 这不符合 OP 的要求
          • 甚至与问题无关
          猜你喜欢
          • 2020-08-11
          • 1970-01-01
          • 1970-01-01
          • 2020-10-06
          • 2017-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-18
          相关资源
          最近更新 更多