CSS: Margin vs Padding

By Xah Lee. Date: . Last updated: .

Difference between margin and padding

margin
padding
Text. Alice in Wonderland.

Padding Syntax

“padding” can have 1 to 4 values, separated by space:

They are shorthands to the following:

Padding value cannot be negative.

Example

.aaa {
 padding: 1rem;
}

.bbb {
 padding-top: 1rem;
 padding-bottom: 1rem;
 padding-left: 1rem;
 padding-right: 1rem;
}

Margin

The syntax of margin is same as padding. But margin values can be negative.

Example

div.aaa {
 margin: 1rem;
}

div.bbb {
 margin-top: 1rem;
 margin-bottom: 1rem;
 margin-left: 1rem;
 margin-right: 1rem;
}

CSS, border, margin, box model