盒模型有两种,W3C 标准模型和 IE 盒子模型。

CSS3 开始引入 box-sizing 属性,它包含 3 个可选值。

W3C 标准模型

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8fe06cd4-1ee6-4692-a92c-722b38bf05f2/Untitled.png

box-sizing: content-box;

/* 这是默认样式指定 CSS 标准。测量 width 和 height 属性只包括的内容,
 * 不含 border, margin, padding。 */

以 width 计算方式为例,W3C 标准模型的宽度计算公式:

W (W3C) = W (内容)

IE 盒子模型(怪异盒模型)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/337a3e5c-cffe-4a1a-b6e7-c53139913ac7/Untitled.png

box-sizing: border-box;

/* width 和 height 属性包括 padding 和 border,不含 margin。 */

以 width 计算方式为例,IE 盒子模型的宽度计算公式:

W (IE) = W (内容) + W (padding) + W (border)