Padding & Margin
Padding vs Margin
Padding is space inside the element, between
content and border.
Margin is space outside the element, between the
border and other elements.
Padding Shorthand
The padding property can take 1, 2, 3, or 4 values:
padding: 20px;- All sidespadding: 10px 20px;- Top/Bottom, Left/Rightpadding: 10px 20px 15px;- Top, Left/Right, Bottompadding: 10px 20px 15px 25px;- Top, Right, Bottom, Left (clockwise)
Margin Shorthand
Margin uses the same shorthand syntax as padding. It creates space between elements.
Individual Sides
You can also set padding and margin for individual sides using specific properties.
Centering with Margin Auto
Use margin: 0 auto; to horizontally center a
block element with a defined width.
Negative Margins
You can use negative margins to pull elements closer or overlap them. Use carefully!
Key Points
- Padding - Space inside, between content and border
- Margin - Space outside, between elements
- Both use same shorthand syntax (1-4 values)
margin: 0 auto;centers block elements- Negative margins pull elements closer
- Individual properties:
padding-top,margin-left, etc.
Quick Quiz
How do you horizontally center a block element?
Enjoying these tutorials?