我有一个背景图像,上面是一个覆盖图,我希望覆盖图与背景图像的高度相匹配,我能管理的最多的是匹配部分的高度,有什么 idea 吗?

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相关问答推荐

Chrome中是否有一个本地显示密码功能,用于`input type = password/`?""<"">

有没有可能一个按钮,有焦点,然后再次点击它清除它的焦点,只是使用css?

有没有可能设置div的边框宽度相对于其父宽度和高度?'

UseEffect()从不调用preact

如何使我的组织 struct 图的连接线响应?

用于删除页面页眉上的超链接的CSS

在窄屏幕上显示表格,每个单元格占一行

为什么前端框架可以创建自定义属性,而我却被期望在挖掘之前添加数据

使用响应迅速的网格系统,将两列保持在同一行,同时允许更多列用于更大的屏幕

带有下拉菜单的完整css导航-链接不起作用?

标题在实时网站上闪烁

为什么我的图像在悬停时与固定顶部栏重叠?

四开将一个 qmd 文档中的单词链接到另一个 qmd 文档中的单词

当作为常规图片输入时,Cloudfront分布式图像可显示,但作为背景图像不显示

如何让一个 div 始终贴在容器的边缘?

从垫分页器中删除输入边框

修复 Vue 3 Component 中的 CSS 以显示后面的 Select 器样式而无需 !important

重点/主动输入的概述问题

使用 rgba() 的 css 中的边框不透明度不起作用

以 HTML 格式显示的 LaTeX 表格