CSS - Box sizing (property and definition)

1 - About

The box-sizing property is used to alter the default box model used to calculate widths and heights of elements.

The width and height are defined as the width and height:

depending of the box-sizing property value.

It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification (ie the content-box)

3 - Usage


box-sizing: content-box; /* default */
box-sizing: border-box;
box-sizing: inherit;

4 - Value

4.1 - content-box

The width and height properties are measured including only the content, but not the padding, border or margin.

Padding, border & margin will be outside of the box

4.2 - border-box

The width and height properties include the padding and border, but not the margin.

This is the box model used by Internet Explorer when the document is in Quirks mode.

padding and border will be inside of the box

The content box can't be negative and is floored to 0 making it impossible to use border-box to make the element disappear.

5 - Default


* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}

6 - Example


p {
   box-sizing: content-box;
   width: 200px;
   border: 30px solid DeepSkyBlue;
}


<p>A p element with a width of 200px in a content box</p>

Result:


p {
   box-sizing: border-box;
   width: 200px;
   border: 30px solid DeepSkyBlue;
}


<p>A p element with a width of 200px in a border box</p>

Result:

7 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap