Padding and Margins: Percentages, auto Centering and Negative Margins
Space inside and outside boxes: padding and margin shorthands, logical sides, why vertical percentages refer to the width, auto margins for centering and pushing, negative margins, inline boxes and box-decoration-break, margin-trim, and padding versus margin versus gap.
kvi5hnuCode Sprout team member9 min readOn this page
Padding is space inside a box's border, and margin is space outside it. Both look like "empty space", but they behave differently: padding is part of the box, so it shows the background and is clickable, while margins are transparent, can be negative, can be auto, and collapse vertically (article 40).
The shorthands
padding and margin take one to four values, going clockwise from the top:
| Values | Meaning | Example |
|---|---|---|
| 1 | all four sides | padding: 1rem |
| 2 | top and bottom, left and right | padding: 0.5rem 1rem |
| 3 | top, left and right, bottom | padding: 1rem 1.5rem 2rem |
| 4 | top, right, bottom, left | margin: 0 auto 2rem 1rem |
A missing value copies its opposite side: with three values, the left copies the right. The longhands are padding-top, margin-left and so on.
Logical sides
The logical versions follow the writing direction (article 47), and take one or two values in the order start end:
.card {
padding-block: 1rem 1.5rem; /* top 1rem, bottom 1.5rem in horizontal text */
padding-inline: 1.25rem; /* left and right */
margin-block-end: 2rem; /* the bottom */
}
padding-inline-start is the left side in left-to-right text and the right side in Arabic or Hebrew, so a layout using logical properties mirrors itself for right-to-left languages. There's no four-value logical shorthand; padding and margin are always physical.
Values
| Padding | Margin | |
|---|---|---|
| lengths | yes, never negative | yes, can be negative |
| percentages | of the containing block's width | of the containing block's width |
auto | no | yes |
| initial value | 0 (browsers add some, such as on lists and buttons) | 0 (browsers add some, such as on <body>, <p> and headings) |
Percentages refer to the width, on all sides
A surprising rule: padding-top: 10% is 10% of the containing block's width (its inline size, to be exact), not its height. The same goes for margins:
.parent { width: 600px; }
.child { padding: 10%; } /* 60px on all four sides, top and bottom included */
The reason is circularity: a parent's height usually depends on its children, so if a child's vertical padding depended on the parent's height, neither could be computed. Using the width means padding: 5% gives equal space on every side.

The parent in the left panel is less than 100px tall, yet the top padding is still 10% of its 300px width.
This rule was the basis of the old aspect-ratio trick, padding-top: 56.25% for 16. Use aspect-ratio instead (article 38). Percentage padding is uncommon today; rem, em and clamp() (article 26) are usually better for responsive spacing.
auto margins
An auto margin takes whatever space is left over. For block boxes in normal flow, that works horizontally:
.page { max-width: 64rem; margin-inline: auto; } /* centered */
.push-right { width: 12rem; margin-left: auto; } /* aligned to the right */
- Both inline margins
auto, with a width: the leftover space is split equally, centering the box. That's the standard way to center a block. It needs a width smaller than the container:width,max-widthorfit-content(article 37). - One margin
auto: it takes all the leftover space, pushing the box to the other side. - Width
autotoo: the width takes the space first, soautomargins become 0. - Overconstrained: if the width and both margins are set to lengths that don't add up, the browser ignores the end margin (the right one in left-to-right text) and recomputes it.

