jQuery offsetParent() method

The offsetParent() method in jQuery is used to return the closest positioned ancestor element. The element can be positioned with jQuery or by using the CSS position property (absolute, relative, or fixed).

Syntax

The offsetParent() method does not accepts any arguments.

Let's see an illustration to understand the working of the offsetParent() method.

Example

It is a simple example of using the offsetParent() method. In this example, there is a div element that contains a relatively positioned heading h5 and a paragraph element. We are using the offsetParent() method to get the first positioned parent of the p element. The heading h5 is the first positioned parent of the p element. So, on clicking the given button, the background color of the first positioned parent of p, which is heading h5, will get changed.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery offsetParent() method

After clicking the given button, the output will be -

jQuery offsetParent() method




Contact US

Email:[email protected]

jQuery offsetParent() method
10/30