The CSS Box Model
Every element is a rectangle made of four nested layers: content, padding, border, margin. Once you can see those boxes — and control how width is calculated — layout stops being mysterious.
1. The four layers
| Layer | What it is | Counts toward click area? |
|---|---|---|
content | Text, images — the payload | Yes |
padding | Space inside the border | Yes — shares the background |
border | The visible edge | Yes |
margin | Space outside, pushing neighbors away | No — always transparent |
Open your browser DevTools and hover any element: the orange/green/blue diagram you see is this model. Learning to read it is learning to debug layout.
2. border-box: the sizing fix
*, *::before, *::after {
box-sizing: border-box;
}
.card {
width: 300px; /* now INCLUDES padding + border */
padding: 20px;
border: 2px solid #E1E5E8;
}By default (content-box), width means only the content — padding and border grow the box beyond it, so a "300px" card renders 344px wide. border-box makes width mean the whole visible box. Practically every modern codebase opts in globally, and so should you.
3. Try it yourself
The two "300px" cards render different widths. Add the universal box-sizing: border-box rule at the top of the styles and watch them match.
4. Quiz — check your understanding
The CSS Box Model Quiz
4 questions · pass at 60%Q1. From inside out, the box model layers are…
Q2. Which layer is always transparent?
Q3. With box-sizing: border-box, width: 200px means…
Q4. An element has width 100px, padding 10px, border 5px, default sizing. Its rendered width is…
Every lesson quiz you pass (60%+) is saved in your browser and counts toward the CSSmatic Certificate of Completion.