Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

Margin vs Padding: When to Use Which?

By · Updated

Disclosure: this page contains Amazon affiliate links. As an Amazon Associate we earn from qualifying purchases, at no extra cost to you.

Hey there, space planner!

Margin vs padding comes down to one line, the border: padding is the cushy room inside it, margin is the elbow room outside it, and knowing which side you need makes every button and card behave.

Margin and padding both add space, so it’s easy to reach for the wrong one and wonder why the button still feels cramped.

If you like a printed reference next to the keyboard, the CSS Visual Dictionary paperback on Amazon draws both of them as pictures.

Short answer: Use padding for space inside an element’s border: it shows the background and is part of the click area, like the room around a button’s text. Use margin for space outside the border, between an element and its neighbours. In flex and grid layouts, use gap for the space between items.

To-scale diagram comparing margin and padding: two buttons labelled Save and Share, each with padding 12px 20px, a 2px border and a 10px margin, drawn at two units per CSS pixel. The green padding sits inside the border and is filled by the button background and clickable; the hatched orange margin sits outside, transparent and not clickable, and the two side margins add up to a 20 pixel gap. Below, two cards with 20px of inner padding and 24px of space between them.
Padding lives inside the border (it takes the background and the click), margin lives outside it (transparent space between things).

What the diagram shows, in plain text (.btn { padding: 12px 20px; border: 2px solid; margin: 10px; } around a 60 x 20px label):

What is the difference between margin and padding?

Padding is space inside an element’s border, and margin is space outside it. MDN Web Docs’ margin reference sums it up in one line:

Margins create extra space around an element, unlike padding, which creates extra space within an element.

The web.dev Learn CSS spacing module uses two handy pictures. Margin is like adding a cushion around your element, while padding creates space on the inside of your box instead: like insulation.

That inside-or-outside position is why the two behave so differently. The W3C CSS Box Model Module Level 3 paints backgrounds inside the padding edge and states that Margins are always transparent.

Padding vs margin, behaviour by behaviour — compiled by Learn CSS Visually from the W3C CSS Box Model Module Level 3, CSS 2.1 and MDN Web Docs, November 20, 2026
QuestionPaddingMargin
Where is the space?inside the borderoutside the border
Shows the background colour or image?yesnever
Part of the click or tap area?yesno
Can it be negative?no (invalid)yes (pulls boxes closer or overlaps them)
Can it be auto?noyes (centering, pushing to the far end)
Can it collapse?nevertop and bottom margins of blocks can
Makes an empty element taller?yesno
Percentages measured fromthe parent’s widththe parent’s width

The last row surprises people: even padding-top and margin-top percentages use the containing block’s width. Our CSS box model guide draws all four layers to scale if you want the full picture.

How do I decide between margin and padding?

Ask whether the space belongs to the element or sits between elements. If it should share the element’s background or be clickable, it’s padding; if it separates two things, it’s margin, or gap inside flex and grid.

Decision diagram with four questions for choosing CSS spacing. One: should the space show the element's background or be part of the click area? Yes means padding. Two: is it space between items of one flex or grid container? Yes means gap. Three: do you need negative or auto space? Yes means margin. Four: otherwise it is space between this box and its neighbours, so use margin on one side, such as margin-block-end.
Four questions pick the right spacing property: padding, gap or margin.

The four questions in the diagram, in order:

  1. Should the space show the element’s background, or be part of what people click or tap? Use padding.
  2. Is it space between items of one flex or grid container? Use gap.
  3. Do you need negative space (an overlap) or auto space (centering, pushing to the far end)? Use margin; only margin can do that.
  4. Still no? It’s space between this box and its neighbours in normal flow. Use margin, on one side only.

The CSS Working Group writes the same split into the W3C CSS Box Model Module Level 4 draft (4 August 2024):

Oftentimes, margins are desired between siblings, but not at the start/end of the container where spacing can be controlled with padding.

Should I use margin or padding for buttons, cards and nav links?

Buttons and cards get padding on the inside; the space between buttons, cards and nav links is gap when their parent is flex or grid, and margin otherwise. Here are the 12 spacing jobs that come up most:

