The CSS box model is a container that contains multiple properties including borders, margin, padding and the content itself. With the help of this box-model, you can create the design and layout of web pages. This can be used as a toolkit for customizing the layout of different elements.
The web browser provides every element as a rectangular box according to the CSS box model.
Box-Model has the following properties in CSS.
Border Area: This area contains in between the box’s padding and margin. Its dimensions are given by the width and height of the border.
Margin Area: The space between the border and margin is a margin area. Its dimensions are given by the width and height of the margin-box.
Padding Area: It defines the element’s padding. This area is actually the space around the content area and within the border-box. Its dimensions are given by the width and the height of the padding-box.
Content area: This area contains content like text, image, etc. The dimensions are given by content-box width and height.
Width and Height of Elements
Normally, when you set the width and height of an element using the CSS width and height properties, then, you are only setting the width and height of the content area of an element.
The actual element’s box width and height depend on several factors.
Total Width = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right
Total Height = height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin + bottom
Following calculation show the how to find total height of an element:
+ 60px (top + bottom padding)
+ 40px (top + bottom margin)
+ 40px (top + bottom border)