box-sizing 告诉浏览器如何计算一个元素的总宽度
和总高度
语法
box-sizing: content-box|border-box|inherit:
1、content-box: W3C标准盒模型
[扩展] 内容尺寸不变,总尺寸增加
total = content + border + padding
2、border-box: IE怪异盒模型
[压缩] 总尺寸不变,内容尺寸减少
content = total - border - padding;
计算示例
<style type="text/css"> * { margin: 0; padding: 0; } .box { width: 200px; height: 100px; padding: 20px; border: 10px solid red; background:yellow; } .content-box { box-sizing: content-box; } .border-box { box-sizing: border-box; } </style> <body> <div class="box">box</div> <div class="box content-box">content-box</div> <div class="box border-box">border-box</div> </body>
计算方式
1、box-sizing: content-box
Total width: 200px + (2 * 20px) + (2 * 10px) = 260px Total height: 100px + (2 * 20px) + (2 * 10px) = 160px Content box width: 200px Content box height: 100px
2、box-sizing: border-box
Total width: 200px Total height: 100px Content box width: 200px - (2 * 20px) - (2 * 10px) = 140px Content box height: 100px - (2 * 20px) - (2 * 10px) = 40px
参考