What is an element with position: absolute positioned relative to?
- A.Its own original position in the document flow
- B.Always the direct parent element
- C.Always the viewport, whatever the ancestors
- D.The nearest ancestor with a position other than
static
Answer: D
position: absolute takes the element out of the document flow and positions it relative to the nearest ancestor whose position is not static (relative/absolute/fixed/sticky). If no ancestor is positioned, the reference is the initial containing block — visually the same as positioning against the page. That's why you usually set position: relative on the parent to serve as the reference.
References