CSS Box Model

All HTML elements can be regarded as boxes. In CSS, the term "box model" is used in design and layout.

The CSS box model is essentially a box that encapsulates the surrounding HTML elements, including margins, borders, padding, and actual content.

The box model allows us to place elements in the space between other elements and the borders of surrounding elements.

The picture below illustrates the Box Model:

CSS box-model

Explanation of the different parts:

  • Margin: Clear area outside the border, the margin is transparent.

  • Border: A border around the inner padding and outside the content.

  • Padding: Clear area around the content, the padding is transparent.

  • Content: The content of the box, displaying text and images.

In order to correctly set the width and height of an element in all browsers, you need to know how the box model works.

Element Width and Height


When you specify the width and height properties of a CSS element, you just set the width and height of the content area. For full-size elements, you must also add padding, borders, and outer margins.

The total width of the elements in the following example is 300px:


    width: 300px;    
    border: 25px solid green;    
    padding: 25px;    
    margin: 25px;

 Try It! 

Let us do the calculation:
300px (width)
+ 50px (left + right padding)
+ 50px (left + right border)
+ 50px (left + right margin)
= 450px

Imagine that you only have 250 pixels of space. Let's set an element with a total width of 250 pixels:


div {
    width: 220px;    
    padding: 10px;    
    border: 5px solid gray;    
    margin: 0; 

 Try It! 

The formula for calculating the total width of the element is as follows:

The width of the total element = width + left padding + right padding + left border + right border + left margin + right margin

The formula for calculating the total height of the element is as follows:

The height of the total element = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin

Browser Compatibility Issues

Once the appropriate DTD is set for the page, most browsers will render the content as shown above. However, the rendering of IE 5 and 6 is incorrect. According to the W3C specification, the space occupied by the element content is set by the width property, and the padding and border values around the content are calculated separately. Unfortunately, IE5.X and 6 use their own non-standard model in weird mode. The width property of these browsers is not the width of the content, but the sum of the width of the content, padding, and border.

Although there are ways to solve this problem. But the best solution is to avoid this problem. That is, do not add padding with a specified width to the element, but try to add padding or outer margins to the element's parent and child elements.

IE8 and earlier versions of IE do not support setting the padding width and border width properties.

To solve the incompatibility problem of IE8 and earlier versions, you can declare <!DOCTYPE html> in the HTML page.