Sub topic 6
box-sizing: content-box vs border-box
This chapter assumes you can write CSS rules and know the four box layers: content, padding, border, and margin. It also assumes you are comfortable with px, rem, and % units. Here you will learn how box-sizing changes what width and height mean, and why most projects set it globally.
What `width` measures by default
The box-sizing property decides which box edge the width and height properties describe. The default value is content-box. With it, width sets only the content area. Padding and border are added outside that width.
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}Under content-box, the horizontal sizes add up like this:
| Part | Calculation | Result |
|---|---|---|
| Content width | `width` | 300px |
| Padding | 20px left + 20px right | 40px |
| Border | 5px left + 5px right | 10px |
| Border box width | 300 + 40 + 10 | 350px |
| Space occupied with margins | 350 + 10 + 10 | 370px |
The element is 350px wide on screen, even though the CSS says width: 300px. The same arithmetic applies to height.
How `border-box` changes the calculation
With box-sizing: border-box, width and height describe the border box. Padding and border are carved out of the stated size, so the content area shrinks to fit.
*,
*::before,
*::after {
box-sizing: border-box;
}
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
}The card's border box is now 300px wide. The content area is 300 − 40 − 10 = 250px. Margin is never included in either model. It always sits outside the box.
Why border-box is easier to work with
Under content-box, changing the padding changes the element's total size. That makes percentage layouts fragile. Consider two columns:
.column {
width: 50%;
padding: 1rem;
}With content-box, each column is 50% plus the padding, so the pair is wider than its parent and wraps or overflows. With border-box, each column is exactly 50% of the parent, whatever the padding.
Most stylesheets apply border-box to every element, as in the earlier example. The ::before and ::after selectors are included because * does not match generated content.
Common errors
- Forgetting that
heightfollows the same rule. Withborder-box, aheight: 100pxelement with 20px of vertical padding has 60px of content height. - Expecting margin to count. Neither value includes margin in
width. - Padding larger than the width. Under
border-box, the content area cannot go below zero. If padding and border exceedwidth, the box grows to fit them, so the declared width is not honoured. - Mixing models. Form controls such as some
inputelements may default toborder-boxin certain browsers. Setting the value globally removes that inconsistency. - Setting the reset after other rules and relying on specificity. The universal selector has the lowest specificity, so any rule that sets
box-sizingon a class will override it. Place the reset at the top of the stylesheet.
Quick check
Exercise
An element has width: 400px; padding: 10px 30px; border: 4px solid; and no margin. What is its border box width, and what is its content width, under content-box and under border-box?
Show answer
Under content-box, the content width is 400px and the border box width is 400 + 60 + 8 = 468px. Under border-box, the border box width is 400px and the content width is 400 − 60 − 8 = 332px.
Exercise
You set box-sizing: border-box on body, but a div inside it still grows beyond its declared width when you add padding. Why, and what is the fix?
Show answer
box-sizing is not inherited, so the div still uses the default content-box. Apply it with *, *::before, *::after { box-sizing: border-box; }, or set box-sizing: inherit on all elements after setting it on html.