CSS Box Model
The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content.
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 image below illustrates the box model:
Example 1: Demonstrating the Box Model
This example demonstrates the box model by setting values for margin, border, padding, and content width.
Example 2: Calculating Total Width
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 padding, borders and margins.
Example 3: Box Sizing Border Box
The box-sizing: border-box; property allows us to include the padding and border in an element's total width and height.
Example 4: Comparison
Compare two divs, one with default box-sizing and one with border-box.