HTML DOM Style marginTop property
Definition and Usage
marginTop property sets or returns the element on the margin.
grammar
Setting marginTop properties:
Object.style.marginTop="%|length|auto|inherit"
Back marginTop properties:
Object.style.marginTop
值 | 描述 |
---|---|
% | 定义基于父元素宽度的百分比上外边距。 |
length | 使用 px、cm 等单位定义上外边距的宽度。 |
auto | 浏览器设定的上外边距。 |
inherit | 上外边距从父元素继承。 |
Browser Support
All major browsers support marginTop property.
Note: IE7 and earlier versions do not support the "inherit" value.IE8 only provides! DOCTYPE supported "inherit". IE9 support "inherit".
Tips and Notes
margin properties and padding properties are inserted into the space around the element. However, the difference is, margin will be inserted into the space around the outside of the border, padding inserts space within the element border.
Examples
Examples
Change div element on the margin:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "utf-8">
<Title> This tutorial (w3big.com) </ title>
<Style type = "text / css">
div {
border: 1px solid # FF0000;
}
</ Style>
<Script>
function changeMargin () {
document.getElementById ( "ex1") style.marginTop = "100px".;
}
function changePadding () {
document.getElementById ( "ex2") style.paddingTop = "100px".;
}
</ Script>
</ Head>
<Body>
<Div id = "ex1"> This is some text. </ Div>
<br>
<Button type = "button" onclick = "changeMargin ()"> Modify div element at the top of the outer spacing </ button>
<br>
<br>
<Div id = "ex2"> This is some text. </ Div>
<br>
<Button type = "button" onclick = "changePadding ()"> modifications within the top of the div element spacing </ button>
</ Body>
</ Html>
<Html>
<Head>
<Meta charset = "utf-8">
<Title> This tutorial (w3big.com) </ title>
<Style type = "text / css">
div {
border: 1px solid # FF0000;
}
</ Style>
<Script>
function changeMargin () {
document.getElementById ( "ex1") style.marginTop = "100px".;
}
function changePadding () {
document.getElementById ( "ex2") style.paddingTop = "100px".;
}
</ Script>
</ Head>
<Body>
<Div id = "ex1"> This is some text. </ Div>
<br>
<Button type = "button" onclick = "changeMargin ()"> Modify div element at the top of the outer spacing </ button>
<br>
<br>
<Div id = "ex2"> This is some text. </ Div>
<br>
<Button type = "button" onclick = "changePadding ()"> modifications within the top of the div element spacing </ button>
</ Body>
</ Html>
try it"
Style Objects