CSS Margin


The CSS margin properties define the space around elements.

Margin

The margin clears an area around an element (outside the border). The margin does not have a background color, and is completely transparent. The top, right, bottom, and left margin can be changed independently using separate properties. A shorthand margin property can also be used, to change all margins at once.

Possible Values

  1. auto: The browser sets the margin. The result of this is dependant of the browser
  2. length: Defines a fixed margin (in pixels, pt, em, etc.)
  3. %: Defines a margin in % of the containing element
It is possible to use negative values, to overlap content.

Margin - Individual sides

In CSS, it is possible to specify different margins for different sides:

Example


Margin - Shorthand property

To shorten the code, it is possible to specify all the margin properties in one property. This is called a shorthand property.

The shorthand property for all the margin properties is "margin":

Example

The margin property can have from one to four values.