The numbers are measured from the rendered boxes. The last row shows that the declared right margin of 50px is simply replaced.
Vertical auto margins become 0 in normal flow, so margin: auto doesn't center vertically. In flexbox and grid (article 63), auto margins work in both directions, and margin: auto centers an item completely. Absolutely positioned elements (article 56) can also center with inset: 0; margin: auto.
Negative margins
Margins can be negative, pulling things together:
| Negative margin | Effect |
|---|---|
| top or left (start sides) | moves the box up or left, over what comes before it |
| bottom or right (end sides) | doesn't move the box: it pulls the following content up or left |
both left and right, with width: auto | makes the box wider than its container |
body { font: 0.9rem/1.5 system-ui, sans-serif; margin: 1rem; }
.column { max-width: 22rem; margin-inline: auto; padding: 0.75rem; border: 2px dashed #8a3b12; }
.box { background: #f4efe6; border: 2px solid #8a3b12; padding: 0.4rem 0.6rem; margin-bottom: 0.5rem; }
.centered { width: 60%; margin-inline: auto; }
.right { width: 40%; margin-left: auto; }
.bleed { margin-inline: -1.5rem; background: #2f7d4f; color: white; border-color: #1f5636; }
mark { padding: 0.1em 0.4em; border-radius: 6px; background: #f4c95d; }
.clone { box-decoration-break: clone; -webkit-box-decoration-break: clone; }
The "bleed" is useful for images or callouts that should be wider than a text column, but check that it doesn't overflow on small screens. For full-width sections inside a centered page, a grid with named full-bleed lines (article 68) is more robust.
Negative margins that make elements overlap are fragile: the overlapped element can become unclickable. For deliberate overlaps, positioning (article 56) or grid placement is clearer.
Inline elements
Inline boxes (article 45) such as <span>, <a> and <mark> accept padding and margins, but only the inline sides affect layout:
- Left and right padding, borders and margins add space before and after the element's text.
- Top and bottom padding and borders are painted, overlapping the lines above and below, and top and bottom margins have no effect at all.
When an inline element wraps across lines, its start padding and margin appear only at the very start, and its end ones only at the very end: the box is sliced at each line break. box-decoration-break: clone gives each line fragment its own padding, border, radius and shadow instead, which looks better for highlighted text, as the last example shows. Chrome and Firefox support it unprefixed. Safari needs -webkit-box-decoration-break, and supports it only for inline elements.
Replaced inline elements like <img> are different: their vertical margins and padding do push lines apart.
margin-trim
A container's padding and its children's margins add up at the edges: a card with padding: 1rem whose first paragraph has margin-top: 1em has double the space at the top. margin-trim removes the children's margins at the container's edges:
.card { padding: 1rem; margin-trim: block; } /* trims the first child's top margin and the last child's bottom margin */
It's in Safari and Chrome 155, not yet in Firefox. Until it's everywhere, use the common alternatives:
.card > :first-child { margin-block-start: 0; }
.card > :last-child { margin-block-end: 0; }
Padding, margin or gap?
| Use | For |
|---|---|
| padding | space between a box's border and its content; it's part of the box, shows the background and enlarges the clickable area |
| margin | space between a box and its neighbors in normal flow |
gap | space between items in flex, grid and multi-column layouts: it goes only between items, never at the edges |
A good habit is that components don't set outer margins on themselves; their container spaces them, with gap or a rule like .stack > * + * { margin-block-start: 1rem; } (article 40). The component then fits anywhere.
What's current
- Logical padding and margin properties work everywhere.
margin-trimis in Safari and Chrome 155, not in Firefox.box-decoration-breakis unprefixed in Chrome 130 and Firefox; Safari needs the prefix.- For
margin-inline: autocentering,width: fit-content(article 37) handles boxes of unknown width.
Common questions
Why doesn't margin: auto center my box?
Either it has no width smaller than its container, so there's no leftover space, or you're trying to center vertically in normal flow, where auto block margins are 0. Give it a max-width or fit-content, or use flexbox or grid for vertical centering.
Why is my padding-top: 50% based on the width?
Percentage padding and margins on every side refer to the containing block's width, to avoid a circular dependency on height.
Can padding be negative? No. Negative padding is invalid, and the declaration is ignored. Use negative margins instead.
Why is there extra space at the top of my card?
The card's padding and its first child's top margin add up. Remove the child's margin, or use margin-trim where supported. If the card has no padding, the child's margin may instead escape through the top of the card (article 40).
Do margins work on table cells?
No: margins don't apply to table cells, rows or other internal table boxes, and padding applies to cells but not to rows. Use border-spacing (article 74).
Summary
- Shorthands go clockwise from the top. Logical forms like
padding-blockandmargin-inlinetake start and end values. - Padding can't be negative or
auto. Margins can be both. - Percentage padding and margins on all sides refer to the containing block's width.
- Two
autoinline margins center a block with a width; one pushes it to the other side. Verticalautomargins are 0 in normal flow, but not in flex and grid. - Negative start margins move a box; negative end margins pull the next content. Negative inline margins can widen a box beyond its container.
- On inline elements, only inline-side spacing affects layout.
box-decoration-break: clonerepeats decorations on each line. margin-trimremoves child margins at a container's edges. Usegapfor spacing items in flex and grid.
Practice questions
What are the four margins of margin: 1rem 2rem 3rem?
Answer
Top 1rem, right 2rem, bottom 3rem, and left 2rem, copied from the right.
A child of a 500px-wide, 200px-tall parent has padding: 10%. What is its top padding?
Answer
50px. Percentage padding refers to the containing block's width, even for the top and bottom.
Write CSS to place a 15rem-wide box at the right edge of its container, using margins only.
Answer
.box { width: 15rem; margin-left: auto; }
Better still for right-to-left languages: margin-inline-start: auto.
A highlighted <mark> with padding and rounded corners wraps onto two lines, and the line break cuts it with no padding or corners. How do you fix it?
Answer
mark { box-decoration-break: clone; -webkit-box-decoration-break: clone; }
Each line fragment then gets its own padding, border radius and background edges.
Previous: aspect-ratio, object-fit and object-position for Media and Boxes
Next: Margin Collapsing: When Vertical Margins Combine and How to Stop It

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.