The CSS box-sizing property is used to alter the default CSS box model used to calculate width and height of the elements.

In CSS, by default, the width and height you assign to an element is applied only to the element's content box. If the element has any border or padding, this is then added to the width and height to arrive at the size of the box that's rendered on the screen. This means that when you set width and height you have to adjust the value you give to allow for any border or padding that may be added. This is especially tricky when implementing a responsive design.

The box-sizing property can be used to adjust this behavior:

  • content-box is the default, and gives you the default CSS box-sizing behavior. If you set an element's width to 100 pixels, then the element's content box will be 100 pixels wide, and the width of any border or padding will be added to the final rendered width.
  • border-box tells the browser to account for any border and padding in the value you specify for width and height. If you set an element's width to 100 pixels, that 100 pixels will include any border or padding you added, and the content box will shrink to absorb that extra width. This typically makes it much easier to size elements.

Some experts recommend that web developers should consider routinely applying box-sizing: border-box to all elements.

Syntax

The box-sizing property is specified as a single keyword chosen from the list of values below.

Values

content-box
This is the initial and default value as specified by the CSS standard. The width and height properties are measured including only the content, but not the padding, border or margin. For example, if you set .box {width: 350px;}, then apply {border: 10px solid black;} , then the rendered result is a box of width: 370px.
Here the dimensions of the element are calculated as: width = width of the content, and height = height of the content (excluding the values of border and padding).
border-box
The width and height properties include the content, the padding and border, but not the margin. Note that padding and border will be inside of the box e.g. .box {width: 350px; border: 10px solid black;} leads to a box rendered in the browser of width: 350px. The content box can't be negative and is floored to 0, making it impossible to use border-box to make the element disappear.
Here the dimensions of the element are calculated as: width = border + padding + width of the content, and height = border + padding + height of the content.

Formal syntax

content-box | border-box

Examples

HTML

<div class="content-box">Content box</div>
<br>
<div class="border-box">Border box</div>

CSS

div {
  width: 160px;
  height: 80px;
  padding: 20px;
  border: 8px solid red;
  background: yellow;
}

.content-box {
  box-sizing: content-box;
}

.border-box {
  box-sizing: border-box;
}

Result

Specifications

Specification Status Comment
CSS Basic User Interface Module Level 3
The definition of 'box-sizing' in that specification.
Proposed Recommendation Initial definition.

Initial valuecontent-box
Applies toall elements that accept width or height
Inheritedno
Mediavisual
Computed valueas specified
Animation typediscrete
Canonical orderthe unique non-ambiguous order defined by the formal grammar

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support

101

1 -webkit-

Yes

Yes -webkit-

29

1 -moz- 2

49 -webkit-

44 -webkit- 3

817

5.1

3 —? -webkit-

padding-box No No1 — 50 No No No
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic support

41

2.1 -webkit-

?

Yes

Yes -webkit-

29

4 -moz- 2

49 -webkit-

44 -webkit- 3

9 Yes Yes
padding-box No ? No4 — 50 No No No

1. box-sizing is not respected when the height is calculated from window.getComputedStyle().

2. Before Firefox 23, box-sizing is not respected when the height is calculated from window.getComputedStyle().

3. From version 44: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.

 

See also