The CSS box-sizing
property is used to alter the default CSS box model used to calculate width and height of the elements.
/* Keyword values */ box-sizing: content-box; box-sizing: border-box; /* Global values */ box-sizing: inherit; box-sizing: initial; box-sizing: unset;
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.
<div class="container"> <div class="parent"> <p>Parent container</p> <div class="child" style="box-sizing: content-box;"> <p>Child container</p> <p class="css"> box-sizing: content-box;<br/> width: 100%;<br/> padding: 0;<br/> border-width: 0;</p> </div> </div> <div class="parent"> <p>Parent container</p> <div class="child border-padding" style="box-sizing: content-box;"> <p>Child container</p> <p class="css"> box-sizing: content-box;<br/> width: 100%;<br/> padding: 5px;<br/> border-width: 10px;</p> </div> </div> <div class="parent"> <p>Parent container</p> <div class="child border-padding" style="box-sizing: border-box;"> <p>Child container</p> <p class="css"> box-sizing: border-box;<br/> width: 100%;<br/> padding: 5px;<br/> border-width: 10px;</p> </div> </div> </div>
p { margin-left: 0.2em; font-family: "Open Sans", sans-serif; } .container { display: flex; } .parent { width: 220px; border: solid 10px #FFC129; margin: 0.8em; } .child { width: 100%; margin: 2em auto; background-color: rgba(229, 232, 252, 0.6); } .border-padding { padding: 5px; border: solid #5B6DCD 10px; } .css { font-family: monospace; }
The example above shows three scenarios. In each scenario there is a parent DIV (with an orange border) that contains a child DIV. The child has width: 100%
set, and a pale blue background.
- The first scenario uses the default
box-sizing: content-box
. The child DIV has no padding and no border, and fits neatly inside its parent. - The second scenario uses the default
box-sizing: content-box
. The child DIV has added padding and a border. The child then spills outside the parent because its width is calculated using only the content: padding and border are then added to make the rendered width. - The third scenario uses
box-sizing: border-box
. The child DIV now fits neatly inside its parent because itswidth: 100%
accounts for the padding and border.
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 |
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
andheight
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 abox 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
andheight
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 ofwidth: 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
Example
HTML
<div class="content-box">Content box</div> <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. |
Candidate Recommendation | Initial definition. |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 1.0 -webkit[1] 10.0 |
(Yes)-webkit (Yes) |
8.0[1] |
7.0 | 3.0 (522)-webkit 5.1[3] |
|
padding-box |
No support | No support | 1.0 (1.7 or earlier)-moz[1] 29.0 (29.0) Removed in 50.0 (50.0) |
No support | No support | No support |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 2.1-webkit[1] 4.0 |
(Yes)-webkit (Yes) |
1.0 (1.0)-moz[1] 29.0 (29.0)[2] |
9.0 | (Yes) | (Yes) |
padding-box |
No support | No support | 1.0 (1.0)-moz[1] 29.0 (29.0) Removed in 50.0 (50.0) |
No support | No support | No support |
[1] box-sizing
is not respected when the height is calculated from window.getComputedStyle()
, in Internet Explorer (all versions), in Firefox prior to 23, and in Chrome. Edge doesn't exhibit the problem. Note that IE9's proprietary currentStyle
property does return the correct value of height
.
[2] In addition to the unprefixed support, Gecko 44.0 (Firefox 44.0 / Thunderbird 44.0 / SeaMonkey 2.41) added support for a -webkit
prefixed version of the property for web compatibility reasons behind the preference layout.css.prefixes.webkit
, defaulting to false
. Since Gecko 49.0 (Firefox 49.0 / Thunderbird 49.0 / SeaMonkey 2.46) the preference defaults to true
.
[3] The vendor prefix -webkit
was removed in 534.12.