- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Margin
CSS Box Model
CSS Margin
Control space outside an element's border, use logical margins, understand vertical margin collapsing, and center constrained blocks safely.
Margin is the space outside an element's border, pushing other elements away. It is always transparent. It has two surprises that confuse almost everyone: margin: 0 auto centers a box, and two vertical margins that meet do not add up.
Space inside the border is CSS Padding, and how the two affect an element's total size is explained in Box Sizing.
Syntax
css
selector {
margin: value;
}Example
css
p {
margin: 20px;
}That puts 20px of space around every paragraph, on all four sides.
The shorthand
| You write | Top | Right | Bottom | Left |
|---|---|---|---|---|
| margin: 10px; | 10px | 10px | 10px | 10px |
| margin: 10px 20px; | 10px | 20px | 10px | 20px |
| margin: 10px 20px 30px; | 10px | 20px | 30px | 20px |
| margin: 10px 20px 30px 40px; | 10px | 20px | 30px | 40px |
With four values, go clockwise from the top. You can also set one side with margin-top, margin-right, margin-bottom or margin-left.
Centering with auto
margin: 0 auto on a box with a width centers it horizontally: the browser splits the free space equally between left and right. It only works on block elements that are narrower than their parent.
Margins that collapse
When two vertical margins meet in normal flow, only the larger one is used. A heading with margin-bottom: 20px followed by a paragraph with margin-top: 30px ends up 30px apart, not 50px. A child's top margin can even poke out through its parent's top, if the parent has no padding or border.
This does not happen with left and right margins, and not inside Flexbox or Grid, which is one reason many developers set spacing with gap there.
Complete CSS Margin Example
html
<!DOCTYPE html>
<html>
<head>
<title>CSS Margin Example</title>
<style>
div {
background-color: lightblue;
margin: 20px;
padding: 10px;
}
p {
background-color: yellow;
margin-top: 30px;
}
</style>
</head>
<body>
<div>This is a div with margin.</div>
<p>This paragraph has top margin.</p>
</body>
</html>The div has a 20px margin and the paragraph a 30px top margin. The gap between them is 30px, not 50px, because the two margins collapse into the larger one.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| margin | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
Good to know
- Margins can be negative, pulling an element towards its neighbor or making it overlap. Use them sparingly.
- Margins on inline elements like
<span>only work left and right, not top and bottom. - Many developers add margin in only one direction, usually the bottom, to keep spacing predictable.
CSS Margin exercises
Margin on every side
Give the box a margin of 20px on every side.
The HTML your code works on
<div class="box">Box</div>
<div class="other">Other</div>Result
