HTML | DOM Style margin Property

The DOM Style margin Property is used to sets or returns the margin of an element.We can set the different size of margins for individual sides(top, right, bottom, left).

Margin properties can have following values:

  1. Length in cm, px, pt, etc.
  2. Width % of the element.
  3. Margin calculated by the browser: auto.

Syntax: It is used to set the margin Property.

object.style.margin = "%|length|auto|initial|inherit" 

Return Value: It returns a string value which represent the top, right, bottom, left margin of an element.

Example-1:Margin property set to four values:

margin: 80px 40px 20px 90px;
1. top = 80px
2. right = 40px
3. bottom = 20px
4. left = 90px

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <style>
        h1 {
            coor: green;
        }
          
        #GFG {
            border: 2px solid green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1
        GEEKSFORGEEKS 
    </h1>
        <h2>
        DOM Style margin Property
    </h2>
        <p id="GFG">
            Margin properties
        </p>
        <br>
        <BUTTON ONCLICK="Geeks()">Submit</BUTTON>
        <script>
            function Geeks() {
                document.getElementById("GFG").style.margin = "80px 40px 20px 90px";
            }
        </script>
  
</body>
  
</html>                                 

chevron_right


Output :
Before click on the button:

After click on the button:

Example-2: Change all four margin to a single margin:
margin: 25px; denotes top, right, bottom and left = 25px

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <style>
        h1 {
            coor: green;
        }
          
        #GFG {
            border: 2px solid green;
            margin: 60px 20px 90px 100px;
        }
    </style>
</head>
  
<body>
    <center>
        <h1
        GEEKSFORGEEKS 
    </h1>
        <h2>
        DOM Style margin Property
    </h2>
        <p id="GFG">
            Margin properties
        </p>
        <br>
        <BUTTON ONCLICK="Geeks()">Submit</BUTTON>
        <script>
            function Geeks() {
                document.getElementById("GFG").style.margin = "25px";
            }
        </script>
  
</body>
  
</html>
                                         

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

Supported Browsers: The browser supported by DOM Style margin property are listed below:

  • Apple Safari 1.0
  • Google Chrome 1.0
  • Firefox 1.0
  • Opera 3.5
  • Internet Explorer 4.0


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.