CSS margin Property
Example
Set the margin for all four sides of a <p> element to 35 pixels:
p {
margin: 35px;
}
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The margin
property sets the margins for an
element, and is a shorthand property for the following properties:
If the margin property has four values:
- margin: 10px 5px 15px 20px;
- top margin is 10px
- right margin is 5px
- bottom margin is 15px
- left margin is 20px
If the margin property has three values:
- margin: 10px 5px 15px;
- top margin is 10px
- right and left margins are 5px
- bottom margin is 15px
If the margin property has two values:
- margin: 10px 5px;
- top and bottom margins are 10px
- right and left margins are 5px
If the margin property has one value:
- margin: 10px;
- all four margins are 10px
Note: Negative values are allowed.
Default value: | 0 |
---|---|
Inherited: | no |
Animatable: | yes, see individual properties. Read about animatable Try it |
Version: | CSS1 |
JavaScript syntax: | object.style.margin="100px 50px" Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Property | |||||
---|---|---|---|---|---|
margin | 1.0 | 6.0 | 1.0 | 1.0 | 3.5 |
CSS Syntax
margin: length|auto|initial|inherit;
Property Values
Value | Description | Play it |
---|---|---|
length | Specifies a margin in px, pt, cm, etc. Default value is 0. Negative values are allowed. Read about length units | Play it » |
% | Specifies a margin in percent of the width of the containing element | Play it » |
auto | The browser calculates a margin | Play it » |
initial | Sets this property to its default value. Read about initial | Play it » |
inherit | Inherits this property from its parent element. Read about inherit |
Margin Collapse
Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.
This does not happen on horizontal (left and right) margins! Only vertical (top and bottom) margins!
Look at the following example:
In the example above, the <p class="a"> element has a top and bottom margin of 30px. The <p class="b"> element has a top and bottom margin of 20px.
This means that the vertical margin between <p class="a"> and <p class="b"> should be 50px (30px + 20px). But due to margin collapse, the actual margin ends up being 30px!
More Examples
Example
Set the margin for a <p> element to 35 pixels for top and bottom, and to 70 pixels for right and left:
p {
margin: 35px 70px;
}
Try it Yourself »
Example
Set the margin for a <p> element to 35 pixels for top, 70 pixels for right and left, and to 50 pixels for bottom:
p {
margin: 35px 70px 50px;
}
Try it Yourself »
Example
Set the margin for a <p> element to 35 pixels for top, 70 pixels for right, 50 pixels for bottom, and to 90 pixels for left:
p {
margin: 35px 70px 50px 90px;
}
Try it Yourself »
Related Pages
CSS tutorial: CSS Margin
CSS tutorial: CSS Box Model
HTML DOM reference: margin property