我需要我的子 div 坚持父 div 的底部,因此 position: absolute
和 bottom: 0px
。我还需要子宽度与父宽度相同,我认为 box-sizing: border-box
可以工作,但由于填充和边距,子宽度仍然向右突出。
我该如何解决?
.parent {
height: 500px;
min-width: 500px;
position: relative;
background-color: red;
}
.child {
padding: 10px 10px 10px 10px;
margin: 10px 10px;
background-color: grey;
bottom: 0px;
box-sizing: border-box;
position: absolute;
width: 100%;
}
<div class="parent">
<div class="child"></div>
</div>
原文由 user3642365 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用
left:0
/right:0
而不是width:100%
来避免溢出,因为实际上你的元素在 500pxx+ parent 的 margin 20 内占用元素。作为旁注,
box-sizing: border-box
工作完美,但 在 元素内部,因此填充包含在宽度中,但边距当然不包含: