Learn Margin in CSS

A margin is the space outside an element's border. Use margin-top, margin-right, margin-bottom or margin-left for one side, or margin with one to four values…

The heading and the paragraph on your page feel too close. What you need isn't a bigger box. You need more space between the two.

That space is called a margin: the empty space outside an element's edge line (its border), between that element and the ones around it. A margin works like the distance you keep in a checkout queue: you don't get any bigger, you only stand further from other people. An element with a margin also keeps its size and only moves further from other elements.

Space on one side

This example gives a paragraph space on its top side:

<h1>Title</h1>
<p>Paragraph</p>

<style>
p {
  margin-top: 40px;
}
</style>

The result: the space above the paragraph becomes 40px, so the paragraph moves down, away from the heading. Why not 20px? The <h1> already has a default bottom margin of about 21px. Margins that meet aren't added: the browser uses the bigger one, so a 20px top margin on the paragraph changes nothing. The section When two margins meet explains why. The other three sides have properties of their own: margin-right, margin-bottom and margin-left.

The selector has to select the element you want to move. If you write h1 > p, you select a <p> that is directly inside an <h1>. In this example the <p> comes after the <h1>, not inside it, so that rule applies to no element at all.

Four sides in one line

The margin property, with no side in its name, can set all four sides at once. The number of values decides which side gets which value:

.a { margin: 40px; }                 /* every side 40px */
.a { margin: 40px 32px; }            /* top-bottom 40px, right-left 32px */
.a { margin: 40px 32px 24px; }       /* top, right-left, bottom */
.a { margin: 40px 32px 24px 16px; }  /* top, right, bottom, left */

Four values go clockwise, starting at the top. One value, like margin: 20px, is the same as margin-top: 20px plus 20px on the other three sides.

The number always needs a unit: 20px, not 20. The browser doesn't understand a number without a unit, so it throws that declaration away. Only 0 may be written without a unit.

Centring with auto, and negative margins

This rule puts a card in the middle of its parent:

.card {
  width: 300px;
  margin: 0 auto;
}

margin: 0 auto uses two values: 0 on the top and bottom, auto on the right and left. With auto, the browser shares the leftover space equally between the left and the right of the box, so the box sits exactly in the middle. One condition: the box needs a width smaller than its parent's width. Without a width, the box is already as wide as its parent, so no space is left over.

A margin can also be negative, for example margin-top: -10px. A negative top margin pulls the element up, so it gets closer to the element above it or overlaps it. Padding can't be negative.

When two margins meet

The top paragraph has a 30px bottom margin, and the paragraph below it has a 20px top margin. Guess first: how big is the gap between the two paragraphs?

.top    { margin-bottom: 30px; }
.bottom { margin-top: 20px; }

The answer is 30px, not 50px. When one element's bottom margin meets the top margin of the element below it, the browser doesn't add them. It only uses the bigger one. This is called margin collapse, and it only happens to top and bottom margins, never to left and right margins. One exception: inside a parent that uses display: flex or display: grid (two ways to lay out a page, covered later), margins don't collapse, so the gap really is 50px.

Three ways to add space:

⚠️ Margins that do nothing, and gaps that appear by themselves

TL;DR: A margin is the space outside an element's border. Use margin-top, margin-right, margin-bottom or margin-left for one side, or margin with one to four values (four values go clockwise from the top). The value needs a unit, like 20px. margin: 0 auto puts a box that has a width in the middle of its parent. A negative margin pulls an element closer to the element next to it. A bottom margin and a top margin that meet aren't added: the browser uses the bigger one.

⚡ Space on one side

Write:

p {
  margin-top: 40px;
}

Meaning: A margin is the empty space outside an element's border. margin-top sets the top side only: every <p> gets 40px of space above it. The value needs a unit: 40px, not 40. Only 0 may go without one.

⚡ Four sides, clockwise

Write:

.a { margin: 40px 32px 24px 16px; }

Meaning: The margin property sets all four sides at once. Four values go clockwise from the top: top, right, bottom, left. With two values, the first is for top and bottom, the second for right and left.

⚡ Centring with auto

Write:

.card {
  width: 300px;
  margin: 0 auto;
}

Meaning: margin: 0 auto means 0 on the top and bottom, auto on the right and left. With auto, the browser shares the leftover space equally between left and right, so the box sits exactly in the middle of its parent. One condition: the box needs a width smaller than its parent.

⚡ When two margins meet

Guess first: the top paragraph has a 30px bottom margin, and the paragraph below it has a 20px top margin. How big is the gap between them?

30px, not 50px. The browser only uses the bigger margin.

Meaning: This is called margin collapse, and it only happens to top and bottom margins, never to left and right margins. Inside a parent that uses display: flex or display: grid (two ways to lay out a page), margins don't collapse: the gap is 50px.

What you will learn