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:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example-2: Set left margin using length unit.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example-3: Set left margin using percentage.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.