The margin
CSS property sets the margin area on all four sides of an element. It is a shorthand that sets all individual margins at once: margin-top
, margin-right
, margin-bottom
, and margin-left
.
/* Apply to all four sides */ margin: 1em; /* vertical | horizontal */ margin: 5% auto; /* top | horizontal | bottom */ margin: 1em auto 2em; /* top | right | bottom | left */ margin: 2px 1em 0 auto; /* Global values */ margin: inherit; margin: initial; margin: unset;
<div class="grid"> <div class="col"> <div class="cell"> <div class="m m0">margin: 0</div> </div> <div class="cell"> <div class="m m1">margin: 1em</div> </div> <div class="cell"> <div class="m m2">margin: 5% auto</div> </div> <div class="cell"> <div class="m m3">margin: 1em auto 2em</div> </div> <div class="cell"> <div class="m m4">margin: 5px 1em 0 auto</div> </div> <div class="note">All the boxes above have the same width of 50%</div> </div> </div>
html,body { height: 100%; box-sizing: border-box; } .grid { width: 100%; height: 100%; display: flex; background: #EEE; font: 1em monospace; } .col { display: flex; flex: 1 auto; flex-direction: column; } .cell { box-sizing: border-box; margin: .5em; padding: 0; background-color: #FFF; overflow: hidden; text-align: center; } .note { background: #fff3d4; padding: 1em; margin: .5em; font: .8em sans-serif; text-align: center; flex: none; } .m { display: block; width: 50%; text-align: left; background: #E4F0F5; padding: .5em; border: 1px solid; } .m0 { margin: 0; } .m1 { margin: 1em; } .m2 { margin: 5% auto; } .m3 { margin: 1em auto 2em; } .m4 { margin: 5px 1em 0 auto; }
Initial value | as each of the properties of the shorthand:
|
---|---|
Applies to | all elements, except elements with table display types other than table-caption , table and inline-table . It also applies to ::first-letter . |
Inherited | no |
Percentages | refer to the width of the containing block |
Media | visual |
Computed value | as each of the properties of the shorthand:
|
Animation type | a length |
Canonical order | the unique non-ambiguous order defined by the formal grammar |
Syntax
The margin
property may be specified using one, two, three, or four values. Each value is a <length>
, a <percentage>
, or the keyword auto
. Each value can be positive, zero, or negative.
- When one value is specified, it applies the same margin to all four sides.
- When two values are specified, the first margin applies to the top and bottom, the second to the left and right.
- When three values are specified, the first margin applies to the top, the second to the left and right, the third to the bottom.
- When four values are specified, the margins apply to the top, right, bottom, and left in that order (clockwise).
Values
<length>
- The size of the margin as a fixed value.
<percentage>
- The size of the margin as a percentage, relative to the width of the containing block.
auto
- The browser selects a suitable margin to use. For example, in certain cases this value can be used to center an element.
Formal syntax
[ <length> | <percentage> | auto ]{1,4}
Examples
Simple example
HTML
<div class="center">This element is centered.</div> <div class="outside">This element is positioned outside of its container.</div>
CSS
.center { margin: auto; background: lime; width: 66%; } .outside { margin: 3rem 0 0 -3rem; background: cyan; width: 66%; }
More examples
margin: 5%; /* all sides: 5% margin */ margin: 10px; /* all sides: 10px margin */ margin: 1.6em 20px; /* top and bottom: 1.6em margin */ /* left and right: 20px margin */ margin: 10px 3% 1em; /* top: 10px margin */ /* left and right: 3% margin */ /* bottom: 1em margin */ margin: 10px 3px 30px 5px; /* top: 10px margin */ /* right: 3px margin */ /* bottom: 30px margin */ /* left: 5px margin */ margin: 2em auto; /* top and bottom: 2em margin */ /* box is horizontally centered */ margin: auto; /* top and bottom: 0 margin */ /* box is horizontally centered */
Notes
Horizontal centering
To center something horizontally in modern browsers, you can use display: flex; justify-content: center;
.
However, in older browsers like IE8-9 that do not support flexbox layout, these are not available. In order to center an element inside its parent, use margin: 0 auto;
.
Margin collapsing
Elements' top and bottom margins are sometimes collapsed into a single margin that is equal to the larger of the two margins. See Mastering margin collapsing for more information.
Specifications
Specification | Status | Comment |
---|---|---|
CSS Basic Box Model The definition of 'margin' in that specification. |
Working Draft | No significant change. |
CSS Transitions The definition of 'margin' in that specification. |
Working Draft | Defines margin as animatable. |
CSS Level 2 (Revision 1) The definition of 'margin' in that specification. |
Recommendation | Removes its effect on inline elements. |
CSS Level 1 The definition of 'margin' in that specification. |
Recommendation | Initial definition. |
Browser compatibility
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | Yes | 1 | 3 | 3.5 | 1 |
auto | 1 | ? | 1 | 61 | 3.5 | 1 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | 1 | Yes | Yes | 4 | 6 | 6 | 1 |
auto | ? | ? | ? | 4 | ? | ? | ? |
1. The auto
value is not supported in quirks mode.