Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CSS margin property


May 05, 2021 CSS Reference Manual


Table of contents


CSS margin property


Set all four margins for a p element:

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

Try it out . . .

Description of property definition and use

Margin shorting properties set all margin properties in one declaration. The property can have 1 to 4 values.

Instance:

  • 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: 0
Inherited: no
Version: CSS1
JavaScript syntax: object .style.margin="10px 5px"


Browser support

The numbers in the table support the first browser version number of the property.

Attributes
margin 1.0 6.0 1.0 1.0 3.5

The property value

Value Describe
auto The browser calculates the margin.
length Specify margin values in specific units, such as pixels, centimeters, etc. The default is 0px.
% Specifies an outer margin based on a percentage of the width of the parent element.
inherit Specifies that margins should be inherited from the parent element.


Related articles

CSS tutorial: CSS Margin