Margin vs Padding: When to Use Which?
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.
What the diagram shows, in plain text (.btn { padding: 12px 20px; border: 2px solid; margin: 10px; } around a 60 x 20px label):
- Content: 60 x 20px, the text itself.
- Padding: 20px left and right, 12px top and bottom. It’s filled by the button’s background, and a click on it presses the button.
- Border box: 104 x 48px. That’s the whole clickable button.
- Margin: 10px on every side, always transparent. A click there lands on the page, not the button.
- Two buttons side by side: 10 + 10 = 20px between their borders, because horizontal margins never collapse.
- Cards:
padding: 20pxkeeps the text 20px off each card’s edge; 24px of margin orgapseparates the cards.
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.
| Question | Padding | Margin |
|---|---|---|
| Where is the space? | inside the border | outside the border |
| Shows the background colour or image? | yes | never |
| Part of the click or tap area? | yes | no |
| Can it be negative? | no (invalid) | yes (pulls boxes closer or overlaps them) |
Can it be auto? | no | yes (centering, pushing to the far end) |
| Can it collapse? | never | top and bottom margins of blocks can |
| Makes an empty element taller? | yes | no |
| Percentages measured from | the parent’s width | the 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.
The four questions in the diagram, in order:
- Should the space show the element’s background, or be part of what people click or tap? Use padding.
- Is it space between items of one flex or grid container? Use gap.
- Do you need negative space (an overlap) or
autospace (centering, pushing to the far end)? Use margin; only margin can do that. - 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:
| The job | Use | Why |
|---|---|---|
| Room around a button’s text | padding | the button colour fills it and it’s clickable |
| Bigger tap area for an icon or link | padding | grows the hit area without changing the icon |
| Text not touching a card’s edge | padding on the card | the space belongs to the card’s background |
| Space between cards in a row or grid | gap | only between items, never at the outer edges |
| Space between nav links | gap on the flex list + padding on each link | gap separates them, padding makes each easy to tap |
| Space between paragraphs | margin, one direction | space between siblings in normal flow |
| Space under a heading | margin | separates two elements |
| Center a fixed-width block | margin-inline: auto | padding can’t be auto |
| Push one nav item to the far right | margin-left: auto in flex | auto margins take the free space |
| Avatar overlapping a card’s top edge | negative margin | padding can’t be negative |
| Coloured section with breathing room | padding on the section | the colour must reach the edges |
| Keep content off the screen edges on phones | padding on the page wrapper | space 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:
| Padding / margin (each side) | Clickable area | Space between buttons | WCAG 2.5.8 (24px, AA) | 44px (AAA) | 48px (Lighthouse) |
|---|---|---|---|---|---|
| 0 / 0 | 16 x 16px | 0px | fails | no | no |
| 0 / 5px | 16 x 16px | 10px | passes through the spacing exception only | no | no |
| 4px / 0 | 24 x 24px | 0px | passes by size | no | no |
| 14px / 0 | 44 x 44px | 0px | passes | yes | no |
| 16px / 0 | 48 x 48px | 0px | passes | yes | yes |
| 16px / 4px | 48 x 48px | 8px | passes | yes | yes, 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:
| Spacing CSS | Each card | Space between cards | At the container’s edges |
|---|---|---|---|
gap: 24px on the row | (960 — 48) / 3 = 304px | 24px | 0px, flush |
margin: 0 12px on each card | (960 — 72) / 3 = 296px | 12 + 12 = 24px | 12px inset on both sides |
margin-right: 24px on each card | (960 — 72) / 3 = 296px | 24px | 0px 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 withmargin-top: -32px. - Auto margins share out free space.
margin-inline: autocenters 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
, somargin-left: autoon 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:
marginandpadding(includingautomargins)- 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)
gapin grid- Chrome 66, Edge 16, Firefox 61, Safari 12
gapin 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
- W3C: CSS Box Model Module Level 3 — Recommendation, 11 April 2024: margin, padding, transparency of margins, negative padding invalid
- W3C: CSS Box Model Module Level 4, margin-trim — Working Draft, 4 August 2024
- W3C: CSS Flexible Box Layout Module Level 1, auto margins — free space goes to auto margins
- W3C: WCAG 2.2, 2.5.8 Target Size (Minimum) — Level AA, 24 by 24 CSS pixels, with exceptions
- W3C WAI: Understanding Target Size (Minimum) — how the 24px spacing circle works
- Chrome for Developers: Tap targets are not sized appropriately (Lighthouse) — padding for size, margin for spacing, 48px
- web.dev: Accessible tap targets — 48px, about 9mm, 8px apart, coarse-pointer padding
- web.dev Learn CSS: Spacing — margin, padding, gap and consistent spacing
- MDN Web Docs: margin — reference, auto and negative values, inline elements
- MDN Web Docs: padding — reference, negative values invalid
- MDN Web Docs: gap — gutters in flex, grid and multi-column
- A List Apart: Axiomatic CSS and Lobotomized Owls, by Heydon Pickering — October 21, 2014
- MDN browser-compat-data — browser versions, release 8.1.10, read November 20, 2026
- Can I use: CSS logical properties — 97.14% of users, read November 20, 2026
- Can I use: gap property for Flexbox — 97.27% of users, StatCounter usage share for October 2026, read November 20, 2026
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.