Open In App

HTML DOM Style top Property

The HTML DOM Style top Property is used to set or return the top position of a positioned element including padding, scrollbar, border, and margin. 

Syntax:

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

Property Values:

Return Value: A String, representing the top position of a positioned element. 

Example 1: In this example, we will use the HTML DOM Style top Property to set the top position of element.

<!DOCTYPE html>
<html>

<head>
    <title>
        HTML DOM Style top Property
    </title>

    <style>
        #myBtn {
            position: absolute;
        }
    </style>
</head>

<body>
    <h2>
        HTML DOM Style top Property
    </h2>

    <button id="myBtn" onclick="myFunction()">
        Click here!
    </button>

    <script>
        function myFunction() {
            document.getElementById("myBtn")
                .style.top = "100px";
        }
    </script>
</body>

</html>

Output:

HTML-DOM-Style-top-Property

Example 2: In this example, we will use the HTML DOM Style top Property to set the top position of element.

<!DOCTYPE html>
<html>

<head>
    <title>
        HTML DOM Style top Property
    </title>

    <style>
        #GfG {
            border: 1px solid black;
            width: 250px;
            text-align: center;
            position: relative;
        }
    </style>
</head>

<body>
    <h2>HTML DOM Style top Property</h2>

    <div id="GfG">Welcome to GeeksforGeeks</div>
    <br>

    <button type="button" onclick="myFunction()">
        Click Here!
    </button>

    <script>
        function myFunction() {
            document.getElementById("GfG").style.top = "-20px";
        }
    </script>
</body>

</html>

Output:

HTML-DOM-Style-top-Property-2

Supported Browsers:

Article Tags :