Margin, padding or gap for 12 common layout jobs — compiled by Learn CSS Visually from the W3C CSS Box Model specs, MDN, web.dev and Chrome’s Lighthouse docs, November 20, 2026
The jobUseWhy
Room around a button’s textpaddingthe button colour fills it and it’s clickable
Bigger tap area for an icon or linkpaddinggrows the hit area without changing the icon
Text not touching a card’s edgepadding on the cardthe space belongs to the card’s background
Space between cards in a row or gridgaponly between items, never at the outer edges
Space between nav linksgap on the flex list + padding on each linkgap separates them, padding makes each easy to tap
Space between paragraphsmargin, one directionspace between siblings in normal flow
Space under a headingmarginseparates two elements
Center a fixed-width blockmargin-inline: autopadding can’t be auto
Push one nav item to the far rightmargin-left: auto in flexauto margins take the free space
Avatar overlapping a card’s top edgenegative marginpadding can’t be negative
Coloured section with breathing roompadding on the sectionthe colour must reach the edges
Keep content off the screen edges on phonespadding on the page wrapperspace at a container’s own edges is its padding

Does padding make a button or link bigger to click?

Yes. Padding is part of the element, so it grows the area people can click or tap, while margin adds space around that area without making it any bigger.

Chrome’s Lighthouse tap targets documentation gives the two fixes side by side. For targets that are too small: Use padding to make tap targets bigger without changing the appearance of an element. For targets too close together: Increase the spacing between tap targets that are too close together using properties like margin.

Three size lines matter. Lighthouse says Tap targets that are 48 px by 48 px never fail the audit. web.dev’s Accessible tap targets adds that a 48 x 48px area corresponds to around 9mm, which is about the size of a person’s finger pad area.

The accessibility standard itself sets lower floors. WCAG 2.2 success criterion 2.5.8 Target Size (Minimum), Level AA, asks for targets of at least 24 by 24 CSS pixels, and 2.5.5 Target Size (Enhanced), Level AAA, asks for 44 by 44.

Here’s what padding and margin do to a 16 x 16px icon button in a row of identical buttons:

Clickable area of a 16 x 16px icon button with different padding and margin — calculated by Learn CSS Visually from WCAG 2.2 (2.5.8, 2.5.5) and Chrome’s Lighthouse tap-target docs, November 20, 2026
Padding / margin (each side)Clickable areaSpace between buttonsWCAG 2.5.8 (24px, AA)44px (AAA)48px (Lighthouse)
0 / 016 x 16px0pxfailsnono
0 / 5px16 x 16px10pxpasses through the spacing exception onlynono
4px / 024 x 24px0pxpasses by sizenono
14px / 044 x 44px0pxpassesyesno
16px / 048 x 48px0pxpassesyesyes
16px / 4px48 x 48px8pxpassesyesyes, plus web.dev’s 8px spacing

Notice the second row: 5px of margin on each side never makes the 16px icon any bigger to hit. Only the padding rows move the clickable area.

A nav menu puts both to work. The links get padding so each one is a comfortable target, and the flex list gets gap so they never touch:

nav ul {
  display: flex;
  gap: 8px;            /* space between links */
  list-style: none;
  margin: 0;
  padding: 0;
}
nav a {
  display: block;      /* so vertical padding counts */
  padding: 12px 16px;  /* the clickable area grows */
}

web.dev suggests going bigger only where fingers are used, with an interaction media query: @media (any-pointer: coarse) { .container a { padding: .8em; } }.

Does margin count toward the WCAG target size?

No. Margin isn’t part of the target, so it never adds to the 24 x 24px size, but spacing can still pass WCAG 2.5.8 through its Spacing exception for targets that are smaller than 24px.

The exception says undersized targets pass when a 24 CSS pixel diameter circle is centered on the bounding box of each and the circles do not intersect another target or the circle for another undersized target.

In the table’s second row, 5px of margin on each side puts the 16px icons 10px apart, so their centres are 16 + 10 = 26px apart. Two 24px circles 26px apart don’t touch, which is how margin can help even though padding is the real fix.

Links inside a sentence get their own exception: WCAG 2.5.8 also passes when The target is in a sentence or its size is otherwise constrained by the line-height of non-target text. So you don’t need to pad every link in a paragraph.

