1. Function
Define how to calculate the total width and total height of an element, whether the main settings need to add padding ( padding
) and borders, etc.
2. Attribute value
content-box
By default, the width of any border and inner border will be added to the width of the last drawn element
-
border-box
The set border and padding values are included in width
3. Examples
<body>
<div class="container"></div>
</body>
* {
margin: 0;
padding: 0;
}
body {
border: 1px solid rgb(192, 192, 235);
margin: 10px;
}
.container {
width: 100px;
height: 100px;
background: pink;
margin: 10px;
padding: 10px;
border: 1px solid black;
}
- content-box
* {
box-sizing: content-box;
}
At this time, the width of the box content area is the set width, and the actual width is:
width+padding*2+border*2+margin*2
- border-box
* {
box-sizing: border-box;
}
/* 此时的盒子比上面的盒子要小 */
As can be seen from the above figure
content + padding + border + margin =width
, the actual width of the box is the set width