html - 优先考虑两个绝对元素

标签 html css absolute

我有两个重叠的绝对元素。我想让一个显示在另一个之上。在下面的示例中,我希望带有文本的粉红色图形显示在使用 ::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>

最佳答案

要使 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>

关于html - 优先考虑两个绝对元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50786898/

相关文章:

html - Flexbox 和溢出 :scroll

html - 是否可以使用 CSS 在图像 map 上设置鼠标悬停样式?

内联 block 中的绝对定位元素

html - 嵌套div和绝对位置问题

css - 绝对定位对语音激活软件的影响

jquery - Div 从绝对定位更改为固定定位 - 发生不需要的移动

css - 需要帮助使用 CSS 定位背景图像

html - 非 float div 的内部文本相对于前一个 float div 定位

javascript - jQuery - 调整屏幕大小时 div 不关闭

javascript - HTML5 中的同步视频采样