Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS margin

CSS margin


The CSS margin property defines the space around an element.


margin

margin clears the area around an element (outside the border). margin has no background color and is completely transparent.

margin can change the top, bottom, left, and right margins of an element individually, or change all properties at once.

Possible values

Value Description
auto Set the browser margin.
The result of doing this depends on the browser
length Define a fixed margin (using pixels, pt, em, etc.)
% Define a margin using a percentage

Remark Margin can use negative values to overlap content.


Margin - individual side margin properties

In CSS, it can specify different margins for different sides:

Example

margin-top

:100

px

;

margin-bottom

:100

px

;

margin-right

:50

px

;

margin-left

:50

px

;

Try it »


Margin - shorthand property

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

The shorthand property for all margin properties is margin:

Example

margin

:100

px

50

px

;

Try it »

The margin property can have one to four values.

- Top margin of 25px - Right margin of 50px - Bottom margin of 75px - Left margin of 100px - Top margin of 25px - Left and right margins of 50px - Bottom margin of 75px - Top and bottom margins of 25px - Left and right margins of 50px - All four margins are 25px

More examples

Set the top margin of text using a value in centimeters
This example demonstrates how to set the top margin of text using a value in centimeters.

Set the bottom margin of text using a percentage value
This example demonstrates how to set the bottom margin using a percentage value, relative to the width of the containing element.


All CSS margin properties

property Description
margin Shorthand property. Sets all margin properties in a single declaration.
margin-bottom Sets the bottom margin of an element.
margin-left Sets the left margin of an element.
margin-right Sets the right margin of an element.
margin-top Sets the top margin of an element.