Complete Independence
position: absolute completely removes an element from flow. Browser draws it on a separate layer, while other elements behave as if this block never existed.
" Base " Search Algorithm:
An absolute element searches for the nearest ancestor whose position is NOT static.
- If it finds a parent with
relative,absolute, orfixed— it sticks to their boundaries. - If it reaches the very top (HTML) and finds no one — it positions relative to viewport (browser window).
.parent { position: relative; padding: 20px; }
.child {
position: absolute;
top: 0; left: 0; /* Will stand exactly in parent's top-left corner */
}
Caution
The width: 100% property on an absolute element makes it stretch to the full width of its positioned parent, not the nearest div.