Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Box Model and Sizing

The box-sizing property

Reference for the CSS box-sizing property: how width and height include padding and borders, why border-box is the common reset, with live comparisons.

The box-sizing property decides which part of the box width and height describe. With content-box (the default), they set the size of the content area only, and padding and border are added outside it, so a box with width: 200px and padding: 20px is actually 240px wide. With border-box, the stated size includes padding and border, so the box is 200px wide and the content area shrinks to make room.

Most projects switch everything to border-box, because it makes percentage and fixed widths behave the way people expect.

CSS
*, *::before, *::after {
  box-sizing: border-box;
}

Try it

Same width, different total size

The red bar is 200px. The second box lines up with it; the first one sticks out.

Why 100% width overflows with padding

Syntax

CSS
box-sizing: content-box | border-box;
  • Initial value: content-box
  • Applies to: all elements that accept width or height
  • Inherited: no
  • Animatable: no (discrete)
  • Computed value: as specified

Values

Value Description
content-box width and height set the content box. Padding and border are added on the outside.
border-box width and height include content, padding and border. Margin is still outside.

Usage notes

  • Not inherited. Setting it on body does not affect descendants. Use the universal selector, or inherit deliberately: html { box-sizing: border-box } *, *::before, *::after { box-sizing: inherit } lets individual components opt out.
  • It affects min-* and max-* too. All four limits use the same box as width and height.
  • Content can disappear. If padding plus border exceed the stated size under border-box, the content area becomes zero but the box does not get smaller than its padding and border.
  • Form controls such as <input>, <select> and <button> often default to border-box in browsers. That is why mixed layouts can look inconsistent without a reset.
  • Percentages. A width: 50% box with padding fits as expected under border-box; under content-box it grows beyond 50%.
  • Margins are never included. Use gap or calc() when you need to account for them.
  • padding-box is not valid. An older Firefox-only value was removed.

Browser support

Supported in every browser, unprefixed.