Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 139 ยท The Box Model

Margins

Learn CSS margins from the shorthand to auto centering and negative margins, plus the exact rules of margin collapsing and how to stop it when you need to.

Margins are the space outside an element's border. They push neighbors away, they are always transparent, and they are the main tool for spacing blocks apart in normal page flow.

Margins also have a few behaviors no other part of the box has. They can be negative. They can be auto, which is how you center a block. And vertical margins sometimes collapse: two margins that meet merge into one. Margin collapsing is the source of more confused "why is there a gap here?" questions than almost anything else in CSS, so this lesson explains its rules precisely.

Setting margins

There is one property per side:

CSS
.note {
  margin-top: 16px;
  margin-right: 24px;
  margin-bottom: 16px;
  margin-left: 24px;
}

The margin shorthand sets all four at once. Like padding and border widths, it takes one to four values, and they go clockwise from the top: top, right, bottom, left.

Shorthand Top Right Bottom Left
margin: 10px 10 10 10 10
margin: 10px 20px 10 20 10 20
margin: 10px 20px 30px 10 20 30 20
margin: 10px 20px 30px 40px 10 20 30 40

When a value is missing, it copies the opposite side: left copies right, and bottom copies top.

Margins accept lengths, percentages and auto. A percentage margin is a share of the containing block's width, even for margin-top and margin-bottom. That surprises people, but it means a 5% margin is the same on every side.

Logical margins

Modern CSS also has margin-block (top and bottom in English) and margin-inline (left and right), each taking one or two values:

CSS
.stack > * {
  margin-block: 0 1rem;   /* top 0, bottom 1rem */
}

.page {
  margin-inline: auto;    /* left and right auto */
}

They follow the writing direction, so a right-to-left page or vertical text gets the right sides automatically. You will learn more in Writing Modes and Logical Properties.

Centering with margin: auto

When a block has a width smaller than its container, there is leftover horizontal space. Setting the left and right margins to auto tells the browser to split that space equally, which centers the block:

CSS
.page {
  max-width: 40rem;
  margin-inline: auto; /* or: margin: 0 auto; */
}

This only works on block boxes with a width (or max-width) narrower than their container. It does not center inline content like text (use text-align: center for that), and on a normal block it does not center vertically, because auto top and bottom margins become zero in normal flow. Inside Flexbox and Grid, though, auto margins absorb free space in both directions, which you will see in Centering Anything.

Setting just one side to auto pushes the block to the opposite side: margin-left: auto moves a block to the right.

Centering and pushing with auto

Negative margins

A negative margin pulls in the opposite direction:

  • A negative margin-top or margin-left moves the element up or left.
  • A negative margin-bottom or margin-right pulls the following content closer, so it may overlap the element.
  • On a block with width: auto, negative left and right margins make the box wider than its container.

That last behavior is handy for a "full-bleed" image that breaks out of a padded card:

Breaking out of padding with negative margins

Negative margins are legitimate, but they are easy to overuse. If you find yourself fighting the layout with several of them, Flexbox or Grid usually solves the problem more cleanly.

Margin collapsing

Here is the rule in one sentence: when the vertical margins of block boxes in normal flow touch, with nothing between them, they combine into a single margin. The rest of this section makes "touch" and "combine" precise.

How the size is worked out

When margins collapse, the browser does not add them. It works out the result like this:

  • All positive: the largest wins. Margins of 30px and 20px give a 30px gap, not 50px.
  • All negative: the most negative wins. -10px and -25px give -25px.
  • Mixed: the largest positive plus the most negative. 30px and -10px give 20px.

When margins touch

There are three situations where this happens.

1. Adjacent siblings. The bottom margin of one block and the top margin of the next block collapse. This is why p { margin: 1em 0; } gives you one em between paragraphs, not two.

2. A parent and its first or last child. A parent's top margin collapses with its first child's top margin when nothing separates them: no top border, no top padding, no inline content (text) before the child, and the parent does not start a new layout context (see below). The same goes for the bottom margin and the last child, with the extra condition that the parent's height is auto. The visible effect is surprising: the child's margin seems to leak out of the parent, pushing the parent down instead of creating space inside it.

3. An empty block. A block with no content, padding, border or height lets its own top and bottom margins collapse together, and they can then collapse with the margins around it.

Collapsing can chain: a child's margin can collapse with its parent's, which collapses with the parent's sibling, all into one margin.

The example below shows the first two cases and measures the real gaps with getBoundingClientRect():

Measure collapsing margins

The gap between A and B is 30 pixels: the larger margin wins. The child's 40-pixel margin collapses with the parent's (zero) top margin, so it ends up above the yellow parent, and the child sits flush with the parent's top edge. Add a 1-pixel padding or border to the parent and the margin moves back inside.

When margins do not collapse

Margins never collapse in these cases:

  • Horizontal margins. Only margins in the block direction (vertical, in English) collapse.
  • Flex and grid items. Margins of items inside a flex or grid container never collapse, with each other or with the container.
  • Floats and absolutely positioned elements.
  • Inline-block elements. They are not block boxes in normal flow.
  • A parent that starts a new block formatting context does not collapse with its children. display: flow-root creates one with no side effects; overflow set to anything other than visible or clip also does.
  • Anything separating them: padding, border, inline content or a clearance from a float.

Fixing unwanted collapsing

When a child's margin leaks out of its parent, you have several fixes. From cleanest to least clean:

CSS
/* 1. Make the parent its own layout context */
.parent { display: flow-root; }

/* 2. Separate them with padding (often you wanted padding anyway) */
.parent { padding-top: 1rem; }

/* 3. Use flex or grid with gap instead of margins */
.parent { display: flex; flex-direction: column; gap: 1rem; }
Three ways to stop the leak

Only the first yellow box is missing its yellow strip above and below the blue child. In the others, the margins stay inside.

Many teams avoid collapsing surprises by spacing elements in one direction only: give elements a bottom margin and never a top margin (or the reverse). Inside components, gap in Flexbox or Grid is even simpler, because gaps never collapse and never leak.

Margin or padding?

Use margin for space between elements, and padding for space inside an element, between its border and its content. The next lesson, Padding, compares the two side by side.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does margin: 5px 10px 15px set?

    Three values mean top, left-and-right, bottom. The missing left value copies the right one.
  2. 2. A paragraph with margin-bottom of 24px is followed by a heading with margin-top of 32px. How big is the gap?

    Adjacent vertical margins of block siblings collapse, and with two positive margins the larger one wins: 32 pixels.
  3. 3. A child's margin-top appears above its parent instead of inside it. Which change does NOT fix this?

    Horizontal margins have nothing to do with collapsing. Padding, a border, flow-root or a flex container all stop the parent and child margins from touching.
  4. 4. Margins of 40px and -15px collapse. What is the result?

    With mixed signs, the largest positive margin is added to the most negative one: 40 + (-15) = 25 pixels.
  5. 5. Why does margin: 0 auto not center a <span>?

    Auto margins share leftover space in a block's container. An inline box has no such space; center text with text-align: center on the parent, or make the element a block with a width.

Summary

  • The margin shorthand takes one to four values, clockwise from the top; missing sides copy their opposite.
  • Percentage margins, even top and bottom, are based on the containing block's width.
  • margin-inline: auto centers a block that is narrower than its container.
  • Negative margins pull elements closer and can widen an auto-width block.
  • Vertical margins of blocks in normal flow collapse between siblings, between a parent and its first/last child, and through empty blocks. The largest positive plus the most negative wins.
  • Collapsing never happens horizontally, in flex or grid layouts, or across padding, borders or a new formatting context such as display: flow-root.

Next: Padding, the space inside the border.