What is Margin?
Margin is the space outside an element's border. It creates distance between elements.
Basic Margin
CSSRead-only
1
Individual Sides
CSSRead-only
1
Shorthand Property
CSSRead-only
1
Shorthand Variations
| Values | Meaning |
|---|---|
| 1 value | All sides |
| 2 values | Top/Bottom, Left/Right |
| 3 values | Top, Left/Right, Bottom |
| 4 values | Top, Right, Bottom, Left |
Auto Margin (Centering)
CSSRead-only
1
Negative Margin
CSSRead-only
1
Margin Collapse
Vertical margins between elements may collapse into a single margin (the larger value is applied).
Example
CSSRead-only
1
Best Practices
- Use margin for spacing between elements
- Use shorthand for cleaner code
- Use auto for horizontal centering
- Avoid excessive negative margins
Common Mistakes
- Confusing margin with padding
- Unexpected margin collapsing
- Using too many manual margins
- Overusing negative margins
Conclusion
CSS margin controls spacing outside elements. Understanding it is key for layout and spacing design.