CSS Margins
Margins create space outside an element's border, pushing neighboring elements away. They are your main tool for spacing things apart: gaps between paragraphs, room around a card, or centering a block horizontally.
Margin properties
| Property | Sets the margin on |
|---|---|
margin-top |
the top side |
margin-right |
the right side |
margin-bottom |
the bottom side |
margin-left |
the left side |
margin |
shorthand for all four |
Margins accept lengths (20px, 1.5em), percentages (relative to the width of the containing block — even for top and bottom), the keyword auto, and negative values.
The shorthand
Like borders and padding, margin takes one to four values, clockwise from the top:
margin: 20px; /* all sides 20px */
margin: 10px 40px; /* top+bottom 10px, left+right 40px */
margin: 10px 40px 0; /* top 10px, left+right 40px, bottom 0 */
margin: 10px 20px 30px 40px; /* top, right, bottom, left */The example draws a dashed outline around each parent so you can see the margin as empty space between the parent's edge and the colored box.
<!DOCTYPE html>
<html>
<head>
<style>
body { font: 14px sans-serif; }
.wrap { border: 2px dashed #aaa; margin-bottom: 10px; }
.box { background: #264de4; color: white; padding: 8px; }
.m1 { margin: 20px; }
.m2 { margin: 5px 60px; }
.m3 { margin: 0 0 0 120px; }
</style>
</head>
<body>
<div class="wrap"><div class="box m1">margin: 20px</div></div>
<div class="wrap"><div class="box m2">margin: 5px 60px</div></div>
<div class="wrap"><div class="box m3">margin-left: 120px only</div></div>
</body>
</html>Note: A margin is always transparent. The parent's background shows through it — that is how margins differ from padding, which takes on the element's own background.
Centering with auto
When a block element has a set width, margin-left: auto and margin-right: auto split the leftover space equally, centering the element. This is the classic way to center a page layout.
<!DOCTYPE html>
<html>
<head>
<style>
body { background: #e6ebf5; font: 16px sans-serif; }
.page {
width: 260px;
margin: 20px auto;
background: white;
padding: 16px;
border-radius: 8px;
text-align: center;
}
.push-right {
width: 140px;
margin-left: auto;
background: gold;
padding: 8px;
}
</style>
</head>
<body>
<div class="page">Centered with margin: 20px auto</div>
<div class="push-right">margin-left: auto</div>
</body>
</html>margin-left: auto on its own takes all the free space on the left, pushing the box to the right edge.
Margin collapse
Here is the most surprising margin rule: when two vertical margins of block elements touch, they do not add up. They collapse into a single margin equal to the larger of the two.
<!DOCTYPE html>
<html>
<head>
<style>
body { font: 16px sans-serif; }
.a, .b { padding: 10px; color: white; }
.a { background: crimson; margin-bottom: 40px; }
.b { background: seagreen; margin-top: 20px; }
</style>
</head>
<body>
<div class="a">margin-bottom: 40px</div>
<div class="b">margin-top: 20px</div>
<p>The gap above is 40px, not 60px.</p>
</body>
</html>Margin collapsing happens only:
- with vertical (top/bottom) margins, never horizontal ones;
- between normal-flow block elements (not flex or grid items, not floats, not absolutely positioned elements);
- also between a parent and its first or last child, if nothing (border, padding, content) separates them.
That last case explains a classic puzzle: you give the first child a margin-top and the parent moves down instead. Adding a little padding or a border to the parent, or making it a flex container, stops the collapse.
Negative margins
A negative margin pulls an element toward its neighbor, or even overlapping it. It is useful for small visual adjustments, such as overlapping an avatar onto a banner.
<!DOCTYPE html>
<html>
<head>
<style>
body { font: 16px sans-serif; }
.banner { height: 100px; background: linear-gradient(90deg, #264de4, #00b4d8); }
.avatar {
width: 80px; height: 80px;
margin: -40px auto 0;
border-radius: 50%;
border: 4px solid white;
background: #ffd166;
font-size: 44px;
display: grid; place-items: center;
}
p { text-align: center; }
</style>
</head>
<body>
<div class="banner"></div>
<div class="avatar">🐼</div>
<p>margin-top: -40px pulls the avatar up</p>
</body>
</html>Tip: For spacing items inside a flex or grid container,
gapis often cleaner than margins: it puts space only between items, never on the outer edges.
Test yourself
What does margin: 10px 20px; set?
- Top 10px, all others 20px
- Top/bottom 10px, left/right 20px
- Left/right 10px, top/bottom 20px
- Top 10px, right 20px, bottom and left 0
A block with margin-bottom: 30px sits above one with margin-top: 50px. How big is the gap?
- 80px
- 50px
- 30px
- 20px
How do you horizontally center a block with width: 300px?
margin: auto 0;text-align: center;margin: 0 auto;align: center;