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.
*, *::before, *::after { box-sizing: border-box; }
Try it
The red bar is 200px. The second box lines up with it; the first one sticks out.
Syntax
box-sizing: content-box | border-box;
- Initial value:
content-box - Applies to: all elements that accept
widthorheight - 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
bodydoes 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-*andmax-*too. All four limits use the same box aswidthandheight. - 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 toborder-boxin browsers. That is why mixed layouts can look inconsistent without a reset. - Percentages. A
width: 50%box with padding fits as expected underborder-box; undercontent-boxit grows beyond 50%. - Margins are never included. Use
gaporcalc()when you need to account for them. padding-boxis not valid. An older Firefox-only value was removed.
Browser support
Supported in every browser, unprefixed.
Related
widthandheightโ the sizes it affectspaddingโ space inside the borderborderโ the line around the paddingmarginโ space outside the box- Lesson: box-sizing
- Lesson: The Box Model