The box model: spacing, borders and sizes
Every element is a box. Control its padding, border, margin, width and height, use box-sizing border-box, centre a page with margin auto, and understand block and inline display.
🎯 By the end of this lesson you will be able to:
- Name the four layers of the box model: content, padding, border and margin
- Set widths with max-width and box-sizing: border-box
- Centre a block and choose between block, inline and inline-block display
Outline
What you'll learn and why it matters
To the browser, every element is a rectangular box. A heading, a paragraph, a link: all boxes. Spacing, borders and sizes all come from one idea, the box model. Once you understand it, cards, buttons and page layouts stop being a mystery.
The four layers of a box
| Layer | Property | What it is |
|---|---|---|
| Content | width, height | The text or image itself |
| Padding | padding | Space between the content and the border. The background colour fills it. |
| Border | border | A line around the padding |
| Margin | margin | Space outside the border that pushes other boxes away. Always transparent. |
.card {
background-color: #fef3c7;
padding: 16px;
border: 3px solid #b45309;
margin: 24px;
}Show the HTML this CSS styles
<div class="card">
<h2>Masala chai</h2>
<p>Black tea simmered with milk, ginger and cardamom.</p>
</div>Browser preview
Try picturing it: the yellow background fills the content and the padding, the brown line is the border, and the 24px margin is the empty space around the card.
One value, two values, four values
padding and margin can take one to four values. With four, they go clockwise from the top: top, right, bottom, left.
| You write | Means |
|---|---|
padding: 16px; | 16px on all four sides |
padding: 8px 16px; | 8px top and bottom, 16px left and right |
padding: 4px 8px 12px 16px; | top 4, right 8, bottom 12, left 16 |
margin-top: 32px; | Only one side. Also -right, -bottom, -left |
border usually takes three values in any order: width, style and colour, for example border: 2px solid #94a3b8;. Styles include solid, dashed and dotted. border-radius rounds the corners.
Widths and box-sizing
By default, width sets only the content width, and padding and border are added on top. So a box with width: 300px, 20px padding and a 5px border is really 350px wide. That surprises everyone. The fix is one rule that nearly every website uses:
*,
*::before,
*::after {
box-sizing: border-box;
}With border-box, width: 300px means the whole visible box is 300px, padding and border included. Much easier.
* {
box-sizing: border-box;
}
.card {
max-width: 320px;
padding: 20px;
border: 2px solid #b45309;
border-radius: 12px;
background-color: #fef3c7;
margin: 24px auto;
}Show the HTML this CSS styles
<div class="card">
<h2>Masala chai</h2>
<p>Black tea simmered with milk, ginger and cardamom.</p>
</div>Browser preview
Two new ideas in that example:
| Code | Effect |
|---|---|
max-width: 320px | The box is never wider than 320px, but it can shrink on a narrow phone screen. Better than a fixed width. |
margin: 24px auto | 24px above and below, and auto left and right: the browser shares the leftover space equally, which centres the box. |
Block, inline and inline-block
The display property decides how a box behaves in the flow of the page:
| Value | Starts on a new line? | Width and height work? | Default for |
|---|---|---|---|
block | Yes, and fills the width | Yes | div, p, h1, section, li |
inline | No, sits inside text | No; vertical padding does not push lines apart | span, a, strong, em |
inline-block | No, sits in the line | Yes | Buttons; useful for tags and badges |
none | Not shown at all | - | Hidden content |
.tag {
display: inline-block;
padding: 4px 12px;
border-radius: 999px;
background-color: #dbeafe;
color: #1e3a8a;
margin: 0 4px 8px 0;
}Show the HTML this CSS styles
<p>Skills:
<span class="tag">HTML</span>
<span class="tag">CSS</span>
<span class="tag">Accessibility</span>
</p>Browser preview
Practice
Guided exercise
A notice box
Turn the paragraph with class notice into a box: light blue background, 16px padding, a 4px solid blue border on the left side only, rounded corners, and 24px of space above and below.
- Select
.notice. - Add
background-colorandpadding: 16px. - Use
border-left: 4px solid #2563eb;for one side only. - Add
border-radius: 8pxandmargin: 24px 0.
Show the solution
.notice {
background-color: #eff6ff;
padding: 16px;
border-left: 4px solid #2563eb;
border-radius: 8px;
margin: 24px 0;
}Show the HTML this CSS styles
<p>Our new term starts on Monday.</p>
<p class="notice">Bring your ID card on the first day.</p>
<p>See you soon!</p>Browser preview
How to get there: Padding makes room inside the box so the text does not touch the edges. border-left sets just one side, and margin: 24px 0 adds space above and below but none at the sides.
Your turn
Centre a button-style link
Make the link with class button look like a button: inline-block, white text on dark green (#166534), 12px top and bottom padding, 24px left and right, rounded corners and no underline.
Show the solution
.button {
display: inline-block;
color: white;
background-color: #166534;
padding: 12px 24px;
border-radius: 8px;
text-decoration: none;
}Show the HTML this CSS styles
<p>Ready to order?</p>
<p><a class="button" href="order.html">Order now</a></p>Browser preview
How to get there: Links are inline, so vertical padding would overlap nearby lines. inline-block makes the padding count, giving a solid button shape while it stays in the line of text.
Common mistakes
Forgetting box-sizing: border-box
You see: Boxes are wider than the width you set, and layouts overflow
Fix: Add the box-sizing rule at the top of your stylesheet.
Using a fixed width like width: 800px
You see: Sideways scrolling on phones
Fix: Use max-width so the box can shrink.
Trying margin: auto to centre text
You see: The text stays on the left
Fix: margin: auto centres a block box that has a width; use text-align: center to centre text inside a box.
Setting width or vertical padding on an inline element
You see: Nothing changes, or the padding overlaps other lines
Fix: Use display: inline-block or block first.
Mixing up padding and margin
You see: Background colour does not reach the space you added, or vice versa
Fix: Padding is inside the border (coloured); margin is outside (transparent).
Recap
- Every element is a box: content, padding, border, margin.
- Shorthand values go top, right, bottom, left (clockwise).
- Use
box-sizing: border-boxandmax-width;margin: 0 autocentres a block. displaydecides behaviour: block, inline, inline-block or none.
Key terms
- Box model
- The way CSS treats every element as content surrounded by padding, border and margin.
- Padding
- Space inside an element's border, filled by its background.
- Margin
- Transparent space outside an element's border.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which layer of the box is filled by the background colour?
2. `padding: 10px 20px;` gives how much padding on the left?
3. With box-sizing: border-box, a box has width: 300px, padding: 20px and a 5px border. How wide is it on screen?
4. Which rule centres a block with max-width: 600px horizontally?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.