If the relative
value acts like Superman, then the absolute
value mirrors Inception—a place where you design your own world. Unlike the static
and relative
values, an absolutely positioned element is removed from the normal flow. This means you can put it anywhere, and it won’t affect or be affected by any other element in the flow. Think of it as an element with a giant strip of velcro on its back. Just tell it where to stick and it sticks. Exactly like the relative
value, absolutely positioned elements respond to offset properties for positioning. You can set an element to top: 100px
and left: 200px;
and that element will sit exactly 100px from the top and 200px from the left of the document. Let’s look at an example using four elements:
Example E shows four boxes, each in a corner of the browser window. Since we set each box’s position
value to absolute
, we’ve essentially velcroed a box to each corner of our browser window. As you resize the browser, those boxes will stay in their respective corners. If you shrink the browser window so that the boxes overlap, you’ll notice that there is no interaction at all—that’s because they’re out of the document’s normal flow.
Just like relative
elements, absolute
elements create a new coordinate system for child elements. Example F extends Example E, with an orange element set inside each box. Notice the offset coordinates are in respect to each parent element.
Just like relative
elements, absolute
elements create a new coordinate system for child elements.
One nuance with absolute positioning is that it will be locked relative to its closest positioned ancestor. If you forget to add a position rule to the parent item, (this is typically done using position: relative;
), the browser will keep looking up the chain and ultimately default to the body tag.