- Home
- /
- CSS Margin
CSS Tutorial
Introduction
CSS Properties
CSS Designing
CSS Advance Topics
CSS FAQs
- Home
- /
- CSS Margin
CSS Margin
Margins define the outer space around HTML elements. It’s like giving each element its own breathing room, separating it from others on the page..
What Is Margin?
Margin is the space outside the border of an element.
If padding is the inner space, margin is the external space that pushes other elements away.
Basic Syntax
selector {
margin: value;
}
You can set margins using:
px
(pixels)em
,rem
%
(percentage)auto
(useful for centering)
Method 1: Same Margin on All Sidess
All sides have 20px margin

Explanation:
The box moves away from all surrounding elements by 20px.
Method 2: Vertical and Horizontal Margins
selector {
margin: vertical horizontal;
}
Top & Bottom: 30px — Left & Right: 60px

Tip: This layout is helpful when you want more horizontal spacing than vertical.
Method 3: Margins on All Sides Individually
selector {
margin: top right bottom left;
}
Custom margins: top-10px, right-40px, bottom-20px, left-80px

Breakdown:
Top → 10px
Right → 40px
Bottom → 20px
Left → 80px
Bonus Tip: Centering with Margin Auto
I'm centered using margin: 0 auto
