The CSS Box Model Explained | Generated by AI
The CSS box model describes how every element in a webpage is rendered as a rectangular box. It consists of four layers (not three), but people sometimes group them into three when they combine padding + border + margin as “spacing”. The precise and correct order, however, has four distinct parts.
Here is the correct structure, from the inside (center) to the outside:
-
Content
The innermost part: the actual text, image, or other content of the element.
Controlled by properties likewidth,height,font-size, etc. -
Padding
The space inside the element, between the content and the border.
It is transparent (takes the background of the element).
Controlled bypadding,padding-top,padding-right, etc. -
Border
The line that surrounds the padding (and content).
Controlled byborder,border-width,border-style,border-color, etc. -
Margin
The outermost layer: transparent space outside the border, used to separate the element from neighboring elements.
Controlled bymargin,margin-top, etc.
Can be negative and can cause margin collapsing.
Visual order from inside → outside:
Margin (outside)
└─ Border
└─ Padding
└─ Content (text/image/etc.)
Or as a simple diagram:
+---------------------------+ ← Margin
| Border |
| +---------------------+ | ← Border
| | Padding | |
| | +---------------+ | |
| | | Content | | |
| | +---------------+ | |
| | | |
| +---------------------+ |
| |
+---------------------------+
Common “three-part” misconception
Some tutorials simplify it as:
- Content
- Padding/Border (grouped as “inner spacing + edge”)
- Margin
But technically and according to the official CSS specification, there are four parts in this exact order:
content → padding → border → margin
This order is critical when calculating the total space an element occupies (especially with box-sizing: content-box vs box-sizing: border-box).