How it works
Every box's position resolves against its containing block, so layout is really a walk up a chain of them.
For in-flow boxes (static, relative, sticky), the formatting context defines it: in block layout, the content box of the nearest block container. That's why a static child's width: 50% means half the parent's content width.
For absolute, the browser walks up the ancestors and stops at the first one that establishes an absolute positioning containing block. Any position other than static does; so do transform, translate/rotate/scale, perspective, filter, backdrop-filter, contain: layout or paint, content-visibility: auto, and will-change naming one of those properties. Both test browsers agreed on every one of these. If none is found, the containing block is the initial containing block, which CSS 2 defines as having the viewport's dimensions and being anchored at the canvas origin.
For fixed, the same walk skips position and stops only at the non-position properties; failing that, the containing block is the layout viewport. Question 040 covers why that matters.
Then the absolute layout model runs:
- The insets carve the inset-modified containing block out of the containing block. One
auto inset becomes 0; two auto insets in an axis put the box at its static position.
width/height resolve: auto stretches when neither inset nor margin in that axis is auto, and otherwise shrinks to fit content. Percentages still use the original containing block.
auto margins split whatever space is left. They can go negative, unlike in flow.
- The margin box is aligned inside that rectangle (
align-self/justify-self apply here).
When the containing block is a grid container, grid-row/grid-column on the absolute child can narrow the containing block to a grid area.
Edge cases
overflow clips an absolute box only if the clipping element is its containing block or an ancestor of it. A position: absolute menu inside a non-positioned overflow: hidden wrapper, with its containing block outside the wrapper, still received elementFromPoint hits 60px below the wrapper's bottom edge. With position: relative on the wrapper, the part of the menu past that edge stopped receiving hits.
If the nearest positioned ancestor is an inline box, the containing block is built from its first and last fragments' content edges, not a padding box. The spec still marks the multi-line case as an open issue.
transform: none and filter: none don't establish anything; only real values do. opacity, isolation, mix-blend-mode, clip-path, overflow and z-index create other effects but are not containing blocks. In Chrome 155 and Firefox 158, container-type: inline-size and size weren't either.
Real-world scenario
A card has position: relative; overflow: hidden; border-radius: 12px so images respect the rounded corners, and a dropdown inside it is position: absolute; top: 100%. The menu gets cut off at the card's edge, because the card is both its containing block and its clipper. Moving position: relative to a wrapper outside the card keeps the menu positioned yet unclipped. For a menu that must escape every ancestor, the popover top layer plus anchor positioning (questions 050 and 060) removes the dependency on the tree entirely.
Trade-offs
position: relative; top: 4px and transform: translateY(4px) both nudge without disturbing siblings. Relative positioning's only side effect is becoming a containing block for absolute descendants; transform animates cheaply but also creates a stacking context and captures fixed descendants.
Absolute positioning removes the box from layout, so the parent can't grow to fit it. Overlapping in-flow boxes (a caption on an image) is often better done by placing both in the same grid area, which overlaps them while still sizing the parent.