The CSS box model: content, padding, border and margin
Understand the idea behind every layout: content, padding, border and margin, box-sizing and how to see them.
In this lesson you will
- Name the four layers of the box
- Use padding, border and margin shorthand
- Fix the width puzzle with border-box
- Inspect boxes in the browser
About 15 minutes
Everything is a box
Here is the idea that unlocks CSS: every element on a page is a rectangular box. A paragraph is a box. A heading is a box. An image is a box. Even a round button is a box with rounded corners. Once you see boxes everywhere, spacing and layout stop being mysterious. This lesson is probably the most useful of the whole beginner course, so take your time with it.
Picture a framed photograph hanging on a wall. The photo itself is the content. The white mat around it is the padding. The frame is the border. And the empty wall space between this frame and the next one is the margin. Those four layers, from the inside out, are the box model.
+------------------------------+ <- margin (outside, empty)
| +------------------------+ |
| | border | |
| | +------------------+ | |
| | | padding | | |
| | | +------------+ | | |
| | | | content | | | |
| | | +------------+ | | |
| | +------------------+ | |
| +------------------------+ |
+------------------------------+
Padding: space inside
.card {
padding: 20px; /* all four sides */
padding: 10px 24px; /* top and bottom, left and right */
padding: 10px 24px 16px 8px; /* top, right, bottom, left (clockwise) */
padding-left: 30px; /* one side only */
}
Padding pushes the content away from the border. The background colour of an element extends through its padding. When you give one value, it applies to all sides. With two values, the first is vertical and the second horizontal. With four, remember T-R-B-L, going clockwise from the top, or think “trouble”.
Border: the frame
.card {
border: 2px solid #5A28C8; /* width, style, colour */
border-bottom: 4px dashed hotpink;
border-radius: 12px; /* rounded corners */
}
A border needs a width, a style (solid, dashed, dotted) and a colour. If you leave out the style, nothing shows, because the default style is none. The border-radius property rounds the corners, and a value of 50% on a square makes a circle.
Margin: space outside
.card {
margin: 24px auto; /* 24px top and bottom, centred horizontally */
}
Margin is the clear space around the outside of the border. It is always transparent. The shorthand works like padding. A very useful trick: when an element has a width, margin-left: auto; margin-right: auto; (or margin: 0 auto) centres it in its parent.
Style the card: add 20px of padding, a 3px solid violet border, rounded corners of 16px, and a margin of 24px on the top and bottom while centring it horizontally.
<style>
body { background: #F7F5FF; font-family: Arial, sans-serif; }
.card {
width: 260px;
background: white;
}
</style>
<div class="card">I am a box with content inside.</div>
Add padding: 20px; border: 3px solid #5A28C8; border-radius: 16px; margin: 24px auto; to .card.
<style>
body { background: #F7F5FF; font-family: Arial, sans-serif; }
.card {
width: 260px;
background: white;
padding: 20px;
border: 3px solid #5A28C8;
border-radius: 16px;
margin: 24px auto;
}
</style>
<div class="card">I am a box with content inside.</div>
The width puzzle and box-sizing
Here is the surprise that confuses nearly every beginner. You write width: 300px and add padding: 20px and a border: 5px. How wide is the box on screen? By default CSS adds them all: 300 + 20 + 20 + 5 + 5 = 350 pixels. The width you set applied only to the content. Your carefully planned layout is suddenly too wide.
The cure is one line that nearly every professional stylesheet starts with.
*, *::before, *::after {
box-sizing: border-box;
}
With border-box, a box that is 300 pixels wide stays 300 pixels wide, with the padding and border carved out of the inside. Now your sums work the way your brain expects. Add this rule to every project from now on.
Both boxes have width 200px, padding 30px and a 6px border. Add the box-sizing rule at the top and compare their widths.
<style>
/* add box-sizing here */
.a, .b { width: 200px; padding: 30px; border: 6px solid #5A28C8; margin-bottom: 10px; background: #EFEAFD; font-family: Arial; }
.a { box-sizing: content-box; }
.b { box-sizing: content-box; }
</style>
<div class="a">Box A</div>
<div class="b">Box B</div>
Change .b to box-sizing: border-box; and see how it shrinks compared with .a.
<style>
.a, .b { width: 200px; padding: 30px; border: 6px solid #5A28C8; margin-bottom: 10px; background: #EFEAFD; font-family: Arial; }
.a { box-sizing: content-box; }
.b { box-sizing: border-box; }
</style>
<div class="a">Box A (272px wide)</div>
<div class="b">Box B (200px wide)</div>
Margin collapsing
Another oddity: when the bottom margin of one block touches the top margin of the next, they do not add up. They collapse into the larger of the two. If a paragraph has margin-bottom: 20px and the next has margin-top: 30px, the gap is 30 pixels, not 50. It only happens vertically, and only between block elements. It is handy to know, so you are not surprised when space looks smaller than you calculated.
Seeing the boxes
The best way to understand a layout is to look at its boxes. Press F12, hover over any element in the Elements panel, and the browser draws the margin in orange, the border in yellow, the padding in green and the content in blue. The Computed tab shows a diagram with the exact numbers. When something is out of place, inspect it first and guess second.
Add * { outline: 1px solid red; } while you work. Every box gets a thin red line, and the structure of the page jumps out at you. Remove it before you publish.
Rules of thumb
- Use padding for space inside a box, especially if it has a background or border.
- Use margin to separate boxes from each other.
- Prefer spacing in one direction, such as only
margin-bottom, so spaces do not double up. - Use numbers from a small scale, such as 8, 16, 24 and 32 pixels, so everything feels consistent.
Negative margins and lots of fixed heights usually point to a layout problem that would be better solved with flexbox or grid, which you will learn soon.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which layer sits directly around the content?
- Margin
- Border
- Padding
- Outline
From the inside out: content, padding, border, margin.
What does margin: 0 auto do on a box with a width?
- Removes the box
- Centres it horizontally
- Adds a border
- Hides it
Auto margins on the left and right split the free space equally.
With the default box model, what is the total width of a 300px box with 20px padding and a 5px border?
- 300px
- 325px
- 350px
- 320px
300 + 40 padding + 10 border = 350px.
What does box-sizing: border-box change?
- It removes borders
- Width includes padding and border
- It adds a margin
- It rounds corners
The set width stays the final width.
Which tool shows margin, border and padding on a real page?
- The browser’s developer tools
- A calculator
- A spell checker
- A zip tool
Inspect an element to see its boxes drawn in colour.
Summary
- Every element is a box with content, padding, border and margin.
- Use box-sizing: border-box on everything.
- Padding is inside; margin is outside.
- Inspect boxes in the developer tools.
Questions
Why is my box wider than I set?
Padding and borders are added to the width by default. Use box-sizing: border-box.
Padding or margin?
Padding when the space belongs to the element, such as around a button label. Margin when you are separating it from neighbours.
Why do my margins not add up?
Vertical margins between blocks collapse into the larger one.
