w3resource

jQuery CSS - Find the offset of an element

jQuery CSS : Exercise-9 with Solution

Find the offset of an element.

Sample Data :

HTML :

<body>
<p></p>
</body>

CSS:

<style>
p {
    margin-top:20px;
    margin-left: 10px;
    padding: 5px;
    border: 2px solid #666;
  }
</style>

Solution:

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Find the offset of an element</title>
</head>
<body>
<p></p>
 </body>
</html>

CSS Code:

p {
    margin-top:20px;
    margin-left: 10px;
    padding: 5px;
    border: 2px solid #666;
  }

JavaScript Code :

console.log("The left & top offset of the Paragraph are:"+$("p").offset().left+',' +$("p").offset().top);

Live Demo:

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


Contribute your code and comments through Disqus.

Previous: Find an element that contains a specified class.
Next: Toggle a specified class when an element is clicked.

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.