CSS Box Sizing
The box-sizing property decides what width and height actually measure. It sounds like a detail, but it changes how almost every layout calculation works — which is why nearly every modern stylesheet sets it on the first line.
The two models
| Value | width/height measure… |
Padding & border |
|---|---|---|
content-box (default) |
Only the content area | Added on top of the width |
border-box |
Content + padding + border | Included inside the width |
With content-box, a box with width: 200px; padding: 20px; border: 5px is actually 250px wide. With border-box it's exactly 200px, and the content area shrinks to 150px.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.ruler { width: 200px; height: 6px; background: #212121; margin: 10px 0 4px; }
.box { width: 200px; padding: 20px; border: 5px solid; }
.content { box-sizing: content-box; background: #ffcdd2; border-color: #c62828; }
.border { box-sizing: border-box; background: #c8e6c9; border-color: #2e7d32; }
</style>
</head>
<body>
<div class="ruler"></div>
<div class="box content">content-box: 250px total</div>
<div class="ruler"></div>
<div class="box border">border-box: 200px total</div>
</body>
</html>The black bars are exactly 200px. Only the green box matches.
Why border-box is easier
Imagine two columns, each width: 50%, with some padding. With content-box they add up to more than 100% and the second one drops to a new line. With border-box they fit perfectly.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; margin: 0; padding: 8px; }
.row { border: 2px dashed #9e9e9e; margin-bottom: 12px; }
.col { float: left; width: 50%; padding: 16px; color: white; }
.row::after { content: ""; display: block; clear: both; }
.a { background: #5c6bc0; }
.b { background: #ec407a; }
.fixed .col { box-sizing: border-box; }
</style>
</head>
<body>
<p>content-box — 50% + padding is too wide:</p>
<div class="row"><div class="col a">50%</div><div class="col b">50%</div></div>
<p>border-box — fits side by side:</p>
<div class="row fixed"><div class="col a">50%</div><div class="col b">50%</div></div>
</body>
</html>Applying it everywhere
The standard reset makes every element, including pseudo-elements, use border-box:
*,
*::before,
*::after {
box-sizing: border-box;
}Some developers prefer an inheritable version, so a third-party widget can opt back into content-box for its whole subtree:
html { box-sizing: border-box; }
*, *::before, *::after { box-sizing: inherit; }Tip: Add the reset at the very top of your stylesheet before writing any layout. Switching models halfway through a project changes the size of many elements at once.
Form elements
Form controls are inconsistent: many browsers already treat buttons as border-box but text inputs as content-box. That's why an <input> and a <button> with the same width often don't line up. The reset fixes it.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 30px; }
form { width: 220px; }
input, button { display: block; width: 100%; padding: 10px; margin-bottom: 8px; font: inherit; border: 2px solid #607d8b; border-radius: 6px; }
button { background: #607d8b; color: white; }
.reset * { box-sizing: border-box; }
form { outline: 2px dashed #ffa000; }
</style>
</head>
<body>
<form><b>Without reset</b><input placeholder="Email"><button>Send</button></form>
<form class="reset"><b>With reset</b><input placeholder="Email"><button>Send</button></form>
</body>
</html>Without the reset the input pokes out of the dashed form outline; with it, input and button are the same width.
What box-sizing does not change
- Margins are never included — they're always outside the box.
- If padding + border are larger than the width, the box grows anyway (the content can't be negative).
- Flexbox and Grid still respect
box-sizingwhen they calculateflex-basisand track sizes.
Test yourself
With box-sizing: border-box, width: 300px; padding: 20px; border: 10px solid; — how wide is the box?
- 300px
- 340px
- 360px
- 260px
What is the default value of box-sizing for a div?
- border-box
- content-box
- padding-box
- auto
Does border-box include margins in the width?
- Yes
- No
- Only vertical margins
- Only negative margins