Trivia Questions2 min readPremium

box-sizing: border-box

box-sizing decides what width and height measure.

  • content-box (the default): width is the content only. Padding and border are added on top.
  • border-box: width includes 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, height follow the same model as width.
  • 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-box in browser default styles, while text inputs historically used content-box. The global reset removes the inconsistency.
  • Flex and grid items. flex-basis and track sizes respect box-sizing too, so flex: 0 0 200px with padding stays 200px under border-box.
  • box-sizing isn’t inherited by default, which is why the inherit variant sets it explicitly.
  • JavaScript measurements. offsetWidth and getBoundingClientRect().width always report the border-box size. clientWidth excludes the border. Neither changes with box-sizing; only what the CSS width value 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-sizing picks which of those width covers.
  • “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-sizing rule appears in both approaches: resetting vs normalizing CSS.

More CSS questions: top CSS interview questions.