HTML DOM margin 屬性
定義和用法
margin 屬性設(shè)置元素的外邊距。
該屬性可使用 1 到 4 個值:
- 如果規(guī)定一個值,比如 div {margin: 50px} - 所有的外邊距都是 50 px
- 如果規(guī)定兩個值,比如 div {margin: 50px 10px} - 上下外邊距是 50px,左右外邊距是 10 px。
- 如果規(guī)定三個值,比如 div {margin: 50px 10px 20px}- 上外邊距是 50 px,而左右外邊距是 10 px,下外邊距是 20 px。
- 如果規(guī)定四個值,比如 div {margin: 50px 10px 20px 30px} - 上外邊距是 50 px,右外邊距是 10 px,下外邊距是 20 px,左外邊距是 30 px。
語法:
Object.style.margin=margin
可能的值:
值 | 描述 |
---|---|
margin |
設(shè)置外邊距。 值可以是:
|
實例
下面的例子改變了段落的外邊距:
<html>
<head>
<script type="text/javascript">
function changeMargin()
{
document.getElementById("p1").style.margin="100px";
}
</script>
</head>
<body>
<input type="button" onclick="changeMargin()"
value="Change margins of a paragraph" />
<p id="p1">This is a paragraph</p>
</body>
</html>