Content Box vs Border Box
You'll learn how the two CSS box-sizing models calculate an element's total size differently—and why that matters for layout control.
The Core Difference
When you set width: 300px on an element, what does that 300px actually measure? The answer depends on the box-sizing model:
content-box(default): The width/height applies only to the content area. Padding and border are added on top, making the element larger than expected.border-box: The width/height includes content, padding, and border. The content shrinks to fit, but the total size stays exactly what you specified.
A Practical Analogy
Imagine ordering a 12-inch pizza box:
- Content-box: The 12 inches measures only the pizza itself. The box, napkins, and packaging add extra size—your delivery is bigger than 12 inches.
- Border-box: The entire package is guaranteed to be 12 inches. The pizza inside adjusts to make room for the box thickness and extras.
When to Use Each
Most developers prefer border-box for predictable layouts. If you say an element is 300px wide, it's actually 300px total—no math required.
/* Common reset: apply border-box everywhere */
* {
box-sizing: border-box;
}
With content-box (the default), you'd need to manually calculate: total width = width + padding-left + padding-right + border-left + border-right. This gets messy fast.
Border-box makes responsive design and grid layouts much easier because dimensions remain consistent regardless of padding or borders.
Key Takeaway: Use box-sizing: border-box to make width and height include padding and border, giving you predictable, easier-to-manage layouts.