THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Style marginTop Property

Style Object Reference Style Object

Example

Set the top margin of a <div> element:

document.getElementById("myDiv").style.marginTop = "50px";
Try it yourself »

Definition and Usage

The marginTop property sets or returns the top margin of an element.

Both the margin property and the padding property insert space around an element. However, the difference is that margin inserts the space around the border, while padding inserts the space within the border of an element.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The marginTop property is supported in all major browsers.


Syntax

Return the marginTop property:

object.style.marginTop

Set the marginTop property:

object.style.marginTop="%|length|auto|initial|inherit"

Property Values

Value Description
% Defines the top margin in % of the width of the parent element
length Defines the top margin in length units
auto The browser sets the top margin
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

Technical Details

Default Value: 0
Return Value: A String, representing the top margin of an element
CSS Version CSS1

More Examples

Example

Change the top margin of a <div> element back to "normal":

document.getElementById("myDiv").style.marginTop="0px";
Try it yourself »

Example

Return the top margin of a <div> element:

alert(document.getElementById("myDiv").style.marginTop);
Try it yourself »

Example

Difference between marginTop and paddingTop:

function changeMargin()
{
document.getElementById("myDiv").style.marginTop="100px";
}
function changePadding()
{
document.getElementById("myDiv2").style.paddingTop="100px";
}
Try it yourself »

Related Pages

CSS tutorial: CSS Margin

CSS reference: margin-top property

HTML DOM reference: margin property


Style Object Reference Style Object