I am trying to do positioning in JavaScript. I am using a cumulative position function based on the classic quirksmode function that sums offsetTop
and o
offsetParent
will return null if your element object hasn't been appended to the DOM yet.
This is an old question, but I have another case. If you manipulate the DOM, you may end up with a null offsetParent - in IE6 and IE7.
See: IE 6/7 - "Unspecified Error" when accessing offsetParent (Javascript)
If the document hasn't finished loading then offsetParent can be null
I have made a test of 2,304 divs with unique combinations of values for position
, display
, and visibility
, nested inside unique combinations of each of those values, and determined that:
an otherwise-valid element
that is a descendant of <body>
will not have an offsetParent
value if:
position:fixed
(Webkit and IE9)display:none
(Webkit and FF)display:none
(Webkit and FF)It is also reasonable to expect that an element that has no parent, or that is not added to the page itself (is not a descendant of the <body>
of the page), will also have offsetParent==null
.
I also experienced an offsetParent
of null
when the parent was inside a web component (i.e. the element I tried getting the offsetParent
of was projected into the web component).
https://developer.mozilla.org/en/DOM/element.offsetParent
offsetParent returns null when the element has style.display set to "none".