Why .offsetParent returns nearest table for non-positioned element


From the MDN documentation for Element.offsetParent:

The HTMLElement.offsetParent read-only property returns a reference to the object which is the closest (nearest in the containment hierarchy) positioned containing element. If the element is non-positioned, the nearest table, table cell or root element is returned by offsetParent

Why does .offsetParent return the nearest table if the element is not positioned (that is, position: static)? Why doesn’t .offsetParent always return the nearest positioned element?

I know that the answer to my question is "because the standard says so," but why did the developers of the standard decide that? What is the purpose of that behavior?


The docs were at best misleading, or mislead.

The new version is clearer:

A positioned ancestor is either:

  • an element with a non-static position, or
  • td, th, table in case the element itself is static positioned.

So, .offsetParent does "return the nearest positioned element", if there is one before the nearest td, th, or table:

console.log("in positioned", target1.offsetParent);
console.log("in static", target2.offsetParent);
.positioned { position: relative }
        <div class=positioned>
          <span id=target1>content</span>
        <div class=static>
          <span id=target2>content</span>

As for why they decided these elements should be treated as positioned elements even though their computed position is "static", it’s certainly because their position is actually dictated by rules that doesn’t match static‘s rules.

Answered By – Kaiido

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published