Absolute Positioning Fundamentals
You'll learn how absolute positioning removes elements from the page's normal flow and positions them relative to their nearest positioned ancestor.
The Core Idea
Unlike relative positioning (which keeps an element's space reserved in normal flow), absolute positioning completely removes an element from the document flow. It's as if the element no longer exists to its siblings—they flow around as though it was never there.
Once removed from normal flow, an absolutely positioned element is positioned relative to its nearest ancestor that has a position value other than static. If no such ancestor exists, it positions itself relative to the initial containing block (typically the viewport).
Analogy: Think of normal flow like people standing in line. Relative positioning is like someone leaning slightly to the side—they still hold their spot. Absolute positioning is like someone leaving the line entirely and floating above it, anchoring themselves to the nearest landmark (a positioned ancestor) or to the room itself if no landmark exists.
How It Works
You apply position: absolute to an element, then use the offset properties (top, right, bottom, left) to specify where it should appear relative to its positioning context.
.parent {
position: relative; /* Creates positioning context */
}
.child {
position: absolute;
top: 20px;
right: 10px;
}
In this example, .child is removed from normal flow and positioned 20px from the top and 10px from the right edge of .parent (because .parent has position: relative, making it the containing block).
Key Takeaway: Absolute positioning removes elements from normal flow entirely and positions them relative to the nearest positioned ancestor, making them "float" independently of surrounding content.