【发布时间】:2021-11-26 06:19:17
【问题描述】:
要求
-
尽管
.Parent有侧边距,.Child必须填充 100% 的视口。 -
对于
.Child,.Parent的侧边距提前未知(假设.Child是一个可重复使用的组件,它不知道它使用的环境) - 滚动条宽度取决于浏览器,因此它的宽度事先未知,无法硬编码。
-
.Child的长度必须完全可见。
解决方案尝试
.Parent {
background: #BBDEFB;
padding: 0 20px;
}
.Child {
width: 100vw;
margin-left: calc(-0.5*(100vw - 100%));
/* Child does not know that `Parent's` paddings are 20px */
height: 40px;
background: #FF8F00;
border: 5px solid #283593;
}
.Dummy {
height: 1000px;
background: #E1BEE7;
}
<div class="Parent">
<div class="Child"></div>
<div class="Dummy"></div>
</div>
Child 的左右边框不可见。
我想原因是滚动条。
我们可以尊重滚动条吗?
【问题讨论】: