Open In App

HTML | DOM Style marginLeft Property

The DOM Style marginLeft property is used to set or get the left margin of an element. The margin is used to insert space around the border, unlike padding that is used to insert space within the border of an element. 

Syntax:



object.style.marginLeft='%|length|auto|initial|inherit'
object.style.marginLeft

Values:

value description
% It is used to define the left margin in percentage of the width of its parent
length It is used to define the left margin in terms of length unit
auto It is used when it is desired that the left margin is set by the browser
initial It is used to set the value of the property to its default value.
inherit Inherits the value of this property i.e sets the value same as that of the parent

Default Value: The default value of this property is 0. 



Return Value: A string which represent the left margin. 

Example-1: Return left margin. 




<!DOCTYPE html>
<html>
 
<head>
    <title>    
        HTML | DOM Style marginLeft Property
    </title>
    <style>
        #d {
            border: 2px solid black;
        }
    </style>
</head>
 
<body>
 
    <div id="d" style="margin-left:3cm;">
      GEEKS FOR GEEKS  A Computer Science Portal For Geeks.
    </div>
    <br>
    <button type="button" onclick="myFunction()">
        Return left margin
    </button>
 
    <script>
        function myFunction() {
            // Returning left margin.
            alert(document.getElementById("d").style.marginLeft);
        }
    </script>
 
</body>
 
</html>

Output:

Example-2: Set left margin using length unit. 




<!DOCTYPE html>
<html>
 
<head>
    <title>    
        HTML | DOM Style marginLeft Property
    </title>
    <style>
        #d {
            border: 2px solid black;
        }
    </style>
</head>
 
<body>
 
    <div id="d">
      GEEKS FOR GEEKS A Computer Science Portal For Geeks.
    </div>
    <br>
   
    <button type="button" onclick="myFunction()">
      Set leftmargin
    </button>
 
    <script>
        function myFunction() {
           
            //  Set left margin.
            document.getElementById("d").style.marginLeft = "10px";
        }
    </script>
 
</body>
 
</html>

Output:

Example-3: Set left margin using percentage. 




<!DOCTYPE html>
<html>
 
<head>
    <title>    
        HTML | DOM Style marginLeft Property
    </title>
    <style>
        #d {
            border: 2px solid black;
        }
    </style>
</head>
 
<body>
 
    <div id="d">
      GEEKS FOR GEEKS A Computer Science Portal For Geeks.
    </div>
    <br>
   
    <button type="button" onclick="myFunction()">
      Set leftmargin
    </button>
 
    <script>
        function myFunction() {
           
            //  Set left margin.
            document.getElementById("d").style.marginLeft = "10%";
        }
    </script>
 
</body>
 
</html>

Output:

 

Supported Browsers: The browser supported by HTML | DOM Style marginLeft Property are listed below:


Article Tags :