CSS Box Model Explained
Table of Contents + −
In the last lesson you learned about Box Sizing. Now let’s step back and look at the big idea behind it. Every single thing you see on a web page sits inside a box. Once you can see those boxes, CSS starts to make sense.
📦 What is the Box Model?
The box model is the idea that every element on a page is a rectangular box. A heading is a box. A paragraph is a box. Even a tiny link is a box.
Each box is built from four layers, one inside the other. Here is what each one means.
- Content is the real stuff. So that is your text, your image, or your button label.
- Padding is the space around the content, inside the box.
- Border is the line that goes around the padding.
- Margin is the space outside the border. It sits between this box and the next one.
So the content sits in the middle. Then padding wraps it. Then the border wraps the padding. Then margin pushes other boxes away. It goes from the inside out.
🖼️ A real-life way to picture it
Think of a framed photo on a wall.
- The photo itself is the content.
- The white card around the photo, inside the frame, is the padding. It gives the photo some room.
- The wooden frame is the border.
- The gap on the wall between this frame and the next frame is the margin.
You never hang two frames touching each other, right? That gap is the margin. And the white card that stops the photo touching the frame, that is the padding. It is the same idea in CSS.
🤔 Why do you need to know this?
Here is the real problem. You set a box to be 200px wide. But on screen it looks bigger than 200px. Or two boxes have a big gap between them and you don’t know why. That confusion comes from the box model.
Once you know the four layers, you can answer questions like these.
- Why is there space inside my button around the text? That is padding.
- Why is there a gap between two paragraphs? That is margin.
- Why is my box wider than I set it? The padding and border got added on top.
So the box model is not just theory. It is the thing that explains almost every spacing surprise you will hit.
🧩 The syntax
Each layer has its own CSS property. Here is a simple box that uses all four.
.card { width: 200px; /* the content area */ padding: 20px; /* space inside, around the content */ border: 4px solid navy; /* the line around the padding */ margin: 30px; /* space outside, pushing others away */}Let’s read it line by line.
width: 200px;sets how wide the content area is.padding: 20px;adds 20px of space on all four sides, inside the box.border: 4px solid navy;draws a 4px thick navy line around the padding.margin: 30px;adds 30px of empty space outside the box on all sides.
The one to watch here is padding. It is the space inside the box, and it is easy to mix up with margin later.
What you see
A box with text in the middle. The text has clear space around it inside a navy frame. And there is a wide empty gap between this box and anything next to it.
📏 A surprise about width
Now here is the part that catches almost every beginner. By default, width only sets the content width. The padding and border are added on top.
So take that .card from above. The real space it takes on screen is wider than 200px.
content width = 200px+ left padding = 20px+ right padding = 20px+ left border = 4px+ right border = 4px----------------------------total width = 248pxSee? You asked for 200, but you got 248 across. That extra space is your padding and border. This is exactly the problem box-sizing fixes, which you just learned. With box-sizing: border-box, the padding and border fit inside the 200px instead of adding on top.
🎯 A practical example
Let’s make a real card, like a small profile box you’d see on a website.
First the HTML.
<div class="profile"> <h3>Alex Carter</h3> <p>Web developer who loves CSS.</p></div>Now the CSS that shapes the box.
.profile { width: 250px; padding: 16px; /* keeps text off the edges */ border: 1px solid #ccc; /* a soft grey frame */ margin: 24px auto; /* gap on top and bottom, centered left and right */}Here is what each line is doing.
padding: 16px;stops the name and text from touching the frame. It gives them room.border: 1px solid #ccc;draws a thin light grey line so the card stands out.margin: 24px auto;puts 24px of space above and below. Theautoon the sides centers the card in its space.
A quick note on that auto value. It tells the browser to share the leftover left and right space equally. That is what pushes the card into the center.
What you see
A neat card with a thin grey outline. The name and text have comfortable space around them. The card sits in the center with clear gaps above and below.
🧱 The four layers side by side
Here is a quick table to keep the layers straight.
| Layer | Where it is | What it does |
| Content | The middle | Holds your text or image |
| Padding | Inside the box | Space between content and border |
| Border | Around the padding | The visible line of the box |
| Margin | Outside the box | Space between this box and others |
A simple way to remember the order from inside out: content, then padding, then border, then margin.
⚠️ Common Mistakes
A few things trip people up early on. Watch for these.
- Mixing up padding and margin. Remember, padding is space inside the box. Margin is space outside it.
/* ❌ Wanted space inside the button, used margin */.button { margin: 12px; }
/* ✅ Space inside the button comes from padding */.button { padding: 12px; }- Forgetting that width does not include padding and border by default. So your box ends up wider than you planned.
- Expecting two margins to always add up. When one box sits above another, their top and bottom margins often join into one. The bigger margin wins. They don’t stack. This is called margin collapse, and it is normal CSS behavior.
✅ Best Practices
Keep these habits and the box model will feel easy.
- Use
box-sizing: border-box;on your whole page. Then width means the full width you see, padding and border included. That is much less math. - Use padding for space inside an element and margin for space between elements. Pick the right one for the job.
- Don’t add margin and padding to fix the same gap. Decide which layer the space really belongs to.
A handy global reset
Many developers start every project with this one rule. It makes all boxes behave the simple way: * { box-sizing: border-box; }. The * means “every element on the page”.
🧩 What You’ve Learned
Let’s quickly go over what you now know.
- ✅ Every element on a page is a box, built from content, padding, border, and margin.
- ✅ Padding is space inside the box. Margin is space outside it.
- ✅ The layers go from the inside out: content, padding, border, margin.
- ✅ By default,
widthsets only the content, so padding and border make the box bigger. - ✅
box-sizing: border-box;keeps padding and border inside your set width.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Which layer is the space INSIDE the box, between the content and the border?
Why: Padding is the space inside the box, around the content. Margin is the space outside the box.
- 2
In what order do the box model layers go, from inside to outside?
Why: From the inside out it is content, then padding, then border, then margin.
- 3
A box has width: 200px, padding: 20px, and a 4px border on each side. By default, how wide is it on screen?
Why: 200 + 20 + 20 (padding) + 4 + 4 (border) = 248px, because width only sets the content by default.
- 4
What does box-sizing: border-box; do?
Why: With border-box, the padding and border are counted inside the width you set, so the box stays the size you asked for.
🚀 What’s Next?
Now that you can see the box behind every element, let’s start styling it. Next you’ll learn how to fill that box with color.