Sub topic 6
Absolute positioning: removing an element from the flow
This chapter assumes you know basic HTML and CSS selectors, the box model, and length units such as px, %, and rem. It also assumes you have read the earlier chapters on normal flow, offsets, relative positioning, and containing blocks.
What position: absolute does
Setting position: absolute takes an element out of normal flow. Its siblings behave as if it were not there, so they close up the gap it leaves. The element is then placed using its offset properties (top, right, bottom, left, or the inset shorthand) measured from its containing block.
Two other effects follow:
- The element no longer takes up space in its parent. A parent that contains only absolutely positioned children can collapse to zero height.
- The element shrinks to fit its content. A block-level
divthat is absolutely positioned is no longer as wide as its parent unless you set a width.
Finding the containing block
An absolutely positioned element is placed against its nearest ancestor that is positioned. In this context, "positioned" means the ancestor's position is anything other than static. The browser walks up the tree and uses the padding box of the first such ancestor. If it finds none, it uses the initial containing block, which is roughly the size of the viewport at the top of the page.
The usual way to control this is to give the intended parent position: relative with no offsets. The parent does not move, but it becomes the reference box.
A worked example: a corner badge
<div class="card">
<span class="badge">New</span>
<p>Card content</p>
</div>
<style>
.card {
position: relative;
padding: 1rem;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 0;
right: 0;
}
</style>The card has position: relative, so it is the nearest positioned ancestor. top: 0 and right: 0 place the badge's top and right edges against the card's padding box, which puts it in the top-right corner. The paragraph is laid out as if the badge did not exist.
Remove position: relative from .card and run it again. The badge jumps to the top-right corner of the page, because no ancestor is positioned.
Offsets and sizing
Offsets on an absolutely positioned element measure the distance between the element's margin edge and the containing block's padding edge. A few details are useful:
- Setting both
leftandrightwithwidth: autostretches the element between them. The same applies totopandbottomwithheight: auto. inset: 0is shorthand for all four offsets at zero. Withwidthandheightleft asauto, the element fills the containing block.- Percentages in offsets refer to the containing block's size.
left: 50%is half the containing block's width. - If you set only some offsets, the element keeps the position it would have had in flow on the other axis. This is called the static position. Omitting
topleaves the element at its natural vertical place, which is a common source of confusion.
Overlap and paint order
Because absolute elements do not take up space, they can sit on top of other content. Positioned elements paint above non-positioned ones. Among positioned elements, later ones in the HTML paint on top unless z-index says otherwise. The next chapter covers z-index in detail.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Element appears at the page corner | No positioned ancestor | Add `position: relative` to the intended parent |
| Parent collapses to zero height | All children are absolute | Give the parent an explicit height or `min-height` |
| Element is only as wide as its text | Absolute elements shrink to fit | Set `width`, or use `left` and `right` together |
| Element sits at an unexpected vertical place | `top` and `bottom` were both omitted | Set an explicit offset |
Exercise
A .tooltip has position: absolute; top: 100%; left: 0;. Its parent .button has no position set, and the button sits in the middle of a long page. Where does the tooltip appear, and what one-line change makes it appear directly below the button?
Show answer
Because .button is not positioned, the browser looks further up for a positioned ancestor and, finding none, uses the initial containing block. top: 100% then means 100% of that block's height, so the tooltip lands near the bottom of the first screen, far from the button. Adding position: relative to .button makes the button the containing block, so the tooltip appears just below it.
Exercise
What do you get from position: absolute; inset: 0; on an element with width: auto and height: auto, inside a position: relative parent?
Show answer
The element stretches to fill the parent's padding box on all four sides, which is useful for overlays that cover a card or image.