top style property

Browser support:
Specifies or returns the top coordinate of a positioned element.
A positioned element is an element whose position property is set to relative, absolute or fixed. The value of the top property specifies the top position of the element including the padding, scrollbar, border and the margin.
Use the left, top, right and bottom properties to specify the position for elements. You can set the size of an element with the width and height properties.
The type of the top property is string.
  • You can set or retrieve the value of the top property as an integer with the pixelTop property. It contains the value in pixels.
  • If you want to get or set the top property as a floating-point number that specifies the value in the current unit type of the top property, use the posTop property.
The properties mentioned above can be used to access style settings. If you need the top position of a rendered element, you can use the clientTop, offsetTop and scrollTop properties and the getBoundingClientRect method.

Syntax:

object.top;
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: top

Possible values:

The type of this property is string.
 One of the following values: 
auto
Default. The top position of the object is in accordance with the default layout of the page.
inherit
Takes the value of this property from the computed style of the parent element.
position in length
The top position of the element in length units. For the supported length units, see the length page.
position in percentage
The top position is the specified percentage of the height of the parent object.
Default: auto.

Example HTML code 1:

This example illustrates the use of the top property:
<head>
    <style>
        .example {
            position: relative;
            top: 50px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div style="border-top: 5px solid #408080;">Center-line</div>
    <div class="example">The top of this is 50px under the center line</div>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the top property in JavaScript:
<head>
    <style>
        #myDiv {
            position: absolute; 
            left: 100px; 
            top: 60px;
            width: 200px;
            height: 50px;
            background-color: #c00000;
        }
    </style>
    <script type="text/javascript">
        function ChangeTop () {
            var div = document.getElementById ("myDiv");
            var input = document.getElementById ("myInput");

            div.style.top = input.value + "px";
        }
    </script>
</head>
<body>
    <div id="myDiv">
        An absolute positioned division
    </div>

    <input id="myInput" type="text" value="60" />
    <button onclick="ChangeTop ();">Change the top position!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content