How do box-sizing rules affect an element’s final width?
Quick answer
With content-box, the declared width is the content width, so horizontal padding and borders increase the rendered width. With border-box, the declared width includes the content, padding, and borders. [S1] For a 300px width with 20px padding and 2px borders on each side, that gives 344px under content-box and 300px under border-box, before margins.
The question, the details, the next step.
A practical checklist
Find the element’s
box-sizingvalue.Record its declared width.
Add the left and right padding and border widths.
For
content-box, add those side values to the declared width.For
border-box, use the declared width as the width including padding and borders.Keep margins separate from this calculation.
A few more useful answers
Does `border-box` include margins in the declared width?
No. The declared width under border-box includes content, padding, and borders, but not margins. [S1]
Why does `content-box` make the rendered width larger than `width`?
Because width applies to the content, while horizontal padding and borders are added to get the width including borders. [S1]
How do I calculate the content width with `border-box`?
Subtract the left and right padding and border widths from the declared width. For example, a 300px width with 20px padding and 2px borders on both sides leaves 256px for content, assuming those values on each side.
Sources
References used for this guide.
- [S1] box-sizing CSS property - CSS | MDN developer.mozilla.org · Checked
