Q&A-1Explain the CSS box model and how width and height are computed under content-box versus border-box.
Answer
The 90-second answer. The box model is four nested rectangles: content, padding, border, margin. width and height name one of them, and box-sizing decides which. Under the initial value content-box, width sizes the content box only, so padding and border are added on top of it. Under border-box, width sizes the border box (content, padding and border together) and the content box is whatever is left. Margin sits outside both: box-sizing never counts it, so width: 100% plus any margin still overflows the parent.
content-box (initial) | border-box | |
|---|---|---|
width applies to | content box | border box |
| rendered border-box width | width + padding + border | width |
| content width | width | max(0, width − padding − border) |
Worked, with width: 300px; padding: 0 20px; border: 2px solid:
content-box: content 300px, padding 40px, border 4px → 344px rendered.border-box: 300px rendered, content box 300 − 40 − 4 = 256px.
/* checked in Chrome 126, Firefox 127, Safari 17 */
*, *::before, *::after { box-sizing: border-box; }
box-sizing is not an inherited property, so the * selector is doing the work. The html { box-sizing: border-box } *, *::before, *::after { box-sizing: inherit } variant exists so one subtree can opt back out with box-sizing: content-box.
Why most codebases flip the default. Percentage widths, padding and grid tracks then compose without arithmetic. The failure it prevents: a width: 50% card with padding: 16px under content-box is 50% + 32px wide, so two of them plus a gap overflow the row and you get a horizontal scrollbar at some viewport widths but not others.
What border-box does not change. Percentage widths still resolve against the containing block's content box, not the element's own box. Percentage padding, including padding-top and padding-bottom, resolves against the containing block's inline size, which is why a 16:9 box is written padding-top: 56.25%. And border-box cannot shrink below padding plus border: width: 10px; padding: 20px renders 40px wide with a zero-width content box.
Failure modes, and how you catch each.
- Overflow from content-box arithmetic. Detect:
document.documentElement.scrollWidth > document.documentElement.clientWidth, or DevTools' overflow badge on the container. - Flex or grid items that refuse to shrink.
min-width: autofloors a flex item at its content size regardless ofbox-sizing;min-width: 0is the fix. Detect: the item'sgetBoundingClientRect().widthexceeds the track size. - Reading the wrong measurement in JS.
getBoundingClientRect()andoffsetWidthare border-box;clientWidthis the padding box (content + padding, no border, no scrollbar);scrollWidthincludes overflow. A layout calculation that mixes them is off by the border width: 4px here, which is exactly the kind of bug that only shows at one zoom level.
When not to use border-box. Rarely. It is the wrong tool when padding should grow the element rather than eat into a fixed content area, a button whose label must occupy a fixed 200px content width, but min-width plus content-box on that one element expresses that more honestly.
Curated: · Written: · Reviewed: