The Complete Box Model
Content → padding → border → margin
1. Read boxes from the inside out
- Content holds text, images, or child elements.
- Padding creates space inside the border.
- The border surrounds the content and padding.
- Margin creates transparent space outside the border.
Margin area
Border area
Padding area
Content area
Each layer surrounds the previous one.
2. Compare sizing modes
Both boxes declare width: 280px, padding: 20px, and border: 4px.
content-box
Visible width: 328px
280 content + 40 padding + 8 border
border-box
Visible width: 280px
Content shrinks so padding and border fit inside 280px.
Key rule
Margin is outside both sizing modes.
Using border-box simplifies calculations, but it does not include margin.