Margin in CSS - External Space


Margin is the space between the border of the element and the border of the adjacent element. It is the space between the element and the edge of the container.

In CSS, the margin property is used to specify space around the HTML element. The specified space is transparent and has no background color.

This paragraph has a margin of 50px.

Using this property we can specify the top, bottom, left, and right margin independently. These are four properties of margin.


CSS margin properties

There are four properties of margin in CSS which are used to specify the margin of the element. Also, there is one shorthand property of margin which is used to specify all the four properties of margin at once.

PropertyDescription
margin-topSpecifies the top margin of the element.
margin-bottomSpecifies the bottom margin of the element.
margin-leftSpecifies the left margin of the element.
margin-rightSpecifies the right margin of the element.
marginSpecifies all the four properties of margin at once.

CSS margin values

There are many values that can be used with the margin property. The values can be specified in pixels, percentage, ems, rems, etc.

ValueDescription
autoIt is used to set the margin to the auto value. The margin is set to the auto value so that the element is centered in the container.
inheritIt is used to inherit the margin from the parent element.
initialIt is used to set the margin to the initial value.
lengthIt is used to set the margin to the specified length. The length can be specified in pixels, percentage, ems, rems, etc.
unsetIt is used to set the margin to the initial value.

margin-top

The margin-top property is used to specify the top margin of the element. The top margin is the space between the top border of the element and the margin of the adjacent element.

Increasing the top margin will push the element down and decreasing the top margin will pull the element up.

Example

.box {
    margin-top: 25px;
}
Try it
CSS margin-top example
Change the values from options to see the effect.

margin-bottom

The margin-bottom property is used to specify the bottom margin of the element. The bottom margin is the space between the bottom border of the element and the margin of the adjacent element.

Increasing the bottom margin will push the next element down and decreasing the bottom margin will pull the next element up.

Example

.box {
    margin-bottom: 25px;
}
Try it
CSS margin-bottom example
Change the values from options to see the effect.
Element Below the testing element.

margin-left

The margin-left property is used to specify the left margin of the element. The left margin is the space between the left border of the element and the margin of the adjacent element.

Increasing the left margin will push the element to the right and decreasing the left margin will pull the element to the left.

Example

.box {
    margin-left: 25px;
}
Try it
CSS margin-left example
Change the values from options to see the effect.

margin-right

The margin-right property is used to specify the right margin of the element. The right margin is the space between the right border of the element and the margin of the adjacent element.

Increasing the right margin will push the element to the left and decreasing the right margin will pull the element to the right.

Example

.box {
    margin-right: 25px;
}
Try it
CSS margin-right example
Change the values from options to see the effect.

margin (shorthand)

The margin is the shorthand hand property for the margin-top, margin-right, margin-bottom, and margin-left properties.

The margin property can have from one to four values.

Example

.box1 {
    margin: 25px 50px 75px 100px;
}

.box2 {
    margin: 25px 50px 75px;
}

.box3 {
    margin: 25px 50px;
}

.box4 {
    margin: 25px;
}
Try it
CSS margin example
Change the values from options to see the effect.
Element below the testing element

Center an element horizontally

To center an element horizontally, you need to set the margin property to auto for the left and right margins.

For example, to center a div element, you need to set the margin property to auto for the left and right margins:

Example

div {
    margin-left: auto;
    margin-right: auto;
}

Or you can use the margin shorthand property:

div {
    margin: 0 auto;
}
Try it

Note: For the margin property to work, the element should be a block element and have a width set.


CSS margin vs padding

The margin property is used to create space around the element, while the padding property is used to create space inside the element.

Both the margin and padding properties can have from one to four values.

Both the margin and padding properties can have negative values.

CSS margin vs padding example
Change the values from options to see the effect.
Element below the testing element