box-sizing: border-box
box-sizing decides what width and height measure.
content-box(the default):widthis the content only. Padding and border are added on top.border-box:widthincludes padding and border. The content area shrinks to make room.
.box {
width: 200px;
padding: 20px;
border: 5px solid;
}
box-sizing |
Content width | Total rendered width |
|---|---|---|
content-box |
200px | 200 + 40 + 10 = 250px |
border-box |
200 − 40 − 10 = 150px | 200px |
Margin is never included in either model.
1. The bug it prevents
.sidebar {
width: 100%;
padding: 16px;
}
With content-box, this element is 100% + 32px wide and overflows its parent, usually producing a horizontal scrollbar. With border-box, it’s exactly 100%.
The same applies to grids of percentage widths: two width: 50% columns with padding only fit side by side under border-box.
2. The global reset
Most projects and CSS frameworks start with:
*,
*::before,
*::after {
box-sizing: border-box;
}
Pseudo-elements are listed because * doesn’t match them.
A variant uses inheritance, so a third-party widget that expects content-box can opt out for its whole subtree with one rule:
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
.legacy-widget {
box-sizing: content-box; /* children inherit this */
}
3. Details worth knowing
min-width,max-width,heightfollow the same model aswidth.- If padding plus border exceed the width, the content area becomes zero and the box grows to fit the padding and border. It can’t go negative.
- Form controls. Several, including buttons and selects, use
border-boxin browser default styles, while text inputs historically usedcontent-box. The global reset removes the inconsistency. - Flex and grid items.
flex-basisand track sizes respectbox-sizingtoo, soflex: 0 0 200pxwith padding stays 200px underborder-box. box-sizingisn’t inherited by default, which is why the inherit variant sets it explicitly.- JavaScript measurements.
offsetWidthandgetBoundingClientRect().widthalways report the border-box size.clientWidthexcludes the border. Neither changes withbox-sizing; only what the CSSwidthvalue means changes.
4. Is there a padding-box?
It was proposed and briefly shipped in one browser, then removed. Only content-box and border-box are valid.
Summary
content-box |
border-box |
|
|---|---|---|
width covers |
Content | Content + padding + border |
| Adding padding | Makes the element bigger | Keeps the element the same size |
width: 100% + padding |
Overflows | Fits |
| Default | Yes | No (but nearly universal via reset) |
Follow-up questions to expect
- “Explain the CSS box model.” Content, padding, border, margin, from the inside out.
box-sizingpicks which of thosewidthcovers. - “Why not include margin?” Margins sit outside the box and can collapse with neighbours, so they aren’t part of an element’s own size. See block formatting context.
- “Reset vs normalize?” A reset removes default styles; normalize makes them consistent. The
box-sizingrule appears in both approaches: resetting vs normalizing CSS.
More CSS questions: top CSS interview questions.
