Access the offset of the second paragraph:
<title>offset demo</title>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
<p>Hello</p><p>2nd Paragraph</p>
p.html( "left: " + offset.left + ", top: " + offset.top );
<title>offset demo</title>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
<div id="result">Click an element.</div>
This is the best way to <span>find</span> an offset.
$( "*", document.body ).click(function( event ) {
var offset = $( this ).offset();
$( "#result" ).text( this.tagName +
" coords ( " + offset.left + ", " + offset.top + " )" );
The
.offset()
setter method allows us to reposition an element. The element's border-box position is specified
relative to the document
. If the element's
position
style property is currently
static
, it will be set to
relative
to allow for this repositioning.
Set the offset of the second paragraph:
<title>offset demo</title>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
<p>Hello</p><p>2nd Paragraph</p>
$( "p" ).last().offset({ top: 10, left: 30 });