一、box-sizing目的
box-sizing 属性用来设置盒子模型的计算方式
二、box-sizing属性包含
content-box:
- box-sizing的
默认值 - 和margin没有任何关系,不要被干扰❌
- 占据浏览器中的面积是:css设置的height + padding + border
- 那么可使用的内容区域就是:css设置的height
- 🌟下图看出,内容区域就是 100px 即css里面设置的height值
.box {
box-sizing: content-box;
height: 100px;
border: 10px dashed red;
padding: 20px;
margin: 30px;
background-color: antiquewhite;
}
border-box:
- 和margin没有任何关系,不要被干扰❌
- 占据浏览器的面积是:css设置的height
- 那么可使用的内容区域就是:css设置的height- padding - border
- 🌟下图看出,内容区域就是 40px 即css里面设置的height值 - padding * 2 -border * 2
box {
box-sizing: border-box;
height: 100px;
border: 10px dashed blue;
padding: 20px;
margin: 30px;
background-color: antiquewhite;
}
如何获取占据浏览器实际面积?
可以用offestHeight 获取
var wrapper = document.getElementById('box');
console.log(`offsetHeight = ${box.offsetHeight}`)