Favorites

CSS Margin vs Padding Guide

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

CSS Margin Padding

One of the most common CSS questions is whether to use margin or padding. Both add space, but in different places: margin adds space outside an element, separating it from its neighbours, while padding adds space inside it, between its edge and its content. In this guide we'll look at how each one works, when to use which, and the quirks that catch people out.

For full details of each property, see our reference pages for the CSS margin property and the CSS padding property.

The box model

Every element on a web page is a rectangular box, made up of four layers, from the inside out:

  1. Content: the text, image or other content itself.
  2. Padding: space between the content and the border.
  3. Border: a line around the padding (which may be invisible).
  4. Margin: space outside the border, separating the element from others.

Together, these layers are called the box model. Here it is drawn with CSS, in the same colours your browser's developer tools use:

Results
Margin
Border
Padding
Content

Tip: to see the box model of any element, right-click it, choose Inspect, and look for the box diagram in the developer tools. Hovering over an element there highlights its margin, border, padding and content in these colours.

What is margin?

Margin is the space outside an element's border. It pushes other elements away, and it's always transparent: the element's background doesn't extend into it.

You can set each side separately with margin-top, margin-right, margin-bottom and margin-left, or use the margin shorthand:

margin: 20px;                /* all four sides */
margin: 10px 20px;           /* top and bottom | left and right */
margin: 5px 10px 15px;       /* top | left and right | bottom */
margin: 5px 10px 15px 20px;  /* top | right | bottom | left (clockwise) */

Margins can also be negative, which pulls an element towards its neighbours instead of pushing them away, and auto, which takes up all the available space. That's why margin-inline: auto centres a block that has a set width:

.container {
  max-width: 960px;
  margin-inline: auto;   /* same as margin-left: auto; margin-right: auto; */
}

What is padding?

Padding is the space inside an element, between its content and its border. The element's background colour and background image fill the padding, so padding makes the visible box bigger.

Padding uses exactly the same shorthand patterns as margin:

padding: 20px;                /* all four sides */
padding: 10px 20px;           /* top and bottom | left and right */
padding: 5px 10px 15px;       /* top | left and right | bottom */
padding: 5px 10px 15px 20px;  /* top | right | bottom | left */

Unlike margin, padding can't be negative or auto.

How padding affects size

By default, an element's width only sets the width of its content. Padding and borders are added on top, so the box ends up wider than you asked for. The box-sizing property changes this:

.content-box {
  box-sizing: content-box;   /* the default */
  width: 200px;
  padding: 20px;
  border: 4px solid;         /* total width: 200 + 40 + 8 = 248px */
}

.border-box {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 4px solid;         /* total width: 200px, padding and border included */
}
Results
content-box: 248px wide
border-box: 200px wide

Most developers switch every element to border-box at the start of their stylesheet, so widths behave predictably:

*,
*::before,
*::after {
  box-sizing: border-box;
}

Margin vs padding at a glance

Here are the same two boxes with 24px of space added in different ways. The striped area is the container around each box:

Results

padding: 24px

Text

margin: 24px

Text

Padding makes the coloured box itself bigger, while margin moves the box away from the container's edges and leaves the gap uncoloured.

FeatureMarginPadding
LocationOutside the borderInside the border
Background fills it?No, always transparentYes
Adds to the element's size?No, it adds space around itYes, unless box-sizing: border-box is used
Clickable?NoYes, it's part of the element
Can collapse?Vertical margins can, in normal block layoutNever
Negative values?YesNo
auto?Yes, useful for centring and pushing items apartNo

When to use which

Use margin when…

  • You need space between elements, such as between sections or paragraphs.
  • You want to centre a block with margin-inline: auto.
  • You want to push one flex item away from the others, for example a "Log in" link pushed to the far end of a navigation bar with margin-left: auto.
  • You need a small overlap, using a negative margin.

