Estilo marginTop Propiedad
Ejemplo
Establezca el margen superior de un elemento <div>:
document.getElementById("myDiv").style.marginTop = "50px";
Definición y uso
La propiedad marginTop establece o devuelve el margen superior 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 | |||||
---|---|---|---|---|---|
marginTop | Yes | Yes | Yes | Yes | Yes |
Sintaxis
Devuelve la propiedad marginTop:
object.style.marginTop
Establezca la propiedad marginTop:
object.style.marginTop = "%|length|auto|initial|inherit"
Valores de propiedad
Value | Description |
---|---|
% | Defines the top margin in % of the width of the parent element |
length | Defines the top margin in length units |
auto | The browser sets the top 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 de retorno: | Una cadena, que representa el margen superior de un elemento. |
Versión CSS | CSS1 |
Más ejemplos
Ejemplo
Cambie el margen superior de un elemento <div> a "normal":
document.getElementById("myDiv").style.marginTop = "0px";
Ejemplo
Devuelve el margen superior de un elemento <div>:
alert(document.getElementById("myDiv").style.marginTop);
Ejemplo
Diferencia entre marginTop y paddingTop:
function changeMargin() {
document.getElementById("myDiv").style.marginTop = "100px";
}
function changePadding() {
document.getElementById("myDiv2").style.paddingTop = "100px";
}
páginas relacionadas
Tutorial CSS: Margen CSS
Referencia CSS: propiedad margin-top
Referencia HTML DOM: propiedad de margen
❮ Objeto de estilo