【发布时间】:2021-08-06 14:28:26
【问题描述】:
我的水平条在左右两边都有一些边距。 我想在栏后面添加叠加层并希望拉伸到全屏宽度(就像模态对话框叠加层一样)但叠加层的顶部位置应该与栏的顶部位置相同,并且覆盖层的底部位置应该与栏的底部位置相同. 但是,由于左右间距,我无法将其拉伸到全屏宽度。所以我尝试添加等于左/右边距值的负左/右位置以使其全屏显示。 然而,这个不合适的解决方案似乎是合适的,因为左/右的边距将来可以是任何值,并且不是硬编码的。 有没有理想的通用解决方案。
下面是sn-p的代码:
<div class="parent">
<div class="bar"></div>
<div class="overlay"></div>
</div>
.parent {
position: relative;
margin-left: 50px; --> Margins can be any value not just hardcoded
margin-right: 50px;
}
.bar {
height: 50px;
background: blue;
}
.overlay {
z-index: -1;
position: absolute;
background: purple;
left: -50px; --> Set negative position to stretch it to fullscreen but not seems to be proper solution as margin can be of any value
top: 0;
right: -50px;
bottom: 0;
}
【问题讨论】:
标签: html css frontend padding margin