Careeroza
The Box Model
The CSS Box Model
Every HTML element is a rectangular box made of four layers, from inside out:
| Layer | What it is |
|---|---|
| Content | the text or image |
| Padding | transparent space around the content, inside the border |
| Border | a line around the padding |
| Margin | transparent space outside the border, separating it from other elements |
Example
Example
Open DevTools (Inspect) on any element to see its box model drawn with the same colors:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
.box {
width: 220px;
padding: 20px;
border: 6px solid #04AA6D;
margin: 24px;
background: #e6fcf5;
font-family: Arial;
}
</style>
</head>
<body>
<div class="box">Content area with 20px padding, a 6px border and 24px margin.</div>
</body>
</html>Result
Total Width
By default (box-sizing: content-box), width sets only the content. The element above takes:
220px (width) + 2 × 20px (padding) + 2 × 6px (border) = 272px wide, plus 24px margin on each side