CSS Margins and Padding

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

Padding

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

Example

HTML
<style>
.box {
  padding: 20px;  /* 20px on all sides */
  background: #fff3e0;
  border: 1px solid #ffb74d;
}
</style>
<div class="box">20px padding creates space between content and border</div>
▶ Try it Yourself

Setting Individual Sides

Example

HTML
<style>
.box {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 30px;
  padding-left: 40px;
  background: #fce4ec;
  border: 1px solid #e91e63;
}
</style>
<div class="box">Different padding: top 10px, right 20px, bottom 30px, left 40px</div>
▶ Try it Yourself

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;

Common Padding Use Cases

Example

HTML
<style>
/* Add padding to button for better clickability */
.button {
  padding: 12px 24px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
}

/* Card content whitespace */
.card {
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #f9f9f9;
}

/* Navigation item tap area */
.nav-item {
  padding: 10px 16px;
  background: #e8eaf6;
  display: inline-block;
}
</style>
<button class="button">Button</button>
<div class="card">Card content with comfortable spacing from border</div>
<nav class="nav-item">Nav Item</nav>
▶ Try it Yourself

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.

Margin

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

Example

HTML
<style>
.box {
  margin: 20px;  /* 20px on all sides */
  background: #e8f5e9;
  border: 1px solid #66bb6a;
  padding: 10px;
}
</style>
<div class="box">20px margin maintains 20px spacing from other elements</div>
▶ Try it Yourself

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 */

Common Margin Use Cases

Example

HTML
<style>
/* Center a container */
.container {
  width: 1200px;
  margin: 0 auto;   /* Horizontal center */
  background: #e3f2fd;
  padding: 10px;
}

/* Spacing between elements */
.box {
  margin-bottom: 20px;
}

/* Space below headings */
h1 {
  margin-bottom: 16px;
}
</style>
<div class="container">Container element with child elements inside</div>
<div class="box">Box element demonstrating box model styling</div>
<h1>First Level Heading</h1>
▶ Try it Yourself

Using margin: auto for Centering

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

Example

HTML
<style>
.box {
  width: 400px;      /* Must specify width */
  margin: 0 auto;    /* Horizontal center */
}
</style>
<div class="box">Box element demonstrating box model styling</div>
▶ Try it Yourself

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.

Scenario 1: Adjacent Siblings

Example

HTML
<style>
.box1 {
  margin-bottom: 30px;
}
.box2 {
  margin-top: 20px;
}
/* Actual spacing is 30px (larger value), not 50px */
</style>
<div class="box1">Box1</div>
<div class="box2">Box2</div>
▶ Try it Yourself

Scenario 2: Parent-Child Elements

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

Example

HTML
<style>
.parent {
  background: #eee;
  /* No padding or border */
}
.child {
  margin-top: 20px;  /* This margin makes parent "drop" too */
}
</style>
<div class="parent">Parent container</div>
<div class="child">Child content</div>
▶ Try it Yourself

Avoidance methods:

Scenario 3: Empty Elements

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

Cases Where Margins Don't Collapse

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.


❓ 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.
100%

🙏 帮我们做得更好

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

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