CSS marginProperty


Example

Set all four margins of a p element:

p
{
margin:2cm 4cm 3cm 4cm;
}

Try it Yourself »

Property definition and usage instructions

The margin shorthand property sets all the margin properties in one declaration. This property can have from 1 to 4 values.

Example:

  • margin:10px 5px 15px 20px;
    • Top margin is 10px
    • Right margin is 5px
    • Bottom margin is 15px
    • Left margin is 20px

  • margin:10px 5px 15px;
    • Top margin is 10px
    • Right and left margins are 5px
    • Bottom margin is 15px

  • margin:10px 5px;
    • Top and bottom margins are 10px
    • 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 specify the first browser version that supports the property.

Property
margin 1.0 6.0 1.0 1.0 3.5

Property value

Value Description
auto The browser calculates the margins.
length Specifies a margin value in specific units, such as pixels, centimeters, etc. The default value is 0px.
% Specifies a margin in percent of the width of the parent element.
inherit Specifies that the margin should be inherited from the parent element.


Related Articles

CSS Tutorial:CSS Margin

other extensions