CSS Margin vs Padding Differences | Generated by AI

Home 2025.11

Here are the main differences between margin and padding in CSS, explained clearly:

Feature Margin Padding
Location Outside the element’s border Inside the element’s border
Affects Space between elements Space inside the element (around content)
Part of box model Not included in the element’s width/height (unless box-sizing: border-box) Included in width/height by default (changes with box-sizing)
Background color Does not get background color Does get background color/image
Collapsing Vertical margins collapse (larger one wins) Padding never collapses
Can be negative Yes (e.g., margin: -10px) No (negative padding is invalid)
Auto value behavior margin: auto centers block elements horizontally padding: auto does nothing
Visual effect Pushes other elements away Increases the clickable/visible area of the element

Visual Representation (Box Model)

+-----------------------------+
|          Margin             |
|    +-----------------+      |
|    |    Border        |      |
|    |  +-------------+  |      |
|    |  |  Padding    |  |      |
|    |  |  +-------+  |  |      |
|    |  |  |Content|  |  |      |
|    |  |  +-------+  |  |      |
|    |  +-------------+  |      |
|    +-----------------+      |
+-----------------------------+

Quick Examples

.box {
  width: 200px;
  height: 100px;
  background: lightblue;
  border: 5px solid navy;

  padding: 20px;     /* Adds space INSIDE → content appears smaller */
  margin: 30px;      /* Adds space OUTSIDE → pushes other elements away */
}

Common Use Cases

In short:
Padding = internal space (affects element itself)
Margin = external space (affects layout around the element)


Back

x-ai/grok-4.1-fast

Donate