【问题标题】:HTML/CSS How to create an overlay that covers a certain container without using "position:absolute;"?HTML/CSS 如何在不使用“position:absolute;”的情况下创建覆盖某个容器的叠加层?
【发布时间】:2019-12-11 21:46:14
【问题描述】:

我需要使用 HTML/CSS 创建一个覆盖元素,它只覆盖某个容器,而不是整个页面。我可以使用position:absolute;,然后在任何地方使用0px,但问题是我无法定位应该覆盖的容器,它必须保持静止,然后position:absolute; 将与此容器无关。

所以我尝试将此设置的高度和宽度修复为 100%:

body {
  background-color: grey;
}

#ovl {
  height: 100%;
  width: 100%;
  background-color: #a10000a1;
}
#container {
  border: 4px dotted blue;
}           
<!DOCTYPE html>
<html>
    <body>
        <div id="container">
            <div id="ovl">
                This Overlay should hide the images.
            </div>
            <img height=300 src="1.png" class="imgs" alt="Error!">
            <img height=300 src="2.png" class="imgs" alt="Error!">
            <img height=300 src="3.png" class="imgs" alt="Error!">
        </div>
    </body>
</html>

我希望红色覆盖元素会覆盖整个容器和其中的三个图像,但我得到了this。这三个图像(乐谱)没有被红色覆盖层覆盖。我的代码有错误吗?如何解决?

【问题讨论】:

  • 那么,叠加层在嵌套的 div 之外?
  • display:grid 如果您将图像覆盖的相同区域分配给叠加层,则可以使用。请参阅下面的答案。
  • @AnuragSrivastava 不幸的是,这并没有解决我的问题。
  • @AxelAnaya 叠加层位于容器 div 内,但如有必要,我可以更改它。

标签: html css layout overlay


【解决方案1】:

进行叠加的最佳方法是在子级中使用position:absolute,在父级中使用position:relative,这将是您的CSS 代码

body {
  background-color: grey;
}

#ovl {
  background-color: #a10000a1;
  position: absolute;
  width: 100%;
  height: 100%;
}
#container {
  border: 4px dotted blue;
  position: relative;
}                 

查看结果here

没有绝对/相对位置的所有其他解决方案可能非常麻烦

【讨论】:

  • 操作不想使用绝对定位,没有它只有网格可以做到这一点:)
【解决方案2】:

根据我对问题的理解,您的 Div 有错误。试试这个:

<!DOCTYPE html>
<html>
    <body>
      <div id="ovl">
        <div id="container">

                This Overlay should hide the images.
            </div>
            <img height=300 src="1.png" class="imgs" alt="Error!">
            <img height=300 src="2.png" class="imgs" alt="Error!">
            <img height=300 src="3.png" class="imgs" alt="Error!">

        </div>
    </body>
</html>

CSS

body {
  background-color: grey;
}

#ovl {
  height: 100%;
  width: 100%;
  background-color: #a10000a1;
}
#container {
  border: 4px dotted blue;
}           

【讨论】:

  • 由于您只是交换了ID,因此您编写的代码会导致图像容器为红色,并且叠加层带有蓝色边框,这不是我的意图。我的意图是让覆盖层填满整个容器,见上文。
【解决方案3】:

CSS Grid 可以做到这一点。无需定位。

body {
  background-color: grey;
}

#ovl {
  background-color: #a10000a1;
  grid-column: 1 / -1;
  grid-row: 1;
  z-index: 2;
  color: white;
}

#container {
  border: 4px dotted blue;
  display: inline-grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto;
}

img {
  grid-row: 1;
  max-height: 100%;
  width: auto;
}

img:nth-of-type(1) {
  grid-column: 1;
}

img:nth-of-type(2) {
  grid-column: 2;
}

img:nth-of-type(3) {
  grid-column: 3;
}
<div id="container">
  <div id="ovl">
    This Overlay should hide the images.
  </div>
  <img height=300 src="http://www.fillmurray.com/g/140/100" class="imgs" alt="Error!">
  <img height=300 src="http://www.fillmurray.com/g/140/100" class="imgs" alt="Error!">
  <img height=300 src="http://www.fillmurray.com/g/140/100" class="imgs" alt="Error!">
</div>

【讨论】:

    【解决方案4】:

    这是一个使用 CSS Grid 的解决方案:

    .grid{
      display:grid;
      grid-template-columns: 1fr 1fr 1fr;
      grid-template-rows: 1fr 1fr 1fr;
    /*   grid-template-row: 1fr; */
      border: 3px dotted blue;
      width: 100%;
    }
    .overlay{
      background: #000000a1;
      color: white;
      grid-column: 1 / span 3;
      grid-row: 1/ span 3;
      z-index: 1;
    }
    .img1{
      grid-column: 1 / span 2;
      grid-row: 1/ span 1;
    }
    .img2{
      grid-column: 1 / span 2;
      grid-row: 2/ span 2;
    }
    .img3{
      grid-column: 1 / span 2;
      grid-row: 3/ span 3;
    }
              
    <div class="grid">
      <div class="overlay">
        This Overlay should hide the images.
      </div>
      <img src="1.png" class="img1" alt="Error!">
      <img src="2.png" class="img2" alt="Error!">
      <img src="3.png" class="img3" alt="Error!">
    </div>

    如果你想尝试和实验,这是我的CodePen

    【讨论】:

    • 不是你回答我应该使用“位置:相对;”对于容器,但随后删除了这个答案?但是,我想省略使用 position:absolute; 覆盖的原因是因为我不知道如何在不改变其实际位置的情况下定位容器。但是,如果 position:relative; 没有指定 top/bottom/... 时不改变位置,我可以使用它。没有任何其他规范的相对定位是否会改变位置?
    • 是的,我删除了我的答案,因为您说过要避免使用position,但是,如果您relative 定位顶部/底部...,position: relative 将布局一个元素相对于自身。换句话说,元素以正常流程布局,然后从正常流程中移除并偏移您指定的任何值参见this
    • 那么写我希望避免position是我的错。因此,position 容器relative 和覆盖absolute 以及无处不在的0px... 是一个很好的解决方案?这将绝对不会改变除叠加层之外的任何元素的位置?
    • 如果你想覆盖容器中的所有东西,你应该使用 100% 的高度和宽度,因为当你在标签中使用绝对位置时,它将忽略其亲属的所有位置。但保持其第一个位置相对父级的位置,这就是为什么你需要在容器中使用位置相对
    猜你喜欢
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 2012-05-15
    • 2021-04-14
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多