Open In App

HTML DOM Style marginTop Property

The Style marginTop property in HTML DOM is used to set or return the top margin of an element. 

Syntax:



object.style.marginTop
object.style.marginTop = "length|percentage|auto|initial|
inherit"

Return Values: It returns a string value that represents the top margin of an element

Property Values:



Example: In this example, we are using object.style.marginTop = length property.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Style marginTop Property
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <b>DOM Style marginTop Property</b>
 
    <div class="container">
        <div class="div1">Line One</div>
        <div class="div2">Line Two</div>
 
        <button onclick="setMargin()">
            Change marginTop
        </button>
    </div>
 
    <!-- Script to set top margin -->
    <script>
        function setMargin() {
            elem = document.querySelector('.div1');
            elem.style.marginTop = '50px';
        }
    </script>
</body>
 
</html>

Output:

 

Example: In this example, we are using object.style.marginTop = percentage property.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Style marginTop Property
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <b>DOM Style marginTop Property</b>
 
    <div class="container">
        <div class="div1">Line One</div>
        <div class="div2">Line Two</div>
 
        <button onclick="setMargin()">
            Change marginTop
        </button>
    </div>
 
    <!-- Script to set top margin -->
    <script>
        function setMargin() {
            elem = document.querySelector('.div1');
            elem.style.marginTop = '20%';
        }
    </script>
</body>
 
</html>

Output:

 

Example: In this example, we are using object.style.marginTop = auto property.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Style marginTop Property
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <b>DOM Style marginTop Property</b>
 
    <div class="container">
        <div class="div1" style="margin-top: 50px;">
            Line One
        </div>
 
        <div class="div2">
            Line Two
        </div>
 
        <button onclick="setMargin()">
            Change marginTop
        </button>
    </div>
 
    <!-- Script to set top margin -->
    <script>
        function setMargin() {
            elem = document.querySelector('.div1');
            elem.style.marginTop = 'auto';
        }
    </script>
</body>
 
</html>

Output:

 

Example: In this example, we are using object.style.marginTop = initial property.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Style marginTop Property
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <b>DOM Style marginTop Property</b>
 
    <div class="container">
 
        <div class="div1"
             style="margin-top: 50px;">
            Line One
        </div>
 
        <div class="div2">
            Line Two
        </div>
 
        <button onclick="setMargin()">
            Change marginTop
        </button>
    </div>
 
    <!-- Script to set top margin -->
    <script>
        function setMargin() {
            elem = document.querySelector('.div1');
            elem.style.marginTop = 'initial';
        }
    </script>
</body>
 
</html>

Output:

 

Example: In this example, we are using object.style.marginTop = inherit property.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Style marginTop Property
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <b>DOM Style marginTop Property</b>
 
    <div class="container">
        <div class="div1"
             style="margin-top: 50px;">
            Line One
        </div>
 
        <div class="div2">
            Line Two
        </div>
 
        <button onclick="setMargin()">
            Change marginTop
        </button>
    </div>
 
    <!-- Script to set top margin -->
    <script>
        function setMargin() {
            elem = document.querySelector('.div1');
            elem.style.marginTop = 'inherit';
        }
    </script>
</body>
 
</html>

Output:

 

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


Article Tags :