Documentos de Académico
Documentos de Profesional
Documentos de Cultura
La propiedad CSS margin 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:
margin-top , margin-right , margin-bottom y margin-left .
Applies to all elements, except elements with table display types other
than table-caption , table and inline-table . It also
applies to ::first-letter .
Heredable no
Media visual
Valor calculado as each of the properties of the shorthand:
margin-bottom : the percentage as specified or the
absolute length
margin-left : the percentage as specified or the
absolute length
margin-right : the percentage as specified or the
absolute length
margin-top : the percentage as specified or the
absolute length
Sintaxis
/* Vertical | Horizontal */
margin: 5% auto;
/* Valores globales */
margin: inherit;
margin: initial;
margin: unset;
Valores
Acepta uno, dos , tres o cuatro valores de los siguientes:
<length>
Especifica un ancho fijo. Valores negativos son permitidos. Mira <length> para conocer las
posibles unidades.
<percentage>
Un <percentage> relativo al ancho del bloque contenedor. Se permiten valores negativos.
auto
auto es reemplazado por algún valor apropiado. Por ejemplo, puede usarse para
centrar horizontalmente un elemento bloque.
div { width:50%; margin:0 auto; } centrará el div horizontalmente.
Sintaxis formal
Ejemplos
Ejemplo simple
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%;
}
Otro ejemplo
margin: 10px 3px 30px 5px; /* 10px arriba, 3px derecha, 30px abajo, 5px izq
Sin embargo, en navegadores antiguos como IE8-9, flexbox no está disponible. Para poder
centrar un elemento horizontalmente con respecto a su contenedor, usa margin: 0 auto;
Especificaciones
Specification Status Comment
CSS Basic Box Model
La definición de 'margin' en esta Working Draft No significant change
especificación.
CSS Transitions
La definición de 'margin' en esta Working Draft Defines margin as animatable.
especificación.
CSS Level 1
La definición de 'margin' en esta Recommendation Initial definition
especificación.
Compatibilidad en navegadores
We're converting our compatibility data into a machine-readable JSON format. This
compatibility table still uses the old format, because we haven't yet converted the data it
contains. Find out how you can help!
Escritorio Móvil
auto value 1.0 1.0 (1.7 o anterior) 6.0 (strict mode) 3.5 1.0 (85)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 1.0 1.0 (1) 6.0 6.0 1.0