Style marginRightPropiedad
Objeto Style
Definición y uso
La propiedad marginRight establece o devuelve el margen exterior derecho de un elemento.
Sintaxis
Establecer la propiedad marginRight:
Object.style.marginRight="%|length|auto|inherit"
Devolver la propiedad marginRight:
Object.style.marginRight
| Valor | Descripción |
|---|---|
| % | Define el margen exterior derecho basado en un porcentaje del ancho del elemento padre. |
| length | Define el ancho del margen exterior derecho utilizando unidades como px, cm, etc. |
| auto | El margen exterior derecho establecido por el navegador. |
| inherit | El margen exterior derecho se hereda del elemento padre. |
Soporte del navegador
![]()
![]()
![]()
![]()
![]()
Todos los navegadores principales admiten la propiedad marginRight.
Nota:IE7 y versiones anteriores no admiten el valor 'inherit'. IE8 solo admite 'inherit' si se especifica !DOCTYPE. IE9 admite 'inherit'.
Consejos y notas
Las propiedades margin y padding insertan espacio alrededor de un elemento. Sin embargo, la diferencia es que margin inserta espacio fuera del borde, mientras que padding inserta espacio dentro del borde del elemento.
Ejemplo
Ejemplo
Cambiar el margen exterior derecho de un elemento div:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
<style type="text/css">
div{
border: 1px solid #FF0000;
}
</style>
<script>
function changeMargin(){
document.getElementById("ex1").style.marginRight="100px";
}
function changePadding(){
document.getElementById("ex2").style.paddingRight="100px";
}
</script>
</head>
<body>
<div id="ex1">Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto.</div>
<br>
<button type="button" onclick="changeMargin()">Modificar el margen exterior derecho del elemento div</button>
<br>
<br>
<div id="ex2">Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto.</div>
<br>
<button type="button" onclick="changePadding()">Modificar el relleno interior derecho del elemento div</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
<style type="text/css">
div{
border: 1px solid #FF0000;
}
</style>
<script>
function changeMargin(){
document.getElementById("ex1").style.marginRight="100px";
}
function changePadding(){
document.getElementById("ex2").style.paddingRight="100px";
}
</script>
</head>
<body>
<div id="ex1">Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto.</div>
<br>
<button type="button" onclick="changeMargin()">Modificar el margen exterior derecho del elemento div</button>
<br>
<br>
<div id="ex2">Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto. Esto es un texto.</div>
<br>
<button type="button" onclick="changePadding()">Modificar el relleno interior derecho del elemento div</button>
</body>
</html>
Pruébalo »
Objeto Style Otras extensiones