YouTip LogoYouTip

Prop Element Offsetparent

# HTML DOM offsetParent Property The `offsetParent` is a read-only property of the HTML DOM `Element` interface. It returns a reference to the nearest ancestor element that has a layout position other than `static` (i.e., its CSS `position` is set to `relative`, `absolute`, `fixed`, or `sticky`), or a few other specific container elements. This property is highly useful when calculating the absolute coordinates of an element on a webpage, as it serves as the reference point for the (prop-element-offsetleft.html) and (prop-element-offsettop.html) properties. --- ## Syntax ```javascript let parent = element.offsetParent; ``` ### Return Value * **Element Object**: The closest positioned ancestor element. * **`null`**: Returned if any of the following conditions are met: * The element or any of its ancestors has its CSS `display` property set to `"none"`. * The element's CSS `position` is set to `fixed` (in most modern browsers). * The element is the `` or `` element. --- ## How offsetParent is Determined To find the `offsetParent`, the browser searches up the DOM tree for the nearest ancestor that meets any of these criteria: 1. Has a CSS `position` other than `static` (e.g., `relative`, `absolute`, `sticky`). 2. Is a ``, ``, or `` element. 3. Is the `` element. --- ## Code Examples ### Example 1: Basic Usage Get the closest positioned ancestor of a `
` element: ```html

Hello World!

The offsetParent of the paragraph is:

``` ### Example 2: When offsetParent Returns `null` If an element is hidden using `display: none`, its `offsetParent` will return `null`. ```html
``` --- ## Technical Details | Feature | Description | | :--- | :--- | | **Return Value** | A DOM `Element` object representing the closest positioned ancestor, or `null`. | | **Read/Write** | Read-only | | **DOM Level** | CSSOM View Module | --- ## Important Considerations * **Fixed Positioning**: In most modern browsers, elements with `position: fixed` return `null` for their `offsetParent` because they are positioned relative to the viewport rather than any container. * **Hidden Elements**: If you are trying to calculate the position of an element dynamically and get unexpected `null` values, double-check if the element or its parent hierarchy has `display: none` applied. * **Performance**: Accessing `offsetParent` forces the browser to calculate the layout (reflow), which can impact performance if accessed repeatedly in rapid succession (e.g., inside scroll or resize event listeners). Cache the value if possible. --- ## Browser Support The `offsetParent` property is widely supported across all modern and legacy browsers: | Property | Chrome | Edge | Firefox | Safari | Opera | | :--- | :---: | :---: | :---: | :---: | :---: | | **offsetParent** | Yes | Yes | Yes | Yes | Yes |
← Prop Element ClientheightProp Element Offsetwidth β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.