HTML DOM Style top Property


The HTML DOM Style top property returns and modify the top position of a positioned HTML element in an HTML document.

Syntax

Following is the syntax −

1. Returning top

object.top

2. Modifying top

object.top = “value”

Here, value can be −

Sr.NoValue & Explanation
1initial
It set this property value to its default value.
2inherit
It inherits this property value from its parent element.
3percentage(%)
It defines value in percentage of the width of the parent element.
4length
It define value top in length unit.
5auto
It lets the browser set the value of top position.

Let us see an example of HTML DOM Style top Property −

Example

Live Demo

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem 0;
   }
   .square {
      width: 100px;
      height: 100px;
      background: #db133a6b;
      position: relative;
   }
   .show {
      font-size: 1.2rem;
      margin: 1rem 0;
   }
</style>
<body>
<h1>DOM Style top Property Demo</h1>
<div class='square'></div>
<button onclick="set()" class="btn">Set top position</button>
<script>
   function set() {
      document.querySelector('.square').style.top = "200px";
   }
</script>
</body>
</html>

Output

Click on “Set top position” button to set the top position of pink square.

Updated on: 17-Feb-2021

30 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements