CSS / Reference / box-sizing: Content and Border Boxes, Including Small-Size Limits
The CSS box-sizing property determines what width and height measure. With content-box, they measure the content area. With border-box, they include padding and borders. Margins sit outside both boxes.
What the declared width measures
Add the values on both sides of the relevant axis. With padding: 20px, for example, the left and right padding contribute 40px to the width. Use the top and bottom values when calculating height.
| Value or property | Effect on the size you measure |
|---|---|
content-box |
The declared width measures the content. Add left and right padding and borders to get the border-box width. |
border-box |
Left and right padding and borders fit inside the declared width. The remaining width goes to the content, which has a minimum width of zero in this calculation. |
margin |
Sets space outside the box. Neither box-sizing mode includes it. |
The W3C box-sizing specification defines which box receives length and percentage sizes.
The initial value is content-box and the property is non-inherited. Set box-sizing: inherit to take the parent’s value intentionally. Browser styles can set border-box on some buttons, inputs, and tables, so inspect the computed property on the element you are measuring.
Compare two boxes with the same 200px width
Save the complete example as box-sizing-example.html in UTF-8 and open it in a browser. The boxes share the same width, padding, and border. Only box-sizing differs.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>box-sizing comparison</title>
<style>
body { margin: 0; color: #20242b; background: #fff; font: 16px/1.7 system-ui, sans-serif; }
#demo { padding: 24px; max-width: 820px; margin: auto; }
h1 { font-size: 22px; margin: 0 0 20px; }
.samples { display: flex; flex-wrap: wrap; gap: 24px; }
.label { margin: 0 0 8px; font-weight: 700; }
.target, .reference {
width: 200px;
padding: 20px;
border: 5px solid #006c9c;
background: #bfe3ff;
}
.target { box-sizing: content-box; }
.reference { box-sizing: border-box; }
</style>
</head>
<body>
<main id="demo">
<h1>box-sizing comparison</h1>
<section class="samples">
<div>
<p class="label">content-box</p>
<div class="target">width: 200px</div>
</div>
<div>
<p class="label">border-box</p>
<div class="reference">width: 200px</div>
</div>
</section>
</main>
</body>
</html>
The first box measures 200 + 40 + 10 = 250px across its borders. The second measures 200px across its borders, leaving 200 − 40 − 10 = 150px for the content.
Inspect each element and find the central content width in the box-model diagram in the Computed panel. For this example without transforms, document.querySelector('.target').getBoundingClientRect().width returns the width across the borders, 250. Use .reference for the second box to get 200.
Fit a card inside its parent
Use border-box when the whole card, through its border, should fit the width set by the parent. To compare the two modes, add these rules just before the example’s closing </style> tag. The dashed outline marks the parent’s width.
.samples {
display: block;
width: min(280px, 100%);
outline: 1px dashed #506070;
}
.samples > div + div { margin-top: 16px; }
.target, .reference { width: 100%; }
When the parent is 280px across, the content-box sample measures 330px through its border. It extends 50px past the outline. The border-box sample measures 280px. Narrow the browser window and the same padding-and-border difference remains. Remove the added rules to restore the example.
For a normal-flow block that only needs to fill the available space, width: auto may be enough. If you need width: 100% and horizontal margins, account for the margins separately or use parent padding or a layout gap to create the spacing.
Why a border box can still overflow
In the original example, change only the shared width to 30px. The horizontal padding and borders already measure 50px. The border-box content width becomes zero and the border-box width is 50px. The content-box sample measures 80px. Text can overflow without increasing the measured border-box width. Restore 200px after testing.
If the box itself is wider than its parent, inspect the computed min-width and margins first. In a horizontal flex row, an automatic minimum size can prevent an item from shrinking. Try min-width: 0 on the item that needs to shrink. If only a long string escapes a correctly sized box, try overflow-wrap: anywhere where wrapping is appropriate. The W3C Flexbox specification describes when an automatic minimum applies.
Apply the sizing rule where it is needed
To scope the rule to a group of new cards, add class="cards" to their wrapper and put this CSS in your stylesheet. It applies to the wrapper, its descendants, and their ::before and ::after pseudo-elements.
.cards,
.cards *,
.cards::before,
.cards::after,
.cards *::before,
.cards *::after {
box-sizing: border-box;
}
Before adding a rule to a site with an existing reset, check which value already wins. Keep legacy components and third-party widgets that expect content-box outside the new scope, then check narrow layouts for horizontal scrolling.
See the CSS box model guide for a diagram of the four areas. Use border-width when you need to set a different border thickness on each side.





