I am reading the "Positioning Techniques" of this article on MDN. It talks about "Absolute positioning elements can be fixed to a position relative to its nearest positioned ancestor element".
My understanding of "positioned ancestor" is an ancestor with any position property other than static. But I need expert opinions to verify my thought.
When referring to positioned ancestor we mean the closest ancestor to the element with a set position value, other than static (which is default).
An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
An element with position: absolute; is positioned relative to the nearest positioned ancestor, instead of position relative to the viewport like fixed. However, if an absolute positioned element has no positioned ancestors, it uses the document body and moves along with page scrolling.
Absolute positioning The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static ).
When referring to positioned ancestor we mean the closest ancestor to the element with a set position
value, other than static
(which is default).
So there are two assumptions here:
position:static
(presumably set by JavaScript, in order to change the "positioned ancestor") we don't mean that ancestor.Why is this important? Because the "positioned ancestor" is the reference from which properties like left
and top
are calculated, when the descendant is given position:absolute
. It's also called "the reference element/parent" of the descendant.
Also, if this closest ancestor has a set z-index
(other than auto
), it creates a stacking context for its descendants.
From the W3C:
In this case the containing block is the nearest positioned ancestor. By “positioned” I mean an element whose position property is set to relative, absolute or fixed—in other words, anything except normal static elements.
Long description for example illustrating positioning with respect to a positioned ancestor
a containing block established by a relatively positioned ancestor ("outer").
And CSS Module 3, search for "positioned ancestor" on that page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With