Skip to content
Related Articles

Related Articles

HTML | DOM Style marginLeft Property
  • Last Updated : 01 Aug, 2019

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:

  • To Set the marginLeft:
    object.style.marginLeft='%|length|auto|initial|inherit'
    
  • To return the marginLeft:
    object.style.marginLeft
    

Values:

valuedescription
%It is used to define the left margin in percentage of the width of its parent
lengthIt is used to define the left margin in terms of length unit
autoIt is used when it is desired that the left margin is set by the browser
initialIt is used to set the value of the property to its default value.
inheritInherits 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:

  • Before Click on the Button:
  • After Click on the Button:

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:

  • Before Click on the Button:
  • After Click on the Button:

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:

  • Before Click on the Button:
  • After Click on the Button:

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

  • Google Chrome
  • Edge
  • Mozilla Firefox
  • Opera
  • Safari
My Personal Notes arrow_drop_up
Recommended Articles
Page :