Style marginLeft 属性
- Previous Page marginBottom
- Next Page marginRight
- Go to the Previous Level HTML DOM Style Object
Definition and Usage
marginLeft
property sets or returns the left outer margin of an element.
Margin Property and Padding Property All insert space around the element. However, the difference is that margin inserts space around the border, while padding inserts space inside the element's border.
See Also:
CSS Tutorial:CSS Margin
CSS Reference Manual:margin-left Property
HTML DOM Reference Manual:Margin Property
Example 1
Set the left outer margin of the <div> element:
document.getElementById("myDiv").style.marginLeft = "50px";
Example 2
Change the left outer margin of the <div> element back to "normal":
document.getElementById("myDiv").style.marginLeft = "0px";
Example 3
Returns the left outer margin of the <div> element:
alert(document.getElementById("myDiv").style.marginLeft);
Example 4
Difference between marginLeft and paddingLeft:
function changeMargin() { document.getElementById("myDiv").style.marginLeft = "100px"; } function changePadding() { document.getElementById("myDiv2").style.paddingLeft = "100px"; }
Syntax
Return marginLeft property:
object.style.marginLeft
Set marginLeft property:
object.style.marginLeft = "%|length|auto|inherit"
Property Value
Value | Description |
---|---|
% | Define the left outer margin as a percentage of the parent element's width. |
length | Define 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 |
- Previous Page marginBottom
- Next Page marginRight
- Go to the Previous Level HTML DOM Style Object