The margin
property is the shorthand property for margin-top
, margin-right
, margin-bottom
, and margin-left
(in that order).
An element’s margin is the space between the designated edge(s) of an element and the edge(s) of the next or preceding element in the document flow. Positive and negative values are allowed. In the shorthand format, margin
can have between one to four values.
/* single value - applied to all sides */
div.one {
margin: 10px;
}
/* 2 values - 1st value applied to top & bottom, 2nd value applied to right & left */
div.one {
margin: 10px 20px;
}
/* 3 values - 1st value applied to top, 2nd value applied to right & left, 3rd value applied to bottom */
div.one {
margin: 10px 20px 30px;
}
/* 4 values - 1st value applied to top, 2nd value applied to right, 3rd value applied to bottom, 4th value applied to left */
div.one {
margin: 10px 20px 30px 15px;
}
length
Defines in length units (px, pt, em, etc.) the margin. Default value is 0.
%
Defines the margin of the element in percentage of the width of the containing element.
auto
The margin is calculated by the browser.
initial
Sets the property to its default value.
inherit
Inherits this property’s value from its parent item.
Have a code example of margin
? Submit a codepen.io demo and we'll showcase it here ↴