w3resource

jQuery CSS - Access the position of an element

jQuery CSS : Exercise-5 with Solution

Access the position of an element.

Test Data :

 <body>
 <div style="height:75px;width:200px;padding:10px;margin:3px;border:1px solid blue;background-color:#F3E2A9;">
 </div>
 <p></p>
 </body>

Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Access the position of an element</title>
</head>
<body>
<div style="height:75px;width:200px;padding:10px;margin:3px;border:1px solid blue;background-color:#F3E2A9;"></div>
<p></p>
</body>
</html>

JavaScript Code :

var position = $("div").position();

$( "p" ).text( "Left : " + position.left + ", Top : " + position.top );

Live Demo:

See the Pen jquery-css-exercise-5 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Get the scroll top and left position of an element.
Next: Find the inner height and inner width of an element.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.