Skip to content
fa
Search Frontend Atlas
Search questions and concepts across the handbook.
CSS & Renderingintermediate10 min read

How does CSS determine the containing block for positioned elements?

Understand normal flow, absolute positioning, fixed positioning, and containing-block-establishing properties.

An absolutely positioned element does not automatically measure its offsets from the browser window. It uses a containing block, and the element that establishes that block depends on its positioning and layout context. Once you know which ancestor is in charge, many mysterious CSS offsets stop being mysterious.

An absolutely positioned element does not automatically measure its offsets from the browser window. It uses a containing block, and the element that establishes that block depends on its positioning and layout context. Once you know which ancestor is in charge, many mysterious CSS offsets stop being mysterious.

The quick version

The containing block depends on the positioning scheme and ancestor properties. Absolute elements are commonly positioned relative to the padding box of the nearest qualifying positioned ancestor; fixed elements are usually viewport-relative unless an ancestor establishes a fixed-position containing block.

Build the mental model

Transforms, filters, perspective, and some containment properties can establish containing blocks for positioned descendants. This explains why a fixed tooltip may unexpectedly move with a transformed parent.

See it in code

.dialog-shell {
  position: relative;
}
.dialog-shell .close {
  position: absolute;
  inset: 1rem 1rem auto auto;
}

Where it gets tricky

A transformed ancestor can become the containing block for a fixed descendant, which is a frequent source of broken viewport overlays. Absolute positioning also depends on the nearest ancestor that establishes the relevant containing block, not necessarily the visually nearest wrapper. Diagnose the containing block and clipping ancestor separately; they are different questions.

A common misunderstanding

position: fixed is not unconditionally anchored to the viewport in every containing-block context.

What this changes in an application

Portals can escape clipping and containing-block constraints, but introduce focus, stacking, and ownership concerns.

Questions worth exploring next

  • Why does overflow: hidden clip an overlay?
  • What can establish a fixed-position containing block?

References and next steps