Propiedad marginLeft de estilo
Ejemplo
Establezca el margen izquierdo de un elemento <div>:
document.getElementById("myDiv").style.marginLeft = "50px";
Definición y uso
La propiedad marginLeft establece o devuelve el margen izquierdo de un elemento.
Tanto la propiedad de margen como la propiedad de relleno insertan espacio alrededor de un elemento. Sin embargo, la diferencia es que margin inserta el espacio alrededor del borde, mientras que padding inserta el espacio dentro del borde de un elemento.
Compatibilidad con navegador
Property | |||||
---|---|---|---|---|---|
marginLeft | Yes | Yes | Yes | Yes | Yes |
Sintaxis
Devuelve la propiedad marginLeft:
object.style.marginLeft
Establezca la propiedad marginLeft:
object.style.marginLeft = "%|length|auto|inherit"
Valores de propiedad
Value | Description |
---|---|
% | Defines the left margin in % of the width of the parent element |
length | Defines the left margin in length units |
auto | The browser sets the left margin |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Detalles técnicos
Valor por defecto: | 0 |
---|---|
Valor devuelto: | Una cadena, que representa el margen izquierdo de un elemento. |
Versión CSS | CSS1 |
Más ejemplos
Ejemplo
Cambie el margen izquierdo de un elemento <div> a "normal":
document.getElementById("myDiv").style.marginLeft = "0px";
Ejemplo
Devuelve el margen izquierdo de un elemento <div>:
alert(document.getElementById("myDiv").style.marginLeft);
Ejemplo
Diferencia entre marginLeft y paddingLeft:
function changeMargin() {
document.getElementById("myDiv").style.marginLeft = "100px";
}
function changePadding() {
document.getElementById("myDiv2").style.paddingLeft = "100px";
}
páginas relacionadas
Tutorial CSS: Margen CSS
Referencia CSS: propiedad margin-left
Referencia HTML DOM: propiedad de margen
❮ Objeto de estilo