html - 如何使用纯 CSS 覆盖以填充可滚动的父级?

标签 html css overlay

这是一个简单的例子:

<div class = "has-scrollbar">
    <div class = "long"></div>
    <div class = "overlay"></div>
</div>

.has-scrollbar {
    width: 200px;
    height: 100px;
    overflow-y: scroll;
    position: relative;
}

.long {
    height: 200px;
    width: 50px;
    background: blue;
}

.overlay {
    width: 100%;
    height: 100%;
    background: red;
    opacity: 0.5;
    position: absolute;
    top: 0;
}

JsFiddle

红色覆盖层应完全填满父容器。 .long 的高度是事先不知道的。 .has-scrollbar div 应该仍然可以滚动(并且不被覆盖)。

任何在 .overlay 上使用 position: fixed 的解决方案都不太可能奏效。现实世界的情况要复杂得多。考虑 .has-scrollbarbody 中的位置也不能提前知道。

最佳答案

所以你不知道 long,但看起来你确实控制了 has-scrollbar,所以你可以使 overlay 固定和将其放置在与 has-scrollbar 相同的位置:

.overlay {
    height: 100px;
    margin-top: 8px; /* just to compensate for body margin in the example */
    pointer-events: none; /* mouse events will pass through */
    position: fixed;
    width: 200px;
}

Updated JSFiddle.

关于html - 如何使用纯 CSS 覆盖以填充可滚动的父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026337/

相关文章:

javascript - 每当您在选择器内移动鼠标时,悬停功能都会重复

javascript - 悬停时更改链接/按钮的背景颜色

html - 叠加背景颜色和图像

php - 提交表单后如何获得弹出框?

html - 网络字体仅适用于 Mac 的 Chrome 和 Safari,

css - IE7 float 列表元素格式问题

android - 如何在图库上叠加 ImageView ?

jquery - 全页叠加拖放 jQuery

html - Java Web 小服务程序 : writing plain text on an existing html template file

javascript - 在 HTML 中显示我的 JSON