Use padding when…

  • Content needs breathing room inside a card, alert or panel.
  • You want a bigger clickable area. Padding on buttons and links makes them easier to tap, which especially helps on touch screens and for people with limited dexterity.
  • The background should extend around the content, as with badges, buttons and highlighted blocks.

Consider gap for rows and grids

For spacing between items in a flex or grid layout, the gap property is often simpler than margins. It only adds space between items, never at the outer edges, so there's no need to remove a margin from the last item:

.card-list {
  display: grid;
  gap: 24px;   /* space between cards only */
}

Rule of thumb: if the space belongs to the element (inside its background), use padding. If it's space between things, use margin, or gap in flex and grid layouts.

Margin collapsing

The most surprising thing about margins is margin collapsing. When the vertical margins of two block elements touch, they don't add together. Instead, they combine into a single margin the size of the larger one.

.first {
  margin-bottom: 30px;
}

.second {
  margin-top: 20px;
}

/* In normal block layout, the gap between them is 30px, not 50px */
Results

Block layout: gap is 30px

margin-bottom: 30px
margin-top: 20px

Flex column: gap is 50px

margin-bottom: 30px
margin-top: 20px

Collapsing only happens in normal block layout. As the right-hand example shows, inside a flex or grid container the margins are added together.

Where collapsing happens

  • Between neighbours: one element's bottom margin and the next element's top margin, as above.
  • Between a parent and its first or last child: if nothing separates them (no padding, border or content), a child's top margin can "escape" out of its parent and push the parent down instead.
  • Through empty elements: an empty block's own top and bottom margins collapse together.

How to prevent it

  • Give the parent some padding or a border.
  • Make the parent a new layout context with display: flow-root, which exists for exactly this kind of job and has no side effects.
  • Use a flex or grid layout, where margins never collapse, or use gap instead of margins.

Horizontal margins never collapse.

Advanced tips and gotchas

Negative margins

Negative margins pull elements towards each other, or make them overlap. They're handy for small adjustments, such as letting an image stretch to the edges of a padded card:

.card {
  padding: 20px;
}

.card img {
  margin-inline: -20px;   /* cancel out the card's padding */
  width: calc(100% + 40px);
}

Use them sparingly, as heavy use can make layouts hard to predict at different screen sizes.

Percentages use the width

Percentage margins and padding, including padding-top and padding-bottom, are calculated from the width of the containing block, not its height. That's why padding-top: 56.25% was once a popular trick for keeping video boxes at a 16:9 shape. Today, the aspect-ratio property does that directly:

.video {
  width: 100%;
  aspect-ratio: 16 / 9;
}

Inline elements and vertical spacing

On inline elements such as <span>, <a> and <strong>, vertical margins have no effect at all, and vertical padding is drawn but doesn't push the surrounding lines apart, so it can overlap them. Horizontal margin and padding work as normal. In the first line below, the highlighted words have 12px of vertical padding and margin; in the second, they're inline-block:

Results

This line has inline words with vertical padding and margin, which overlap the lines around them instead of pushing them apart, as you can see here.

This line has inline-block words, which take up real vertical space, so the lines move apart to fit them.

If an inline element needs real vertical space, such as a link styled as a button, give it display: inline-block.

Logical properties

Modern CSS also has logical versions of margin and padding, which follow the direction of the text rather than the physical sides of the screen:

margin-block: 20px;      /* top and bottom, in English */
margin-inline: auto;     /* left and right, in English */
padding-inline-start: 1rem;  /* left in English, right in Arabic or Hebrew */

They're shorter to write for pairs of sides, and they adapt automatically to right-to-left languages.

Wrapping up

Margin is space outside an element's border, padding is space inside it. Use padding for breathing room and bigger click areas within an element, and margin, or gap in flex and grid layouts, for space between elements. Set box-sizing: border-box so padding doesn't change your widths, watch out for vertical margins collapsing in block layout, and reach for inline-block when inline elements need vertical space.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.