Use box-sizing: border-box when you want an element’s declared width and height to include its content, padding, and border. Padding and borders then fit inside the dimensions you set, while margin remains outside.
What changes between content-box and border-box?
CSS uses content-box as the initial value. With that model, a declared width measures the content area; padding and borders are added outside it. With border-box, the declared width measures the combined content, padding, and border. Margin is outside the measured box in both models.
| Model | What declared width measures | Effect of padding and border |
|---|---|---|
content-box |
Content area | They increase the rendered width beyond the declared width. |
border-box |
Content, padding, and border together | They use space within the declared width, leaving less room for content. |
For example, MDN’s box-sizing example sets an element’s width to 350px and gives it a 10px border. With content-box, the rendered width is 370px before any margin is added. With border-box, the total width stays 350px; the content area becomes narrower to make room for the border and any padding.
Why does adding padding make an element wider?
If the element uses content-box, its declared width applies only to its content. Padding and border are added outside that width, so the element’s rendered size grows. Switching to border-box keeps those parts within the declared dimension, which is useful when an element must fit a fixed column or component width.
#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
For instance, four elements each set to width: 25% can exceed the row’s available width under content-box when they also have padding or borders. With border-box, those additions are included in each element’s 25% width.
How to apply border-box broadly
MDN documents this inheritance pattern for setting the value on the root element and letting elements and pseudo-elements inherit it:
Rank #2
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
To check why an element’s size differs from what you expect, inspect it in your browser’s developer tools and look at the box model view. It shows the content, padding, border, and margin areas separately, as described in MDN’s box model guide.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.When border-box may not be the right choice
border-box does not remove padding or borders. They consume part of the declared dimensions, so the content area shrinks. The content box cannot become negative; MDN notes that it is floored at zero.
Rank #3
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Choose content-box when you want width and height to describe the content independently of padding and borders. MDN notes this can be useful for some relatively or absolutely positioned elements when positioning values should remain independent of changes to padding and border.
The distinction matters for elements whose sizing is controlled by width or height. Ordinary inline boxes do not respect those dimensions in the same way as block boxes, because box-model behavior depends on display type.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




