Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1046702 of margin

  • Revision slug: Web/CSS/margin
  • Revision title: margin
  • Revision id: 1046702
  • Created:
  • Creator: Simplexible
  • Is current revision? No
  • Comment {{xref_csspercentage}} replaced with {{cssxref("<percentage>")}}

Revision Content

{{CSSRef}}

Summary

The margin CSS property sets the margin for all four sides. It is a shorthand to avoid setting each side separately with the other margin properties: {{ Cssxref("margin-top") }}, {{ Cssxref("margin-right") }}, {{ Cssxref("margin-bottom") }} and {{ Cssxref("margin-left") }}.

Negative values are also allowed.

{{cssinfo}}

Syntax

/* 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;

Values

Accepts one, two, three or four values of the following:

<percentage>
Specifies a fixed width. Negative Values are allowed. See for {{cssxref("")}} possible units.
<percentage>
A {{cssxref("<percentage>")}} relative to the width of the containing block. Negative values are allowed.
auto
auto is replaced by some suitable value, e.g. it can be used for centering of blocks.
div { width:50%;  margin:0 auto; } centers the div container horizontally.
  • One single value applies to all four sides.
  • Two values apply first to top and bottom, the second one to left and right.
  • Three values apply first to top, second to left and right and third to bottom.
  • Four values apply to top, right, bottom and left in that order (clockwise).

Formal syntax

{{csssyntax}}

Examples

Simple example

HTML

<div class="ex1">
  margin:     auto;
  background: gold;
  width:      66%;
</div>
<div class="ex2">
  margin:     20px 0 0 -20px;
  background: gold;
  width:      66%;
</div>

CSS

.ex1 {
  margin: auto;
  background: gold;
  width: 66%;
}
.ex2 {
  margin: 20px 0px 0 -20px;
  background: gold;
  width: 66%;
}

{{ EmbedLiveSample('Simple_example') }}

Another example

margin: 5%;                /* all sides 5% margin */

margin: 10px;              /* all sides 10px margin */

margin: 1.6em 20px;        /* top and bottom 1.6em, left and right 20px margin */

margin: 10px 3% 1em;       /* top 10px, left and right 3%, bottom 1em margin */

margin: 10px 3px 30px 5px; /* top 10px, right 3px, bottom 30px, left 5px margin */

margin: 1em auto;          /* 1em margin on top and bottom, box is horizontally centered */

margin: auto;              /* box is horizontally centered, 0 margin on top and bottom */

Horizontal centering with margin: 0 auto;

To center something horizontally in modern browsers, use display: flex; justify-content: center; .

However, in older browsers like IE8-9, these are not available. In order to center an element inside its parent, use margin: 0 auto;

Specifications

Specification Status Comment
{{ SpecName('CSS3 Box', '#margin', 'margin') }} {{ Spec2('CSS3 Box') }} No significant change
{{ SpecName('CSS3 Transitions', '#animatable-css', 'margin') }} {{ Spec2('CSS3 Transitions') }} Defines margin as animatable.
{{ SpecName('CSS2.1', 'box.html#margin-properties', 'margin') }} {{ Spec2('CSS2.1') }} Removes its effect on inline elements.
{{ SpecName('CSS1', '#margin', 'margin') }} {{ Spec2('CSS1') }} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 {{ CompatGeckoDesktop("1") }} 3.0 3.5 1.0 (85)
auto value 1.0 {{ CompatGeckoDesktop("1") }} 6.0 (strict mode) 3.5 1.0 (85)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 1.0 {{ CompatGeckoMobile("1") }} 6.0 6.0 1.0

See also

Revision Source

<div>{{CSSRef}}</div>

<h2 id="Summary">Summary</h2>

<p>The <strong><code>margin</code></strong> CSS property sets the margin for all four sides. It is a shorthand to avoid setting each side separately with the other margin properties: {{ Cssxref("margin-top") }}, {{ Cssxref("margin-right") }}, {{ Cssxref("margin-bottom") }} and {{ Cssxref("margin-left") }}.</p>

<p>Negative values are also allowed.</p>

<p>{{cssinfo}}</p>

<h2 id="Syntax">Syntax</h2>

