CSS Box Model
The CSS box model is a container that contains multiple properties including borders, margin, padding and the content itself. It is used to create the design and layout of web pages. It can be used as a toolkit for customizing the layout of different elements. The web browser renders every element as a rectangular box according to the CSS box model.
Box-Model has multiple properties in CSS. Some of them are given below:
- borders
- margins
- padding
- Content
BOX MODEL
Explanation of the different parts in the box model:
- Content – The content of the box, where text and images appear
- Padding – Clears an area around the content. The padding is transparent
- Border – A border that goes around the padding and content
- Margin – Clears an area outside the border. The margin is transparent
CODE:
Result:
Width and Height of an Element
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
CODE:
Result:
Here, is the calculation:
The total width of an element should be calculated like this:
- Total element width = width + left padding + right padding + left border + right border + left margin + right margin
The total height of an element should be calculated like this:
- Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin
You may also like: CSS Height and Width