If there is one idea that separates people who fight with CSS from people who control it, this is it. Every single element on a web page — every heading, button, image and paragraph — is a rectangular box. Once you can see those boxes and the four layers that make them up, CSS layout suddenly makes sense. We will use a simple human metaphor and a visualizer you can drag, so it sticks.
1Every element is a box — think of a person
Imagine a person standing in a room. That person is one HTML element. The box model has four layers, working from the inside out:
| Layer | The person metaphor | What it really is |
|---|---|---|
| Content | The person themselves | The actual text or image inside the element. |
| Padding | Their personal space — part of them | Space inside the box, between the content and the border. The background colour fills it. |
| Border | Their clothing — the visible edge | A line drawn around the padding. It has a width, style and colour. |
| Margin | Social distance from other people | Space outside the box that pushes other elements away. It is always transparent. |
So when something looks too cramped, you usually want more padding. When two things are too close to each other, you usually want more margin. Keep the person in mind: personal space vs. social distance.
- Every element is a rectangular box with four layers: content, padding, border, margin.
- Padding = personal space, inside the box, shares the background colour.
- Border = the visible edge (clothing) around the padding.
- Margin = social distance, outside the box, always transparent — it pushes other elements away.
2See it move — the box model visualizer
Reading about padding and margin only gets you so far. Drag the sliders below and watch the box react in real time. Notice three things as you play:
- Increasing padding makes the coloured area grow — the content gets more breathing room inside.
- Increasing margin does not change the coloured box; it pushes the dashed "other elements" further away.
- The live CSS at the bottom is exactly what you would write to produce the box you see.
- Padding grows the element's coloured area from the inside.
- Margin adds transparent space outside, separating the element from its neighbours.
- The CSS readout shows the exact code that produces the box on screen.
3The size surprise — box-sizing
Here is the gotcha that confuses almost every beginner. By default, when you set width: 200px, that 200px applies to the content only. Padding and border are then added on top. So a box like this:
.card {
width: 200px;
padding: 20px;
border: 5px solid;
}…is actually 250px wide on screen: 200 (content) + 20 + 20 (padding) + 5 + 5 (border). That extra 50px is why layouts "mysteriously" overflow.
box-sizing: border-box. Now width means the total width including padding and border — exactly what you expected. The content shrinks to fit instead of the box growing.Because this behaviour is so much more intuitive, professionals apply it to the whole page at the very top of their CSS:
*, *::before, *::after {
box-sizing: border-box;
}Add that one rule to any project and a whole category of layout headaches simply disappears. In the visualizer above, toggling box-sizing shows the difference live.
- Default (content-box): width sets the content only; padding and border are added on top, making the box bigger than expected.
- box-sizing: border-box makes width mean the total visible width — content shrinks to fit.
- Pros apply box-sizing: border-box to everything at the top of their CSS to avoid overflow surprises.
★ Practical: build a comfortable card
Open any playground and recreate a simple card. Aim to feel the difference between padding and margin:
- Make a box with a background colour and some text inside.
- Add padding so the text is not touching the edges.
- Add a border so you can see the edge clearly.
- Add margin and notice it does not change the box — it only adds space around it.
- Add box-sizing: border-box and set a fixed width; confirm padding no longer makes it overflow.
Ready to test yourself?
Take the short quiz. Score 60% or more to mark this lesson complete.
Start the quiz →💡 Log in to save your progress and earn the certificate.