Should I use margin or gap between flex and grid items?

Use gap. It puts space only between items, never before the first or after the last, so rows stay flush with their container and you don’t need :last-child fixes.

MDN’s gap reference says it sets the gaps (also called gutters) between rows and columns on multi-column, flex, and grid containers. web.dev points out that margins on every item also might add additional spacing that you don’t want.

Here is the difference for three equal cards (flex: 1) in a 960px row, 24px apart:

Three flex: 1 cards in a 960px container — calculated by Learn CSS Visually from the CSS Flexbox and Box Alignment specs, November 20, 2026
Spacing CSSEach cardSpace between cardsAt the container’s edges
gap: 24px on the row(960 — 48) / 3 = 304px24px0px, flush
margin: 0 12px on each card(960 — 72) / 3 = 296px12 + 12 = 24px12px inset on both sides
margin-right: 24px on each card(960 — 72) / 3 = 296px24px0px left, 24px right (lopsided)

gap works in flexbox from Chrome and Edge 84, Firefox 63 and Safari 14.1, about 97.27% of users on Can I use. See how flexbox works and how CSS Grid works for the containers themselves.

How do I space paragraphs and headings in an article?

Give the blocks a margin in one direction only, such as the top margin of every element that follows another. That keeps the rhythm even and avoids surprises from margins collapsing at both ends.

The best-known version is the “lobotomized owl” selector from Heydon Pickering’s Axiomatic CSS and Lobotomized Owls (A List Apart, October 21, 2014). Its * + * matches every element that comes right after a sibling:

.flow > * + * {
  margin-block-start: 1.5em;   /* space above every element except the first */
}

Scoping it to a .flow wrapper keeps it inside article text, where stacked blocks belong. web.dev’s advice fits here too: It is a really good idea to choose a strategy and stick with it.

Top and bottom margins between ordinary blocks collapse into the larger one, so margin-bottom: 30px above margin-top: 20px leaves 30px, not 50px. One-direction spacing sidesteps that, and our box model guide draws the collapsing rules.

Why use padding on the container instead of margin on the first child?

Because space at a container’s own edges belongs to the container, and a first child’s top margin can escape a parent that has no padding or border, pushing the whole parent down instead of making space inside it.

Real talk: padding the list items of a menu and then clicking right next to a link with nothing happening is a moment almost everyone hits. Moving that padding onto the link itself, so the whole comfy area is clickable, feels like finding a secret door.

A little padding-top on the parent keeps the space inside, where its background shows. The newer margin-trim property goes further: the W3C draft says it allows the container to trim the margins of its children where they adjoin the container’s edges.

.card {
  padding: 20px;          /* space at the card's edges */
  margin-trim: block;     /* drops children's top/bottom margins at those edges */
}

margin-trim works in Safari 16.4 and Chrome and Edge 155, but Firefox doesn’t support it yet. Until it does, .card > :first-child { margin-top: 0; } does the same job.

What can margin do that padding can’t?

Margin can be negative and it can be auto; padding can be neither. The W3C Box Model spec says Negative values for padding properties are invalid.

  • Negative margins pull boxes closer or overlap them. web.dev: Instead of adding space between adjacent sibling elements, it will reduce space between them. A round avatar can sit half over a card’s top edge with margin-top: -32px.
  • Auto margins share out free space. margin-inline: auto centers a block with a width, the classic trick in our guide to centering a div.
  • Auto margins push flex items apart. The W3C Flexbox spec says any positive free space is distributed to auto margins in that dimension, so margin-left: auto on the last nav item sends it to the far right.
.avatar  { margin-top: -32px; }            /* overlaps the edge above */
.page    { max-width: 70ch; margin-inline: auto; }  /* centered */
.nav .login { margin-left: auto; }         /* pushed to the end of a flex row */

What can padding do that margin can’t?

Padding shows the element’s background, adds to its click area and gives an empty element real height. Margin does none of those.

The W3C Box Model spec notes that Backgrounds specified on the box are by default laid out and painted within the padding edges. That’s why a coloured banner needs padding: margin would put the space outside the colour.

An empty <div> shows the third point. With padding: 20px it’s 40px tall and you can see its background. With margin: 20px 0 it’s 0px tall, and its top and bottom margins collapse through it into one 20px gap.

