box-sizing: border-box
时间: 2023-09-11 18:04:25 浏览: 113
box-sizing: border-box is a CSS property that changes the way the browser calculates the size of an element. By default, when you set the width and height of an element, the browser calculates the size of that element based on its content and any padding or borders added to it.
However, when you use the box-sizing: border-box property, the browser includes the padding and border of an element in its total width and height, rather than adding them on top of the width and height. This can be useful for creating layouts where you want to have consistent spacing between elements, regardless of their padding or border size.
For example, if you have a div with a width of 100px and a border of 10px, the box-sizing property will ensure that the total width of the div is 100px, including the border. Without this property, the div would be 120px wide (100px + 10px border on each side).
Here's an example of using box-sizing: border-box in CSS:
```
div {
box-sizing: border-box;
width: 100px;
height: 100px;
padding: 10px;
border: 5px solid black;
}
```
In this example, the total size of the div will be 100px x 100px, including the padding and border.
阅读全文