CSS: CSS Margins and Padding

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Dana🏷 Category: CSS Tutorial

CSS spacing comes in two types: padding and margin. They are key properties for controlling "inner whitespace" and "outer spacing" of elements.

Spacing Properties Quick Reference

Property Common Values Purpose
padding 16px, 10px 20px, 10px 5px 15px 20px Padding (top, right, bottom, left)
padding-top/right/bottom/left 16px, 5% Single-side padding
margin 20px, 0 auto, 10px 0 20px 0 Margin (top, right, bottom, left)
margin-top/right/bottom/left 10px, auto Single-side margin
auto margin: 0 auto Center block elements horizontally
💡 Margin shorthand rule: 4 values (top, right, bottom, left) → 3 values (top, left+right, bottom) → 2 values (top+bottom, left+right) → 1 value (all sides equal).

Properties in this section: padding margin display line-height

1. Padding

padding is the space between the content area and the border — the element's "inner whitespace."

▶ Example

▶ Try it Yourself

(1) Setting Individual Sides

▶ Example

▶ Try it Yourself

(2) Padding Shorthand Rules

💡 Memory trick: Padding/margin shorthand starts from "top" going clockwise — top, right, bottom, left. Can't remember? Think "clockwise from 12 o'clock." Two values mean "top+bottom, left+right."

CSS
/* One value: all sides equal */
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
padding: 20px;

/* Two values: top+bottom, left+right */
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
padding: 10px 20px;

/* Three values: top, left+right, bottom */
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
padding: 10px 20px 30px;

/* Four values: top, right, bottom, left (clockwise) */
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
padding: 10px 20px 30px 40px;

(3) Common Padding Use Cases

▶ Example

▶ Try it Yourself

(4) Padding Notes

📌 Inline element note: <span>, <a> and other inline elements' padding only works horizontally. If you want perfect click areas on inline elements, set display: inline-block.


2. Margin

margin is the space between the element's border and other elements — the element's "outer spacing."

▶ Example

▶ Try it Yourself

(1) Margin Shorthand Rules

Same as padding:

CSS
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
margin: 20px;            /* All sides equal */
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
margin: 10px 20px;       /* Top+bottom, left+right */
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
margin: 10px 20px 30px;  /* Top, left+right, bottom */
/* Box model properties */
/* Box model properties */
/* Box model properties */
/* Box model properties */
margin: 10px 20px 30px 40px; /* Top, right, bottom, left */

(2) Common Margin Use Cases

▶ Example

▶ Try it Yourself

(3) Using margin: auto for Centering

margin: 0 auto can horizontally center a block element within its parent:

▶ Example

▶ Try it Yourself

3. Margin Collapse

⚠️ Classic trap: Adjacent vertical margins collapse into the larger one — this is called "Margin Collapse." The simplest fix is adding overflow: hidden or padding: 1px to the parent element. Using a flex container also prevents this.

This is an important CSS concept — adjacent vertical margins collapse into one, taking the larger value.

(1) Scenario 1: Adjacent Siblings

▶ Example

▶ Try it Yourself

(2) Scenario 2: Parent-Child Elements

When the parent has no padding, border, or content, the child's margin "penetrates" outside the parent:

▶ Example

▶ Try it Yourself

Avoidance methods:

(3) Scenario 3: Empty Elements

A block element with no content, padding, or border will also have its top and bottom margins collapse.

(4) Cases Where Margins Don't Collapse


4. Choosing Between Padding and Margin

Scenario Use Which
Keep button text from touching edges padding
Keep two cards apart margin
Keep content from border padding
Center container on page margin: auto
Control background area size padding

Simple decision method: Should the background cover this area? If yes, use padding; if no, use margin.


Properties in this section: padding margin display line-height

❓ FAQ

Q Two stacked elements show 30px gap instead of 50px — why did the margins "collapse"?
A Adjacent vertical margins collapse into the larger one. margin-bottom: 30px meets margin-top: 20px and only 30px takes effect. Don't count on stacking for 50px — plan a single margin value, or wrap one element in a container with overflow: hidden.
Q How do I prevent margin collapse?
A It triggers on vertically adjacent block elements with nothing between them. Pick any one: parent with overflow: hidden or display: flow-root, insert a border/padding as a divider, or switch to flex/grid (no collapsing inside). display: flow-root is the most semantic modern fix.
Q My child's margin-top pushed whitespace outside the parent — why?
A That's parent-child margin collapse: without padding-top or a border as a barrier, the first child's margin-top "escapes" outside the parent. Fix with overflow: hidden, display: flow-root, or a padding-top: 1px barrier on the parent. Same rule as adjacent collapse, different shape.
Q What conditions does margin: 0 auto centering need?
A The element must have explicit width (not auto) and must be a block element (display: block). Inline elements and block elements without set width cannot be centered with margin: auto.
Q margin: 0 auto doesn't work on my image or inline element — what now?
A Auto margins only work horizontally on block elements. For images, set display: block first, then margin: auto; for inline elements, use text-align: center on the parent instead. Check the actual display value in DevTools Computed — many "not working" cases are elements that were never block.
Q How does padding shorthand work with 1, 2, 3, or 4 values?
A Clockwise "top right bottom left": one value = all sides; two = top/bottom, left/right; three = top, left/right, bottom; four = top right bottom left. padding: 10px 20px means 10 top/bottom, 20 sides. Same for margin — no need to memorize the four longhand properties.
Q What are negative margins good for, and what are the pitfalls?
A Negative margins pull the element that direction — margin-top: -10px shifts it up 10px to overlap content. Common uses: canceling parent padding for full-bleed, stretching in equal-height layouts. The pitfall: combined with z-index or floats the behavior gets unintuitive; prefer position: relative with negative offsets or grid in new code.
Q Why does margin-top do absolutely nothing on some elements?
A Most common cause: the element is inline — vertical margins don't apply to inline elements. Check the actual display value in the Computed panel. Switch to inline-block for vertical spacing, or adjust line-height instead.
Q Percentage padding is calculated against what, exactly?
A The parent's width — all four sides, including padding-top and padding-bottom. This is used for aspect-ratio placeholders: padding-top: 56.25% reserves a 16:9 box. Beware: it scales when the parent resizes, so don't treat it as fixed spacing.

📖 Summary

📝 Exercises

  1. Create a "card group" page with 3 cards, using margin to control spacing between cards.
  2. Create a navigation bar using padding to control text-to-border distance in each nav item.
  3. Demonstrate "margin collapse" and solve it with at least two different methods.
  4. Use margin: 0 auto to center an 800px-wide container horizontally on the page.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