Padding (or a border) on a parent also stops a child’s margin escaping, the parent-and-first-child case from the section above.

Why doesn’t margin-top work on a span or a link?

Vertical margins do nothing on inline elements such as <span>, <a> and <code>. MDN’s margin page says The top and bottom margins have no effect on non-replaced inline elements, such as <span> or <code>.

Top and bottom padding on an inline element is painted, so the background grows, but it doesn’t push the lines above and below away. Left and right margin and padding work normally.

For real vertical spacing, switch the element to display: inline-block (it stays in the line of text) or display: block.

Do margin and padding change an element’s width?

Padding does with the default box-sizing: content-box: it’s added on top of width. Margin never changes the width, but it does add to the space the element takes up.

That’s why width: 100% plus side margins overflows. In an 800px parent, width: 100%; margin: 0 20px; needs 840px: the 800px box starts 20px in and pokes 20px past the parent’s right edge.

Leave the width at auto instead and the block shrinks to 760px so it fits with its margins. With box-sizing: border-box, padding fits inside the width; the box model guide works through those numbers.

Do margin-block, padding-inline and gap work in every browser?

Yes, all of them work in every current browser; only margin-trim is still arriving. These versions are from MDN’s browser-compat-data (release 8.1.10), read on November 20, 2026:

margin and padding (including auto margins)
Chrome 1, Edge 12, Firefox 1, Safari 1
margin-block, margin-inline, padding-block, padding-inline
Chrome 87, Edge 87, Firefox 66, Safari 14.1 (CSS logical properties: 97.14% of users on Can I use)
gap in grid
Chrome 66, Edge 16, Firefox 61, Safari 12
gap in flexbox
Chrome 84, Edge 84, Firefox 63, Safari 14.1 (97.27% on Can I use)
margin-trim
Chrome 155, Edge 155, Safari 16.4; not yet in Firefox

The logical properties follow the text direction. In English, margin-inline means left and right and margin-block means top and bottom; in Arabic or a vertical script they turn with the text.

Pick one button on a page you made, open DevTools, and swap its margin for padding (or the other way round) while you watch the coloured layers in the box model panel. Then try gap on its parent. Two minutes of that and the four questions on this page become pure instinct.

Sources and further reading

Questions and answers

Is padding inside or outside the border?

Padding is inside the border, between the border and the content, and it shows the element’s background. Margin is outside the border and is always transparent.

Should I use margin or padding for a button?

Use padding for the space around the button’s text, because it takes the button’s colour and makes the button bigger to click. Use margin, or gap on a flex parent, for the space between one button and the next.

Why is margin not working on my span?

A <span> is an inline element, and top and bottom margins have no effect on inline elements. Give it display: inline-block or display: block, and the vertical margin starts to work.

Is it better to use gap or margin?

Inside a flex or grid container, gap is usually better: it only adds space between items, never at the outer edges, and needs no :last-child fix. Margin is still the tool for space between blocks in normal flow and for negative or auto spacing.

Does padding affect the background color?

Yes. The background colour or image fills the content and the padding (and runs under the border), so more padding means more visible background. Margin never shows the background, because margins are always transparent.

How this was written: Written from the W3C CSS Box Model Module Level 3 (Recommendation, 11 April 2024) and Level 4 (Working Draft, 4 August 2024, margin-trim), CSS 2.1, the W3C CSS Flexible Box Layout Module (auto margins), WCAG 2.2 success criteria 2.5.8 and 2.5.5 with the W3C’s Understanding Target Size (Minimum) page, Chrome’s Lighthouse tap-target documentation, web.dev’s Learn CSS spacing module and Accessible tap targets article, MDN Web Docs’ margin, padding and gap references, Heydon Pickering’s "Axiomatic CSS and Lobotomized Owls" (A List Apart, 2014), MDN’s browser-compat-data and Can I use, all read on November 20, 2026. Every size in the diagrams and tables is our own arithmetic from those rules. Code samples are simplified for learning.

Oh, one more thing…

If this helped, you’ll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS — no spam, no promotions.

One short email when there’s something worth your time. Unsubscribe any time.