Web/CSS/box-sizing

From Get docs

The box-sizing CSS property sets how the total width and height of an element is calculated.


By default in the CSS box model, 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. For example, if you have four boxes with width: 25%;, if any has left or right padding or a left or right border, they will not by default fit on one line within the constraints of the parent container.

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

  • content-box 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, making the element wider than 100px.
  • border-box tells the browser to account for any border and padding in the values you specify for an element's 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.

Note: It is often useful to set box-sizing to border-box to layout elements. This makes dealing with the sizes of elements much easier, and generally eliminates a number of pitfalls you can stumble on while laying out your content.  On the other hand, when using position: relative or position: absolute, use of box-sizing: content-box allows the positioning values to be relative to the content, and independent of changes to border and padding sizes, which is sometimes desirable.


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 include the content, but does not include the padding, border, or margin. For example, .box {width: 350px; border: 10px solid black;} renders a box that is 370px wide.
Here, the dimensions of the element are calculated as: width = width of the content, and height = height of the content. (Borders and padding are not included in the calculation.)
border-box
The width and height properties include the content, padding, and border, but do not include the margin. Note that padding and border will be inside of the box. For example, .box {width: 350px; border: 10px solid black;} renders a box that is 350px wide, with the area for content being 330px wide. 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 definition

Initial value content-box
Applies to all elements that accept width or height
Inherited no
Computed value as specified
Animation type discrete

Formal syntax

content-box | border-box

Examples

Box sizes with content-box and border-box

This example shows how different box-sizing values alter the rendered size of two otherwise identical elements.

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; 
  /* Total width: 160px + (2 * 20px) + (2 * 8px) = 216px
     Total height: 80px + (2 * 20px) + (2 * 8px) = 136px
     Content box width: 160px
     Content box height: 80px */
}

.border-box { 
  box-sizing: border-box;
  /* Total width: 160px
     Total height: 80px
     Content box width: 160px - (2 * 20px) - (2 * 8px) = 104px
     Content box height: 80px - (2 * 20px) - (2 * 8px) = 24px */
}

Result

Specifications

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

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
box-sizing

Chrome Full support 10

Notes'

Full support 10

Notes'

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

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Edge Full support 12


Full support 12


Full support 12

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Firefox Full support 29


Full support 29


Full support 1

Prefixed' Notes'

Prefixed' Implemented with the vendor prefix: -moz- Notes' Before Firefox 23, box-sizing is not respected when the height is calculated from window.getComputedStyle(). Full support 49

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit- Full support 44

Prefixed' Disabled'

Prefixed' Implemented with the vendor prefix: -webkit- Disabled' 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.

IE Full support 8

Notes'

Full support 8

Notes'

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

Opera

Full support 7

Safari Full support 5.1


Full support 5.1


Full support 3

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

WebView Android Full support 4

Notes'

Full support 4

Notes'

Notes' box-sizing is not respected when the height is calculated from window.getComputedStyle(). Full support 2

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Chrome Android Full support 18

Notes'

Full support 18

Notes'

Notes' box-sizing is not respected when the height is calculated from window.getComputedStyle(). Full support 18

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Firefox Android Full support 29


Full support 29


Full support 4

Prefixed' Notes'

Prefixed' Implemented with the vendor prefix: -moz- Notes' Before Firefox 23, box-sizing is not respected when the height is calculated from window.getComputedStyle(). Full support 49

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit- Full support 44

Prefixed' Disabled'

Prefixed' Implemented with the vendor prefix: -webkit- Disabled' 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.

Opera Android Full support 14

Notes'

Full support 14

Notes'

Notes' box-sizing is not respected when the height is calculated from window.getComputedStyle(). Full support 14

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Safari iOS Full support 6


Full support 6


Full support 1

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Samsung Internet Android Full support 1.0

Notes'

Full support 1.0

Notes'

Notes' box-sizing is not respected when the height is calculated from window.getComputedStyle(). Full support 1.0

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

padding-box

Deprecated'Non-standard'

Chrome

No support No

Edge

No support No

Firefox

No support 1 — 50

IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android

No support 4 — 50

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.'
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.'
Deprecated. Not for use in new websites.
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.'
Requires a vendor prefix or different name for use.


See also