English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS reference manual

CSS @rules

CSS attributes大全

Usage and examples of the CSS margin property

The margin CSS property sets the outer margin on all four sides of an element. It ismargin-top,margin-right,margin-bottomAndmargin-leftAbbreviated property name.

The table below provides usage instructions and version history for this property, as well as the syntax for using this property in JavaScript scripts.

Default value:0
Applies to:All elements, but not table displayelements of types other than table- caption table inline-table. It also applies to ::first-letter.
Inheritance:No
Animated:Yes.Please refer to Animation properties.
Version:CSS 1,2,3
JavaScript syntax:object.style.margin="20px 15px"

Syntax

The syntax of this attribute is as follows:

margin: [ length | percentage | auto ] 1 to 4 values | initial | inherit

The following examples demonstrate how to use the margin property.

  h1 {
   margin: 25px;
  }
  p {
   margin: 50px 100px;
  }
Test to see‹/›

This shorthand notation can take one, two, three, or four space-separated values.

  • If setOne value, then this margin applies to all four sides.

  • If setTwo valuesIf the first value is applied to the top and bottom, and the second value is applied to the right and left.

  • Three values separatelyApplied to the top, horizontal (i.e., left and right), and bottom.

  • Four valuesApplied sequentially to the top, right, bottom, and left.

Attribute value

The following table describes the value of this property.

ValueDescription
margin-topSet the top margin of the element.
margin-rightSet the right margin of the element.
margin-bottomSet the bottom margin of the element.
margin-leftSet the left margin of the element.
initialSet this property to its default value.
inheritIf specified, the associated elements will adopt the margin attribute value of their parent element.

Browser compatibility

Browser compatibility of the margin property; the numbers in the table below represent the minimum version number of the browser that supports this property; all mainstream browsers support this property.

  • Firefox 1+

  • Google Chrome1+

  • Internet Explorer 3+

  • Apple Safari 1+

  • Opera 4+

Further reading

Please refer to the following tutorials:CSS Margin,CSS Box Model.

Related properties:margin-top,margin-right,margin-bottom,margin-left.