【问题标题】:Why does z-index not work with display: flex; and a negative margin? [duplicate]为什么 z-index 不适用于 display: flex;和负边距? [复制]
【发布时间】:2019-12-04 09:38:19
【问题描述】:

我正在尝试在横幅图像上覆盖一些面包屑。我没有使用绝对定位或类似的解决方案,而是使用负边距。

由于某种原因,横幅文本的边缘覆盖了面包屑,这意味着它们无法被点击或选择。

我的假设是这是一个简单的 z-index 问题,但我无法让 z-index 工作。无论我把它放在哪里或我将它设置为什么值,问题都仍然存在。据我所知,它只发生在display: flex

这是问题的简化版本:

https://codepen.io/anon/pen/jgMGYj

.small-text
{
  margin-bottom: -20px;
}

.small-text span
{
    background-color: orange;
}

.big-container
{
    display: flex;
    align-items: center;
    background-color: purple;
}

.big-text
{
  display: flex;
  flex-direction: column;
  align-items: center;
}

.big-text span
{
  margin-top: 50px;
  background-color: lime;
}
<div class="small-text">
  <span>I want to select/highlight this.</span>
</div>
<div class="big-container">
  <div class="big-text">
    <span>But this element's margin is covering it up.</span>
  </div>
</div>

这是什么原因?是否有一些我不理解的 z-index 规则?它是一个弹性错误吗?是否可以在不删除display: flex的情况下修复它?

【问题讨论】:

    标签: html css flexbox margin


    【解决方案1】:

    您需要使用position: relative 来制作z-index behave like you want it to

    .small-text {
      margin-bottom: -20px;
      position: relative;
      z-index: 1;
    }
    
    .small-text span {
      background-color: orange;
    }
    
    .big-container {
      display: flex;
      align-items: center;
      background-color: purple;
    }
    
    .big-text {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    
    .big-text span {
      margin-top: 50px;
      background-color: lime;
    }
    <div class="small-text">
      <span>I want to select/highlight this.</span>
    </div>
    <div class="big-container">
      <div class="big-text">
        <span>But this element's margin is covering it up.</span>
      </div>
    </div>

    【讨论】:

    • 我考虑过这一点,但我试图避免它,因为它会阻止您与big-text 的内容进行交互。但是想一想,您可以将pointer-events: auto 添加到big-text span 以抵消这种情况。
    • 下面是一个实际的例子:codepen.io/anon/pen/zgKEWK 到目前为止,这似乎是最好的解决方案,但难道不能让小文本真正与大文本重叠吗?我不明白那里发生了什么。
    • 同意,不过可能z-index也可以用。我已经编辑了代码,请检查一下。
    • 谢谢,看来position: relative 可以解决问题。
    • 是的,值得一提的是,z-index 需要 position: relative 并且不适用于默认的 position: static
    【解决方案2】:

    试试下面的代码: 每次都使用带有 z-index 的位置。

    .small-text {
      margin-bottom: -20px;
      position: relative;
      z-index: 1;
    }
    

    【讨论】:

    • 看起来position: relative 是我需要的。谢谢!
    • 如果 z-index 不起作用,那么使用 position 属性也可以完美地工作。谢谢。
    【解决方案3】:
    .small-text
    {
      margin-bottom: 0;
    }
    
    .big-text span
    {
      margin-top: 30px;
      background-color: lime;
    }
    

    我想我明白你在问什么,如果你从小文本中删除 -20px 边距并将 margin-top 减少到 30px,你会得到相同的定位,你可以选择橙色文本。

    【讨论】:

    • 这将完全消除重叠。请记住,我们的目标是让小文本及其容器位于大文本及其容器之上,因为它将是覆盖横幅图像的面包屑。
    • 好吧,我显然没看懂这个问题,我以为你试图选择文本并将它们放在同一个地方,我的错
    • 不用担心,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多