ComparisonComparisons

margin vs padding

margin is transparent space outside the element. padding is space inside the border, between the content and the edge, and it takes on the element’s background.

On this page

Side-by-side comparison

#
  • Where it sits relative to the border

    margin
    Outside
    padding
    Inside, around the content
  • Shows the element’s background

    margin
    No, always transparent
    padding
    Yes, it takes on the element’s background and color
  • Negative values

    margin
    Allowed, pull elements closer or make them overlap
    padding
    Not allowed
  • Merging with a neighbor (collapsing)

    margin
    Vertical margins of neighboring blocks sometimes merge
    padding
    Never merges
  • Centering a block

    margin
    margin: 0 auto centers a block with a known width
    padding
    Not used for centering
  • Typical use

    margin
    Spacing between cards, paragraphs, list items
    padding
    Spacing inside buttons, fields, cards

Example

#
CSS
.card {
  margin-bottom: 16px;
  padding: 16px;
  border: 1px solid #3f3f46;
  border-radius: 10px;
}
Preview

Definition and usage

#

Both properties add free space, but at a different point in the box model. margin sits outside the element’s border and separates it from its neighbors, so it’s always transparent, and you can see through it to whatever is underneath. padding sits inside the border, between the content and the edge, so it inherits the element’s background and color: a button with padding "breathes," because the background stretches along with it.

The difference also shows up in behavior. margin can be negative, which pulls elements closer together or makes them overlap, and the vertical margins of two neighboring blocks sometimes merge into one, larger gap (collapsing). padding is never negative and never merges with anything.

In practice, the choice is simple: the space between cards, paragraphs or list items is margin, or, even better inside flex and grid containers, the gap property. The space between the content of a button, a field or a card and its edge is padding.

Syntax

#
Syntax
margin: <top> <right>? <bottom>? <left>?;
padding: <top> <right>? <bottom>? <left>?;

More examples

#
A negative margin overlaps elements
CSS
.avatar {
  margin-left: -12px;
  border: 2px solid #0c0c10;
}

.avatar:first-child {
  margin-left: 0;
}
Preview

A negative margin overlaps the avatars, like the popular "who’s online" pattern.

Too little padding makes clicking harder
CSS
.button-tight {
  padding: 2px 4px;
}

.button-comfy {
  padding: 10px 18px;
}
Preview

A tappable area on a phone should be at least about 44px, and padding is exactly what decides that.

Good practices

#
  • Set the spacing between list items, cards or form fields through the container’s gap, instead of margins on every element.
  • If an element "disappears" under its neighbor or grows too large, check padding first: with box-sizing: content-box it enlarges the element beyond width.
  • Margin between sections can be unpredictable because of collapsing. When in doubt, check the result in the developer tools.

Related entries

#

Spotted a mistake or missing an example? Write to us.