/

HTML / CSS / JavaScript Tutorial

CSS Property: margin-left

[this page | pdf | back links]

The CSS (CSS1) margin-left property sets the width of the left margin of an element.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

length

Any specified margin size as a CSS length

%

Margin specified as percentage of width of container

auto

Browser calculates margin

 

Default Value:

0

JavaScript syntax:

e.g. object.style.marginLeft="30px"

Inherited:

No

Animatable:

Yes

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
p {border: 2px solid red; width: 400px}
span {border: 2px solid green;}
.x1 {margin-left: 50px;}
</style>
</head>
<body>
Element with default property<br>
<p><span>1</span><span>2</span><span>3</span></p>
Property set using in-file HTML style<br>
<p><span>1</span><span class="x1">2</span><span>3</span></p><br><br>
Property set using JavaScript<br>
<p><span>1</span><span id="x2">2</span><span>3</span></p>

<script>
document.getElementById("x2").style.marginLeft = "50px";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyMarginLeft() {
  var x = document.createElement("DIV"); x.style.marginLeft = "50px"; return (window.getComputedStyle(x, null).marginLeft == "50px");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile