Skip to content
Related Articles

Related Articles

HTML | DOM Style marginLeft Property

Improve Article
Save Article
  • Last Updated : 08 Aug, 2022
Improve Article
Save Article

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:

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. 

html




<!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. 

html




<!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. 

html




<!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 1
  • Edge 12
  • Internet Explorer 3
  • Mozilla Firefox 1
  • Opera 3.5
  • Safari 1

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!