39 of 119 menu

The offsetParent method

The offsetParent method allows you to get the closest positioned ancestor of an element. 'positioned' refers to an element with the CSS property position set to relative, absolute, or fixed. This method is useful for animation and when placing elements on a page.

Syntax

Get the closest positioned ancestor:

$(selector).offsetParent();

Example

Let's find the closest positioned (in our case, relatively) ancestor of the paragraph with #test and color its background green:

<div> <p>aaa</p> <div style="position: relative;"> <p>bbb</p> <p>bbb</p> <p>bbb</p> <div> <p id="test">ccc</p> <div> <p>ddd</p> <p>ddd</p> <p>ddd</p> </div> <p>ccc</p> <p>ccc</p> </div> </div> <p>aaa</p> <p>aaa</p> </div> $('#test').offsetParent().css('background-color', 'green');

See also

  • method parents,
    which allows you to get all the parents of an element
  • method closest,
    which allows you to get the closest parent element that satisfies the selector
  • method position,
    which allows you to get the current coordinates of an element
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline