【问题标题】:Prioritising two absolute elements优先考虑两个绝对元素
【发布时间】:2018-06-11 02:46:44
【问题描述】:

我有两个绝对元素,它们重叠。我希望有一个显示在另一个之上。在下面的示例中,我希望带有文本的粉红色图形显示在使用 ::before 创建的伪元素上方。
我试过z-index,但似乎没有帮助。 有什么想法吗?

.navbar {
  display: flex;
}
.lightbox {
  display: none;
  margin: auto;
}
.links {
  flex: 1;
  text-align: center;
  display: block;
}

.lightbox:target {
  display: flex;
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  color: black;
  left: 0;
  align-items: center;
  justify-content: center;
}
#close_button {
  position: relative;
  display: block;
}

.lightbox #close_button::after {
  content: "x";
  color: white;
  position: absolute;
  align-items: center;
  justify-content: center;
  right: -1rem;
  top: -1rem;
  z-index: 2;
  width: 2rem;
  height: 2rem;
  background: black;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
}
figcaption {
  z-index: 1000;
  background: lightpink;
  width: 25rem;
  text-align: center;
}

#close_button::before {
  top: 0;
  left: 0;
  z-index: 1;
  background-color: rgba(0,0,0,.7);
  height: 100%;
  width: 100%;
  content: "";
  border: solid red;
  position: fixed;
}
    <div style="height: 200px; background: lightgreen;">
    <nav class="navbar">
      <a class="links" href="#text1">Open example 1</a>
      <a class="links" href="#text2">Open example 2</a>
    </nav>
    <br><br>
    <div class="lightbox" id="text1">
      <figure>
        <a href="#" id="close_button"></a>
        <figcaption>
          Ipsum lorem ...
        </figcaption>
      </figure>
    </div>
    <div class="lightbox" id="text2">
      <figure>
        <a href="#" id="close_button"></a>
        <figcaption>
          Some latin text in textbox2 ...
        </figcaption>
      </figure>
      </div>
      
    </div>

【问题讨论】:

    标签: html css absolute


    【解决方案1】:

    要使z-index 起作用,元素的位置必须不是static。在您的情况下,元素需要有 position: relative

    .navbar {
      display: flex;
    }
    .lightbox {
      display: none;
      margin: auto;
    }
    .links {
      flex: 1;
      text-align: center;
      display: block;
    }
    
    .lightbox:target {
      display: flex;
      position: absolute;
      top: 0;
      width: 100%;
      height: 100%;
      color: black;
      left: 0;
      align-items: center;
      justify-content: center;
    }
    #close_button {
      position: relative;
      display: block;
    }
    
    .lightbox #close_button::after {
      content: "x";
      color: white;
      position: absolute;
      align-items: center;
      justify-content: center;
      right: -1rem;
      top: -1rem;
      z-index: 2;
      width: 2rem;
      height: 2rem;
      background: black;
      border-radius: 50%;
      cursor: pointer;
      display: flex;
    }
    figcaption {
      z-index: 1000;
      background: lightpink;
      width: 25rem;
      text-align: center;
      position: relative;
    }
    
    #close_button::before {
      top: 0;
      left: 0;
      z-index: 1;
      background-color: rgba(0,0,0,.7);
      height: 100%;
      width: 100%;
      content: "";
      border: solid red;
      position: fixed;
    }
    I have following example: https://codepen.io/elenaoat/pen/NzdVda?editors=1100
    
    When you click on the buttons you will see some text popping up. I cannot figure out how to get the text in the figcaption to be "above" the pseudo-element created with before. Right now the pseudo-element covers the whole window, which is fine, but I want the pink div with the text to show above it, so it's not greyed out.
    
        <div style="height: 200px; background: lightgreen;">
        <nav class="navbar">
          <a class="links" href="#text1">Open example 1</a>
          <a class="links" href="#text2">Open example 2</a>
        </nav>
        <br><br>
        <div class="lightbox" id="text1">
          <figure>
            <a href="#" id="close_button"></a>
            <figcaption>
              Ipsum lorem ...
            </figcaption>
          </figure>
        </div>
        <div class="lightbox" id="text2">
          <figure>
            <a href="#" id="close_button"></a>
            <figcaption>
              Some latin text in textbox2 ...
            </figcaption>
          </figure>
          </div>
          
        </div>

    【讨论】:

    • 确实如此。但是为什么它需要一个相对位置呢?我试图从逻辑上理解它。
    • top、right、bottom、left 和 z-index 属性对 position static 没有影响,它是元素的默认值。当 position 设置为 relative 时,它​​会在 z-index 的值不是 auto 时创建一个新的堆叠上下文。
    【解决方案2】:

    添加位置:相对于 figcaption 并且还更改了 after 元素和 figcaption 上的 z-index。

    z-index 属性仅适用于固定、绝对或相对 div。所以这就是为什么你需要在 figcaption 上的相对位置,甚至 z-index:2 在这种情况下也可以工作。不需要 1000 的值。同样,您只需要为关闭后图标设置一个更高的值。所以 zindex 3 也可以工作。

    .navbar {
      display: flex;
    }
    .lightbox {
      display: none;
      margin: auto;
    }
    .links {
      flex: 1;
      text-align: center;
      display: block;
    }
    
    .lightbox:target {
      display: flex;
      position: absolute;
      top: 0;
      width: 100%;
      height: 100%;
      color: black;
      left: 0;
      align-items: center;
      justify-content: center;
    }
    #close_button {
      position: relative;
      display: block;
    }
    
    .lightbox #close_button::after {
      content: "x";
      color: white;
      position: absolute;
      align-items: center;
      justify-content: center;
      right: -1rem;
      top: -1rem;
      z-index: 3;
      width: 2rem;
      height: 2rem;
      background: black;
      border-radius: 50%;
      cursor: pointer;
      display: flex;
    }
    figcaption {
      position: relative;
      z-index: 2;
      background: lightpink;
      width: 25rem;
      text-align: center;
    }
    
    #close_button::before {
      top: 0;
      left: 0;
      z-index: 1;
      background-color: rgba(0,0,0,.7);
      height: 100%;
      width: 100%;
      content: "";
      border: solid red;
      position: fixed;
    }
        <div style="height: 200px; background: lightgreen;">
        <nav class="navbar">
          <a class="links" href="#text1">Open example 1</a>
          <a class="links" href="#text2">Open example 2</a>
        </nav>
        <br><br>
        <div class="lightbox" id="text1">
          <figure>
            <a href="#" id="close_button"></a>
            <figcaption>
              Ipsum lorem ...
            </figcaption>
          </figure>
        </div>
        <div class="lightbox" id="text2">
          <figure>
            <a href="#" id="close_button"></a>
            <figcaption>
              Some latin text in textbox2 ...
            </figcaption>
          </figure>
          </div>
          
        </div>

    【讨论】:

      【解决方案3】:

      尝试将 figcaption 位置设置为相对。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-16
        • 2010-10-27
        • 1970-01-01
        相关资源
        最近更新 更多