Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS3 box-sizing property

CSS3 box-sizing property

Example

The total height and width of an element include padding and border (padding and border):

#example1

{

box-sizing:

border-box

;

}

Try it »


Browser support

The numbers in the table indicate the first browser version that supports this property.

A number immediately before -webkit-, -ms-, or -moz- indicates the first browser version that supports that prefixed property.

property
box-sizing 10.0
4.0 -webkit- 8.0 29.0
2.0 -moz- 5.1
3.2 -webkit- 9.5

Property definition and usage

The box-sizing property defines how the total width and total height of an element are calculated, mainly setting whether padding and border need to be added.

For example, if you need to place two bordered boxes side by side, you can set box-sizing to "border-box". This lets the browser render boxes with the specified width and height, with the border and padding included inside the box.

By default, the width and height of an element are calculated as follows:

width(宽度) + padding(内边距) + border(边框) = 元素实际宽度
height(高度) + padding(内边距) + border(边框) = 元素实际高度
Default value: content-box
Inherited: no
Version: CSS3
JavaScript syntax: object.style.boxSizing="border-box"

Syntax

box-sizing: content-box|border-box|inherit:

Value Description
content-box Default value. If you set an element's width to 100px, the content area of the element will be 100px wide, and the width of any border and padding will be added to the final rendered width of the element.
border-box Tells the browser that the values you want to set for the border and padding are included within the width. That is, if you set an element's width to 100px, those 100px will include its border and padding, and the actual width of the content area is width minus (border + padding). In most cases, this makes it easier to set the width and height of an element.
**Note:** border-box does not include margin.
inherit Specifies that the value of the box-sizing property should be inherited from the parent element

More examples

Example

Set two borders placed side by side:

div

{

  box-sizing

:

border-box

;

 

width

:

50%

;

 

border

:

5px

solid

red

;

 

float

:

left

;

}

Try it »

Example

Set a universal box-sizing:

{

  box-sizing

:

border-box

;

}

Try it »


Related Articles

CSS3 tutorial: CSS3 User Interface