HTML DOM Style marginRight 属性

2018-08-04 20:41 更新

Style marginRight 属性

Style 对象参考手册 Style 对象

定义和用法

marginRight 属性设置或返回元素的右外边距。

语法

设置 marginRight 属性:

Object.style.marginRight="%|length|auto|inherit"

返回 marginRight 属性:

Object.style.marginRight

描述
% 定义基于父元素宽度的百分比右外边距。
length 使用 px、cm 等单位定义右外边距的宽度。
auto 浏览器设定的右外边距。
inherit 右外边距从父元素继承。


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 marginRight 属性。

注意:IE7 及更早的版本不支持 "inherit" 值。IE8 只有规定了 !DOCTYPE 才支持 "inherit"。IE9 支持 "inherit"。


提示和注释

margin 属性和 padding 属性都是在元素周围插入空间。然而,不同的是,margin 将围绕边框外面插入空间,padding 将在元素边框内插入空间。


实例

实例

更改 div 元素的右外边距:

<html>
<head>
<style type="text/css">
#ex1
{
border: 1px solid #FF0000;
}
</style>
<script>
function displayResult()
{
document.getElementById("ex1").style.marginRight="100px";
}
</script>
</head>
<body>

<div id="ex1">This is some text.</div>
<br>
<button type="button" onclick="displayResult()">Change right margin</button>

</body>
</html>

尝试一下 »


Style 对象参考手册 Style 对象
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号