HTML DOM offsetParent Property



The HTML DOM offsetParent property returns the referenced parent element from which child offsets are defined.

Following is the syntax −

Returning referenced offset parent element

HTMLelement.offsetParent

Let us see an example of HTML DOM offsetParent property −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM offsetParent</title>
<style type="text/css">
   #picForm {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   input[type="button"] {
      border-radius: 10px;
   }
   #containerDiv {
      margin: 0 auto;
   }
</style>
</head>
<body>
   <form id="picForm">
      <fieldset>
         <legend>HTML-DOM-offsetParent</legend>
         <div id="containerDiv">
            <img id="image" src="https://www.tutorialspoint.com/plsql/images/plsql-mini-logo.jpg">
         </div>
         <input type="button" onclick="getParent()" value="Get offsetParent">
         <div id="divDisplay"></div>
      </fieldset>
   </form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var picForm = document.getElementById("picForm");
   var containerDiv = document.getElementById("containerDiv");
   function getParent() {
      divDisplay.textContent = 'Offset for Image form is referenced from: '+picForm.offsetParent;
   }
</script>
</body>
</html>

Output

Before clicking any button −

After clicking ‘Get offsetParent’ button −


Advertisements