html - 匹配背景图像的高度以进行叠加

标签 html css

我有一个背景图像,其顶部是一个覆盖层,我希望覆盖层能够响应地匹配背景图像的高度,我能管理的最多就是匹配该部分的高度,有什么想法吗?

html

<section class="section">
    <div class="bg-img">
        <div class="overlay">
            <p>This is a text on the overlay</p>
        </div>
    </div>
</section>

CSS

.section {
  width: 100%;
  height: auto;
  min-height: auto;
}

.bg-img {
  background: url('/static/img.jpg');
  background-repeat: no-repeat;
  background-position: top center;
  background-size: 80%;

}

.overlay {
  width: 600px;
  height: 100%;
  background-color: rgb(0,0,0,0.07);
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}

最佳答案

赋予内部 div 绝对定位并使父 div 相对定位。

.section {
  width: 100%;
  height: auto;
  min-height: auto;
}

.bg-img {
     background: url(https://placekitten.com/640/360);
    background-repeat: no-repeat;
    background-position: top center;
    background-size: 80%;
    min-height: 50vh;
    position: relative;
    width: 80%;
    background-size: cover;

}

.overlay {
height: 100%;
    background-color: rgb(179 42 42 / 65%);
    margin-left: auto;
    margin-right: auto;
    text-align: center;
    position: absolute;
    width: 100%;

}
<section class="section">
    <div class="bg-img">
        <div class="overlay">
            <p>This is a text on the overlay</p>
        </div>
    </div>
</section>

关于html - 匹配背景图像的高度以进行叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72666515/

相关文章:

html - 添加额外的 div 有什么意义?

javascript - window.open 函数只在 foreach 循环中运行一次

jQuery忽略输入键直到文本区域中的最小长度,然后绑定(bind)提交

javascript - 如何将 div 容器放在另一个 div 容器内,在一个选项卡内

css - 我可以把省略号放在左边,句点放在右边吗?

php - WordPress 菜单帖子特色图片

javascript - 单击 HTML 按钮更改 CSS?

html - 如何使用 CSS 定位元素中的文本而不是嵌套元素中的文本

javascript - Angular Directive(指令)中的按钮不执行 ng-click 功能

jquery - 分区单选按钮