How to Add Space Outside an Element with margin in CSS
margin puts space outside an element's border, between it and its neighbors. One value sets all four sides, and the background doesn't reach into the margin.
Syntax
<selector> {
margin: <size>;
}
| Part | What it does |
|---|---|
<selector> | The element to space out. |
<size> | The space on all four sides, such as 24px. Two values set top and bottom, then left and right. Four values go clockwise from the top: top, right, bottom, left. |
MDN's margin page shows every form of the value.
The Example
The example is three notices, each on a yellow background, with a 24-pixel margin around every one.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Margin</title>
<style>
p {
background: #fff4ce;
margin: 24px;
}
</style>
</head>
<body>
<p>The lift is out of order today.</p>
<p>Lunch is at 12:30.</p>
<p>The car park closes at 20:00.</p>
</body>
</html>
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- Each yellow band starts 32 pixels from the left edge: the page's own 8-pixel margin plus the paragraph's 24.
- The yellow stops at the margin. The margin is outside the element, so the background doesn't fill it.
- The gap between two paragraphs is 24 pixels, not 48. The bottom margin of one paragraph and the top margin of the next combined into one.
MDN's margin collapsing page explains the last point: the top and bottom margins of blocks are sometimes combined into a single margin, the size of the largest one.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting the margins of two paragraphs to add up | They collapse into the larger one: 24 pixels, not 48 | Set the gap you want as one margin |
| Mixing up the order of four values | Four values go top, right, bottom, left: 10px 20px 30px 40px put 40 pixels on the left | Read the values clockwise from the top |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The space between elements is smaller than both margins together | Margin collapsing | Use one margin of the full size |
| The background doesn't cover the space | Margin is outside the background | Use padding for space inside the background |
FAQ
What do two values mean?
Top and bottom first, then left and right: margin: 10px 20px gave 10 pixels at the top and bottom and 20 pixels on the left and right.
How do I center a block?
Give it a width and margin: 0 auto. A 200-pixel-wide block with margin: 0 auto had 140 pixels on each side in a window 480 pixels wide.
Conclusion
Use margin for space outside an element, with one, two or four values. Expect the vertical margins between blocks to collapse, and use margin: 0 auto with a width to center a block.
Additional Resources
- margin, on MDN.
- Mastering margin collapsing, on MDN.
