Positioning Context
An absolutely positioned element is placed relative to its nearest positioned ancestor (non-static). Understanding this chain is key to predictable absolute positioning.
An absolutely positioned element is placed relative to its nearest positioned ancestor (non-static). Understanding this chain is key to predictable absolute positioning.
.grandparent { position: static; } /* NOT a context */
.parent { position: relative; } /* IS a context */
.child { position: absolute; top: 0; left: 0; }
/* .child positions relative to .parent, not .grandparent */
/* Check: if no positioned ancestor, absolute positions to viewport */
.no-context {
position: absolute;
top: 0; left: 0; /* goes to viewport top-left */
}
When absolute positioning behaves unexpectedly, inspect ancestor elements in DevTools to find which one is the positioning context.