The Box Model, Sizing & Fluid Typography
Understand content, padding, border, and margin, why border-box saves your sanity, and how clamp() creates fluid responsive sizing.
What you'll learn
- Predict element dimensions accurately with box-sizing: border-box
- Distinguish between internal padding and external margin
- Scale typography and spacing smoothly with clamp()
Every HTML element is a rectangular box made of four layers from the inside out: Content → Padding → Border → Margin.
Always Reset box-sizing
By default (content-box), adding padding: 20px to a width: 200px box makes it 240px wide! Switching to border-box keeps the total width locked at 200px.
*, *::before, *::after {
box-sizing: border-box;
}
.card {
width: 240px;
padding: 20px; /* stays 240px */
} .card {
box-sizing: content-box;
width: 240px;
padding: 20px; /* becomes 280px! */
} Fluid Sizing with clamp()
Instead of jumping font sizes at 4 different breakpoints, clamp(min, preferred, max) scales smoothly:
h1 {
font-size: clamp(1.25rem, 4vw, 2.25rem);
}
Try It Yourself
Toggle box-sizing between border-box and content-box, or adjust padding and clamp() below:
Check Your Understanding
1. With box-sizing: border-box, what is the total rendered width of a box with width: 200px, padding: 16px, and border: 2px solid?
Finished this lesson?
Your progress is saved locally in your browser, no account needed.