CSS margin property
Example
Set all four margins of a p element:
p
{
margin:2cm 4cm 3cm 4cm;
}
Try it »
Property definition and usage
The margin shorthand property sets all margin properties in a single declaration. This property can have from one to four values.
Example:
- margin:10px 5px 15px 20px;
- The top margin is 10px
- The right margin is 5px
- The bottom margin is 15px
- The left margin is 20px
- margin:10px 5px 15px;
- The top margin is 10px
- The right and left margins are 5px
- The bottom margin is 15px
- margin:10px 5px;
- The top and bottom margins are 10px
- The right and left margins are 5px
- margin:10px;
- All four margins are 10px
Note: Negative values are allowed.
| Default value: | 0 |
|---|---|
| Inherited: | no |
| Version: | CSS1 |
| JavaScript syntax: | object.style.margin="10px 5px" |
Browser support
The numbers in the table indicate the first browser version that supports this property.
| property | |||||
|---|---|---|---|---|---|
| margin | 1.0 | 6.0 | 1.0 | 1.0 | 3.5 |
Attribute values
| Value | Description |
|---|---|
| auto | The browser calculates the margin. |
| length | Specifies the margin in a specific unit, such as pixels, centimeters, etc. The default value is 0px. |
| % | Specifies the margin as a percentage of the parent element's width. |
| inherit | Specifies that the margin should be inherited from the parent element. |
Related Articles
CSS Tutorial: CSS Margin