CSS: CSS Margins and Padding

Last updated: 2026-10-13

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

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 */
padding: 20px;

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

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

/* Four values: top, right, bottom, left (clockwise) */
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
margin: 20px;            /* All sides equal */
margin: 10px 20px;       /* Top+bottom, left+right */
margin: 10px 20px 30px;  /* Top, left+right, bottom */
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

❓ FAQ

Q What is margin collapse?
A Adjacent vertical margins collapse into the larger one. For example, one element has margin-bottom: 30px, the next has margin-top: 20px — actual spacing is 30px, not 50px. Adding overflow: hidden to the parent or using flex layout prevents this.
Q What if padding makes elements too large?
A Globally set box-sizing: border-box. This includes padding and border in the element's total width/height, preventing expansion. Almost all modern projects do this.
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.

📖 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.

100%

🙏 帮我们做得更好

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

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