aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/css/margin/index.html
blob: 684288ac2fa924df1d50b334c451f022aa09d18e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
title: margin
slug: Web/CSS/margin
translation_of: Web/CSS/margin
translation_of_original: Web/CSS/margin-new
---
<div>{{CSSRef}}</div>

<h2 id="Resumen">Resumen</h2>

<p>La propiedad CSS <strong><code>margin </code></strong>establece el margen para los cuatro lados. Es una abreviación para evitar tener que establecer cada lado por separado con las otras propiedades de margen:  {{ cssxref("margin-top") }}, {{ cssxref("margin-right") }}, {{ cssxref("margin-bottom") }} y {{ cssxref("margin-left") }}.</p>

<p>También se permiten valores negativos.</p>

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

<h2 id="Sintaxis">Sintaxis</h2>

<pre class="brush:css">/* Aplica a todos los cuatro lados */
margin: 1em;

/* Vertical | Horizontal */
margin: 5% auto;

/* Arriba | Horizontal | Abajo */
margin: 1em auto 2em;

/* Arriba | Derecha | Abajo | Izquierda */
margin: 2px 1em 0 auto;

/* Valores globales */
margin: inherit;
margin: initial;
margin: unset;
</pre>

<h3 id="Valores">Valores</h3>

<p>Acepta uno, dos , tres  o cuatro valores de los siguientes:</p>

<dl>
 <dt><code>&lt;length&gt;</code></dt>
 <dd>Especifica un ancho fijo. Valores negativos son permitidos. Mira {{cssxref("&lt;length&gt;")}} para conocer las posibles unidades.</dd>
 <dt><code>&lt;percentage&gt;</code></dt>
 <dd>Un {{cssxref("&lt;percentage&gt;")}} relativo al <strong>ancho </strong>del bloque contenedor. Se permiten valores negativos.</dd>
 <dt><code>auto</code></dt>
 <dd><code>auto es reemplazado por algún valor</code> apropiado. Por ejemplo, puede usarse para centrar horizontalmente un elemento bloque.<br>
 <code>div { width:50%;  margin:0 auto; }</code> centrará el div horizontalmente.</dd>
</dl>

<ul>
 <li><strong>Un</strong> único valor aplicará para todos los <strong>cuatro lados</strong>.</li>
 <li><strong>Dos</strong> valores aplicarán: El primer valor para <strong>arriba y abajo</strong>, el segundo valor para <strong>izquierda y derecha</strong>.</li>
 <li><strong>Tres</strong>  valores aplicarán: El primero para <strong>arriba</strong>, el segundo para <strong>izquierda y derecha</strong>, el tercero para <strong>abajo</strong>.</li>
 <li><strong>Cuatro </strong>valores aplicarán en sentido de las manecillas del reloj empezando desde arriba. (<strong>Arriba, derecha, abajo, izquierda</strong>)</li>
</ul>

<h3 id="Sintaxis_formal">Sintaxis formal</h3>

{{csssyntax}}

<h2 id="Ejemplos">Ejemplos</h2>

<h3 id="Ejemplo_simple">Ejemplo simple</h3>

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

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

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

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

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

<h3 id="Otro_ejemplo">Otro ejemplo</h3>

<pre class="brush: css">margin: 5%;                /* 5% para todos los lados */

margin: 10px;              /* 10px para todos los lados */

margin: 1.6em 20px;        /* 1.6em arriba y abajo, 20px izquierda y derecha */

margin: 10px 3% 1em;       /* 10px arriba, 3% izquierda y derecha, 1em abajo */

margin: 10px 3px 30px 5px; /* 10px arriba, 3px derecha, 30px abajo, 5px izquierda */

margin: 1em auto;          /* 1em arriba y abajo, centrado horizontalmente */

margin: auto;              /* 0px de margen vertical, centrado horizontalmente */
</pre>

<h2 id="Centrado_horizontal_con_margin_0_auto">Centrado horizontal con  <code>margin: 0 auto;</code></h2>

<p>Para centrar algo horizontalmente en navegadores modernos, usa <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>Sin embargo, en navegadores antiguos como IE8-9, <strong>flexbox</strong> no está disponible. Para poder centrar un elemento horizontalmente con respecto a su contenedor, usa <code>margin: 0 auto;</code></p>

<h2 id="Especificaciones">Especificaciones</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="Compatibilidad_en_navegadores">Compatibilidad en navegadores</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="sect1"> </h2>

<h2 id="Ver_también">Ver también</h2>

<ul>
 <li><a href="es/docs/Web/CSS/CSS_Modelo_Caja/Mastering_margin_collapsing">Entendiendo el colapso de margen</a></li>
</ul>