盒模型重置
重置盒模型,使width和height不受border或padding的影响。
- 使用
box-sizing: border-box,在计算元素的width和height时包括padding和border的宽度和高度。 - 使用
box-sizing: inherit将box-sizing属性从父元素传递给子元素。
<div class="box">border-box</div>
<div class="box content-box">content-box</div>
div {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
.box {
display: inline-block;
width: 120px;
height: 120px;
padding: 8px;
margin: 8px;
background: #F24333;
color: white;
border: 1px solid #BA1B1D;
border-radius: 4px;
}
.content-box {
box-sizing: content-box;
}