The box-sizing
CSS property defines how the user agent should calculate the total width and height of an element.
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.
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.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.The box-sizing
property is specified as a single keyword chosen from the list of values below.
content-box
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.border-box
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. The content box can't be negative and is floored to 0, making it impossible to use border-box
to make the element disappear.content-box | border-box
This example shows how different box-sizing
values alter the rendered size of two otherwise identical elements.
<div class="content-box">Content box</div> <br> <div class="border-box">Border box</div>
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 */ }
Specification | Status | Comment |
---|---|---|
CSS Basic User Interface Module Level 3 The definition of 'box-sizing' in that specification. | Recommendation | Initial definition. |
Initial value | content-box |
---|---|
Applies to | all elements that accept width or height |
Inherited | no |
Media | visual |
Computed value | as specified |
Animation type | discrete |
Canonical order | the unique non-ambiguous order defined by the formal grammar |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 10
|
12
|
29
|
8
|
7 | 5.1
|
padding-box
|
No | No | 1 — 50 | No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 4
|
? | Yes
|
29
|
Yes | Yes | ? |
padding-box
|
No | ? | No | 4 — 50 | No | No | No |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing