The Box Model
Every element is a box of content, padding, border, and margin β understand the math and the sizing surprises disappear.
Layout bugs are usually box-model bugs. This lesson removes the surprise permanently.
Every element is four boxes nested together
βββββββββββββββββββββββββββββββββββ
β margin (transparent, outside) β
β βββββββββββββββββββββββββββββ β
β β border β β
β β βββββββββββββββββββββββ β β
β β β padding β β β
β β β βββββββββββββββββ β β β
β β β β content β β β β
β β β βββββββββββββββββ β β β
β β βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββ
- content β the text/image itself; sized by
width/height. - padding β space inside the border, between content and edge. Padding carries the element's background.
- border β the line.
border: 2px solid. - margin β transparent space outside the border, pushing neighbors away.
.card {
padding: 1rem;
border: 2px solid gray;
margin: 1rem;
}
Padding and margin each take 1β4 values (top/right/bottom/left, clockwise): padding: 4px 8px = 4 top & bottom, 8 left & right. There are also single sides: margin-top,
padding-left, β¦.
The prediction every beginner gets wrong
With default box-sizing: content-box, width sizes only the content box:
.box {
width: 200px;
padding: 20px; /* both sides */
border: 4px solid;
}
Real rendered width = 200 + 20 + 20 + 4 + 4 = 248px. Not 200. Set two of these side by side and they wrap when you were sure they would fit.
The universal fix
*,
*::before,
*::after {
box-sizing: border-box;
}
With border-box, width includes padding and border β width: 200px means the
whole box is 200px. This single rule (at the top of every stylesheet on earth) removes
the entire class of surprise. You will put it in every project stylesheet from now on.
Margin collapse (know it exists)
Vertical margins between siblings merge instead of adding: a margin-bottom: 32px
above a margin-top: 16px yields 32px of gap, not 48. Not a bug β a feature you
work with. Practical rule: space sections with one consistent direction of margin.
What you learned
- content β padding β border β margin, and which carry background
widthexcludes padding/border under default sizing β the 200px β 200px trapbox-sizing: border-boxon everything is the professional default- Adjacent vertical margins collapse to the larger
Next: display β how elements decide to stack or flow.