Style marginLeft Attribute

Definition and Usage

marginLeft sets or returns the left outer margin of an element.

margin Property and padding Property Both insert space around the element. However, the difference is that margin inserts space around the border, while padding inserts space within the element's border.

See Also:

CSS Tutorial:CSS Margin

CSS Reference Manual:margin-left Property

HTML DOM Reference Manual:margin Property

Example 1

Sets the left outer margin of the <div> element:

document.getElementById("myDiv").style.marginLeft = "50px";

Try It Yourself

Example 2

Changes the left outer margin of the <div> element back to "normal":

document.getElementById("myDiv").style.marginLeft = "0px";

Try It Yourself

Example 3

Returns the left outer margin of the <div> element:

alert(document.getElementById("myDiv").style.marginLeft);

Try It Yourself

Example 4

Difference between marginLeft and paddingLeft:

function changeMargin() {
  document.getElementById("myDiv").style.marginLeft = "100px";
}
function changePadding() {
  document.getElementById("myDiv2").style.paddingLeft = "100px";
}

Try It Yourself

Syntax

Returns the marginLeft property:

object.style.marginLeft

Sets the marginLeft property:

object.style.marginLeft = "%|length|auto|inherit"

Property Value

Value Description
% Defines the left outer margin as a percentage of the parent element's width.
length Defines the left outer margin with a length unit.
auto Browser sets the left outer margin.
initial Sets this property to its default value. See initial.
inherit Inherits this property from its parent element. See inherit.

Technical Details

Default Value: 0
Return Value: A string that represents the left outer margin of an element.
CSS Version: CSS1

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support