Menu

Margin vs Padding in CSS: The Difference, Side by Side

Padding is space inside an element's border; margin is space outside it. See both on the same box, side by side and live, then learn which to use for backgrounds, click areas, spacing between elements, and centering.

This page includes runnable editors - edit, run, and see output instantly.

The difference in one picture

Padding is inside the border. Margin is outside it. Both boxes below have the same content and the same 24px value; only the property differs. The gray is the container behind them.

With padding, the 24px is blue: it is part of the box, inside the border, and the box grows. With margin, the 24px is gray: the box stays tight around its text and the space outside pushes the container's edges away.

Try both on one box

Move the two sliders. Padding grows the blue box; margin moves the box away from the gray edges without changing it.

Watch the size line: it changes when you move the padding slider and stays the same when you move the margin slider.

Margin vs padding at a glance

PaddingMargin
WhereInside the borderOutside the border
BackgroundShows the element's backgroundAlways transparent
Part of the click areaYesNo
Adds to the element's sizeYes, under content-boxNo
Can be autoNoYes (margin: 0 auto centers)
Can be negativeNoYes
Vertical values collapseNeverYes, adjacent vertical margins collapse
Percentages relative toContaining block's widthContaining block's width
Typical useSpace inside buttons, cards, inputsSpace between elements, centering

Each property has its own page with every detail: CSS padding and CSS margin.

Which one to use

Ask one question: does the space belong to the element, or sit between elements?

  • Room around a button's label, the inside of a card, space between an input's text and its border: padding. It takes the background and makes the target bigger.
  • Space between two paragraphs, between cards, around a section: margin, or gap when the parent is a flex or grid container.

The click area is where the choice matters most. The two links below take up the same room on the page, but the first one's space is padding and the second one's is margin. Only the first is easy to hit:

The dashed amber outline is the element's edge, which is the area that responds to clicks and taps. The margin around the second link is empty space that does nothing.

Spacing between items: margin or gap

For a row or grid of items, gap on the flex or grid parent is usually cleaner than margins on each child: it only goes between items, never before the first or after the last, so there is nothing to undo.

Here the gray container uses padding (space inside it, gray), the items use padding (space inside each, green), and gap handles the space between them.

Common mistakes

  • Using margin for space that should be colored. Margin is transparent. If the space should show the background, it is padding.
  • Using margin to make a link bigger. The click area ends at the border; use padding.
  • Expecting two 20px vertical margins to make 40px. Adjacent vertical margins collapse to 20px. Padding never collapses.
  • Trying padding: auto to center. Only margin accepts auto.
  • Forgetting that padding grows the box. Add box-sizing: border-box so width includes it.

Frequently Asked Questions

What is the difference between margin and padding?

Padding is the space between an element's content and its border, inside the element. Margin is the space outside the border, between the element and its neighbors. Padding shows the element's background; margin is always transparent.

When should I use padding instead of margin?

Use padding for space that belongs to the element: room around a button's text, the inside of a card, anything that should share the background or be clickable. Use margin (or gap in flex and grid) for space between separate elements.

Does padding or margin increase the element's size?

Padding does, under the default box-sizing: content-box: it is added to the width and height. Margin is never added to the element's size; it adds space around it (a block with width: auto even gets narrower, because its margins take part of the available width). With box-sizing: border-box, padding is taken from inside the width instead.

Can padding be negative like margin?

No. Negative padding is invalid and ignored, while negative margins are allowed and can make elements overlap. Padding also cannot be auto, which is why centering uses margin: 0 auto.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED