Skip to main content

How to Add Space Outside an Element with margin in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<selector> {
margin: <size>;
}
PartWhat 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:

index.html, an HTML page with a style element holding a rule for p with the background fff4ce and a margin of 24px, and three paragraphs of notices.

index.html
<!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 page in Chromium: three yellow bands, each set in from the left and right edges, with white space between them.

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​

MistakeWhat happensDo this instead
Expecting the margins of two paragraphs to add upThey collapse into the larger one: 24 pixels, not 48Set the gap you want as one margin
Mixing up the order of four valuesFour values go top, right, bottom, left: 10px 20px 30px 40px put 40 pixels on the leftRead the values clockwise from the top

Troubleshooting​

SymptomLikely causeFix
The space between elements is smaller than both margins togetherMargin collapsingUse one margin of the full size
The background doesn't cover the spaceMargin is outside the backgroundUse 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​