Style marginTop Property
The Style object
Definition and Usage
The marginTop property sets or returns the top margin of an element.
Syntax
Sets the marginTop property:
Object.style.marginTop="
%
|
length
|auto|inherit"
Returns the marginTop property:
Object.style.marginTop
| Value | Description |
|---|---|
| % | Defines a percentage top margin based on the width of the parent element. |
| length | Defines the width of the top margin using units such as px, cm, etc. |
| auto | The top margin set by the browser. |
| inherit | The top margin is inherited from the parent element. |
Browser support





All major browsers support the marginTop property.
**Note:** IE7 and earlier versions do not support the "inherit" value. IE8 supports "inherit" only if a !DOCTYPE is specified. IE9 supports "inherit".
Tips and Notes
Both the margin property and the padding property insert space around an element. However, they differ: margin inserts space outside the border, while padding inserts space inside the border.
Example
Example
Changes the top margin of a div element:
This is some text.
This is some text.
Try it »
The Style object