Relative Positioning as Containing Block
You'll learn: How relatively positioned elements create a reference point for absolutely positioned children.
The Core Idea
When you apply position: relative to an element, it does more than just allow you to shift it with offset properties. It also establishes a containing block for any descendant elements that have position: absolute.
Think of it like this: imagine you're arranging stickers on a poster board. By default, stickers measure their position from the poster's edges. But if you place a smaller card on the poster and mark it as "relative," any stickers you place on that card will now measure their position from the card's edges instead of the poster's. The relatively positioned card becomes the new reference frame.
How It Works
.parent {
position: relative;
/* This element is now a containing block */
}
.child {
position: absolute;
top: 20px;
left: 30px;
/* These offsets are now relative to .parent, not the viewport */
}
Without position: relative on the parent, the .child would position itself relative to the nearest positioned ancestor—or all the way up to the browser viewport if none exists. By adding position: relative to .parent (even without any offset values), you're telling the browser: "this is the boundary for my absolutely positioned children."
Key Takeaway: position: relative transforms an element into a positioning context, making it the reference point for absolutely positioned descendants—even if you don't move the relative element itself.