Css Boxmodel
# CSS Box Model
* * *
## CSS Box Model
All HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content.
The box model allows us to add a border around elements, and to define space between elements.
The image below illustrates the box model:

Explanation of the different parts:
* **Margin** - Clears an area outside the border. The margin is transparent.
* **Border** - A border that goes around the padding and content.
* **Padding** - Clears an area around the content. The padding is transparent.
* **Content** - The content of the box, where text and images appear.
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
* * *
## Width and Height of an Element
**Important:** When you set the width and height properties of an element with CSS, you are setting just the width and height of the content area. To know the full size of the element, you must also add the padding, borders, and margins.
The total width of the element in the example below is 450px:
## Example
```css
div {
width: 300px;
border: 25px solid green;
padding: 25px;
margin: 25px;
}
[Try it Yourself Β»](#)
Let's do the math:
300px (width)
+ 50px (left + right padding)
+ 50px (left + right border)
+ 50px (left + right margin)
= 450px
Imagine that you only had 250px of space. Let's make an element with a total width of 250px:
## Example
```css
div {
width: 220px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
[Try it Yourself Β»](#)
The total width calculation formula for an element is:
Total element width = width + left padding + right padding + left border + right border + left margin + right margin
The total height calculation formula for an element is:
Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin
* * *
## Browser Compatibility Issues
Once you have declared an appropriate DOCTYPE for your page, most browsers will render content as shown above. However, IE 5 and 6 render content incorrectly. According to the W3C specification, the space an element takes up is determined by the width property, while the padding and border values around the content are calculated separately. Unfortunately, IE5.X and 6 use their own non-standard model in quirks mode. In these browsers, the width property is not the width of the content, but rather the total width of the content, padding, and border combined.
There are solutions to this problem, but the best approach currently is to avoid it. That is, instead of adding padding with a specified width to an element, try adding padding or margin to the element's parent or child elements.
IE8 and earlier versions of IE do not support setting the width of padding and border width properties.
To resolve compatibility issues with IE8 and earlier versions, simply declare `` in the HTML page.
YouTip