CSS

Box Model in CSS

Posted on Updated on

The term “Box Model ” is used when we are talking about design and layout .

In CSS, all the HTML contents are considered as boxes

It consists of :-

1. Margin

2. Border

3. Padding

4. Content

Find the reference image below:-

Box model image

· Margin – Clears area around the border. The margin does not have a background color, it is completely transparent

· Border – Border goes around the padding and content. The border is inherited from the color property of the box

· Padding – Clears an area around the content. The padding is affected by the background color of the box

· Content – The content of the box, which is actual content

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

Note: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add the padding, borders and margins.

Example:

The total width of the element in the example below is 200px:

Calculation:
150px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 20px (left + right margin)
= 200px