<pre class="brush:css">
/* 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;
</pre>

<h3 id="Values">Values</h3>

<p>Accepts one, two, three or four values of the following:</p>

<dl>
 <dt><code>&lt;percentage&gt;</code></dt>
 <dd>Specifies a fixed width. Negative Values are allowed. See for {{cssxref("")}} possible units.</dd>
 <dt><code>&lt;percentage&gt;</code></dt>
 <dd>A {{cssxref("&lt;percentage&gt;")}} relative to the <strong>width</strong> of the containing block. Negative values are allowed.</dd>
 <dt><code>auto</code></dt>
 <dd><code>auto </code>is replaced by some suitable value, e.g. it can be used for centering of blocks.<br />
 <code>div { width:50%;&nbsp; margin:0 auto; }</code> centers the div container horizontally.</dd>
</dl>

<ul>
 <li><strong>One</strong> single value applies to all <strong>four sides</strong>.</li>
 <li><strong>Two</strong> values apply first to <strong>top and bottom</strong>, the second one to <strong>left and right</strong>.</li>
 <li><strong>Three</strong> values apply first to <strong>top</strong>, second to <strong>left and right</strong> and third to <strong>bottom</strong>.</li>
 <li><strong>Four</strong> values apply to <strong>top</strong>, <strong>right</strong>, <strong>bottom</strong> and <strong>left</strong> in that order (clockwise).</li>
</ul>

<h3 id="Formal_syntax">Formal syntax</h3>

<pre class="syntaxbox">
{{csssyntax}}</pre>

<h2 id="Examples">Examples</h2>

<h3 id="Simple_example">Simple example</h3>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">
&lt;div class="ex1"&gt;
&nbsp; margin:&nbsp;&nbsp;&nbsp;&nbsp; auto;
&nbsp; background: gold;
  width:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 66%;
&lt;/div&gt;
&lt;div class="ex2"&gt;
 &nbsp;margin:&nbsp;&nbsp;&nbsp;&nbsp; 20px 0 0 -20px;
&nbsp; background: gold;
  width:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 66%;
&lt;/div&gt;</pre>

<h4 id="CSS">CSS</h4>

<pre class="brush: css; highlight:[2,7]">
.ex1 {
&nbsp; margin: auto;
&nbsp; background: gold;
&nbsp; width: 66%;
}
.ex2 {
&nbsp; margin: 20px 0px 0 -20px;
&nbsp; background: gold;
  width: 66%;
}</pre>

<p>{{ EmbedLiveSample('Simple_example') }}</p>

<h3 id="Another_example">Another example</h3>

<pre class="brush: css">
margin: 5%;                /* all sides 5% margin */

margin: 10px;              /* all sides 10px margin */

margin: 1.6em 20px;        /* top and bottom 1.6em, left and right 20px margin */

margin: 10px 3% 1em;       /* top 10px, left and right 3%, bottom 1em margin */

margin: 10px 3px 30px 5px; /* top 10px, right 3px, bottom 30px, left 5px margin */

margin: 1em auto;          /* 1em margin on top and bottom, box is horizontally centered */

margin: auto;              /* box is horizontally centered, 0 margin on top and bottom */
</pre>

<h2 id="Horizontal_centering_with_margin_0_auto">Horizontal centering with <code>margin: 0 auto;</code></h2>

<p>To center something horizontally in modern browsers, use <code><a href="/en-US/docs/Web/CSS/display">display</a>: flex; <a href="/en-US/docs/Web/CSS/justify-content">justify-content</a>: center;</code> .</p>

<p>However, in older browsers like IE8-9, these are not available. In order to center an element inside its parent, use <code>margin: 0 auto;</code></p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('CSS3 Box', '#margin', 'margin') }}</td>
   <td>{{ Spec2('CSS3 Box') }}</td>
   <td>No significant change</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS3 Transitions', '#animatable-css', 'margin') }}</td>
   <td>{{ Spec2('CSS3 Transitions') }}</td>
   <td>Defines <code>margin</code> as animatable.</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS2.1', 'box.html#margin-properties', 'margin') }}</td>
   <td>{{ Spec2('CSS2.1') }}</td>
   <td>Removes its effect on inline elements.</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS1', '#margin', 'margin') }}</td>
   <td>{{ Spec2('CSS1') }}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{ CompatGeckoDesktop("1") }}</td>
   <td>3.0</td>
   <td>3.5</td>
   <td>1.0 (85)</td>
  </tr>
  <tr>
   <td><code>auto</code> value</td>
   <td>1.0</td>
   <td>{{ CompatGeckoDesktop("1") }}</td>
   <td>6.0 (strict mode)</td>
   <td>3.5</td>
   <td>1.0 (85)</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{ CompatGeckoMobile("1") }}</td>
   <td>6.0</td>
   <td>6.0</td>
   <td>1.0</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a class="internal" href="/en/CSS/margin_collapsing" title="en/CSS/margin collapsing">Margin collapsing</a></li>
</ul>
Revert to this revision