CSS Box Model
All HTML elements can be considered as boxes. The CSS box model describes the spacing around every element.
Every HTML element is a rectangular box made of four layers:
- Content — the text or image inside
- Padding — space between content and border (inside)
- Border — a line around the padding
- Margin — space outside the border (between elements)
Try It — Visualise the Box Model
Preview
Box Model Layers
| Layer | Property | What it does |
|---|---|---|
| Content | width, height | The size of the content area |
| Padding | padding | Space inside the border |
| Border | border | Line around the element |
| Margin | margin | Space outside the element |
Total Width Formula
Total width = width + left padding + right padding + left border + right border
Tip: Use
box-sizing: border-boxin your CSS to makewidthinclude padding and border automatically — this is much more predictable and is what most modern websites use.