CSS Box Model: Why content-box and border-box Give Different Widths
The CSS box model describes the size and spacing of an element’s box as content, padding, border, and margin. With width: 240px, padding and borders make a content-box wider, while a border-box normally includes them inside the 240px.
The four areas of a box
From the inside out, padding, border, and margin surround the content. By default, the background extends behind the border. The background-clip property changes how far it is painted. The element’s own background does not paint into the margin. CSS Box Model · The Box Model
| Area | Role |
|---|---|
| Content | Space for text and images width |
| Padding | Space between content and border padding |
| Border | Area that encloses the content and padding border |
| Margin | Space outside the border that separates boxes margin |
Calculate content-box and border-box widths
With content-box, width is the content width. The box below has 20px of padding and an 8px border on each side, so the width across the border box is 240 + 2 × 20 + 2 × 8 = 296px.
.box {
box-sizing: content-box;
width: 240px;
padding: 20px;
border: 8px solid #536d88;
}
With border-box, that outer width is 240px in this example and the content is 240 - 40 - 16 = 184px wide. The margin sits outside the specified width in both modes. The initial value is content-box, but browser styles can set a different value on some form controls. CSS Box Sizing · box-sizing
Compare two boxes with the same width
Save this code as box-model.html and open it in a browser. The top box uses content-box and the bottom uses border-box. Their width, padding, border, and margin declarations are identical. In a very narrow window, the comparison area can scroll horizontally.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Box model size comparison</title>
<style>
body {
margin: 0;
color: #243449;
background: #eef2f6;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 640px; margin: 24px auto; padding: 0 16px; }
h1 { font-size: 26px; }
.stage {
display: flow-root;
overflow-x: auto;
border: 1px dashed #8498ad;
background: #dfe7ef;
}
.box {
width: 240px;
margin: 16px;
padding: 20px;
border: 8px solid #536d88;
background: #fff4d9;
}
.content-box { box-sizing: content-box; }
.border-box { box-sizing: border-box; }
</style>
</head>
<body>
<main>
<h1>Compare box sizes</h1>
<p>Both boxes specify a width of 240px.</p>
<div class="stage">
<div class="box content-box">content-box</div>
<div class="box border-box">border-box</div>
</div>
<p>Inspect the content, padding, border, and margin in developer tools.</p>
</main>
</body>
</html>
Change only the shared padding from 20px to 30px. The top box grows from 296px to 316px across its borders. The bottom box stays 240px wide, while its content narrows from 184px to 164px. Restore the 20px padding after comparing the two.
Read the dimensions in developer tools
Right-click the first box and choose Inspect. In the computed box model, find the 240px content width, 20px padding on each side, and 8px borders. Select the second box to see the content width change to 184px. Both have a 16px margin outside the border. Refresh to restore any padding changed only in developer tools.
Margin collapse and overflow
In normal block flow, positive vertical margins that meet can collapse into one margin. The two boxes each have a 16px margin, yet the gap between them is 16px. Grid and Flexbox items do not collapse margins in the same way. CSS 2.2 · Collapsing Margins
With border-box, padding and borders can still make the box wider than specified because the content width cannot go below zero. If you change the example’s width to 40px without changing padding or borders, the box is at least 56px wide. Handle overflow from long words or large images separately with wrapping or size limits.




