•5 min read

position: fixed Not Working? An Ancestor Took the Containing Block

The bug report had a screenshot and five words: the drawer is in a box.

It was. position: fixed; inset: 0 inside a card component. On my machine the drawer covered the screen. In production it covered a 320px rectangle in the middle of the page, and scrolled up with the content instead of staying put. Nobody had touched the drawer. A week earlier someone put a hover animation on the card wrapper.

Third time I have run into this one. Numbers instead of adjectives from here.

The viewport is only the default containing block

Offsets on a fixed element resolve against a containing block, and normally that block is the viewport. That is why top: 0 means the top of the screen. Put the right property on an ancestor and the ancestor becomes the containing block instead. Your inset: 0 now means the ancestor's edges.

The answers I found when this first bit me all list transform, filter, perspective and will-change. Correct, and incomplete in ways that cost me an afternoon.

Wooden nesting dolls lined up with each smaller doll sitting inside the one before it

What actually triggers it

I dropped a 10px fixed probe with top: 0; left: 0 inside a 300px div, one property at a time, and read getBoundingClientRect(). A probe at 0,0 means the viewport still owns it. Chrome 157, headless, macOS:

nothing                        viewport
transform: none                viewport
transform: scale(1)            TRAPPED
transform: translateX(0)       TRAPPED
transform: rotate(0deg)        TRAPPED
translate: 0 0                 TRAPPED
scale: 1                       TRAPPED
rotate: 0deg                   TRAPPED
filter: none                   viewport
filter: blur(0px)              TRAPPED
filter: opacity(1)             TRAPPED
backdrop-filter: blur(2px)     TRAPPED
perspective: 1000px            TRAPPED
will-change: transform         TRAPPED
will-change: opacity           viewport
contain: layout                TRAPPED
contain: paint                 TRAPPED
contain: content               TRAPPED
container-type: inline-size    viewport
content-visibility: auto       TRAPPED
offset-path                    TRAPPED

Identity values count. transform: scale(1) changes nothing on screen and still takes the containing block, and so do translateX(0) and rotate(0deg). The check is whether the property is none, not whether the matrix does anything. That is how a translateZ(0) added for GPU compositing breaks a modal two components away.

The longhands hide from the Styles panel. translate: 0 0, scale: 1 and rotate: 0deg trap the child the same way, and computed transform still reads none. Nothing shows up in DevTools, and nothing stops you from typing those longhands by hand.

Containment does it too. contain: layout, contain: paint and contain: content all trap, and so does content-visibility: auto, which is exactly what you add to keep a long list scrolling smoothly. container-type: inline-size did not trap in Chrome 157, which surprised me after writing about container queries. I did not test Safari, so treat that line as Chrome-only.

Four symptoms and a one-line tell

  • An inset: 0 overlay covers the card instead of the screen and starts a few pixels in. My ancestor had a 352x152 border box with a 6px border; the overlay measured 340x140 sitting 6px inside. Fixed offsets skip the border.
  • bottom: 0 sticks to the card's bottom edge. Against the ancestor's bottom, not the window's.
  • The child scales with the parent. A 20px probe measured 10x10 under transform: scale(0.5) on the ancestor. That is the "why is my close button tiny and drifting" bug.
  • Scrolling drags it along. Probe top went from 890 to 690 after a 200px scroll, while the untrapped probe sat at 0 the whole time.

The cheap tell is el.offsetParent. On a fixed element it is normally null. Once an ancestor owns the containing block it comes back as an element. Not always the guilty one, but enough to know the viewport is out of the picture.

Finding the ancestor instead of guessing

Toggling styles in DevTools until something moves works, and I have done it enough times to want a shortcut.

function findFixedTrap(el) {
  const contain = ['layout', 'paint', 'content', 'strict'];
  for (let n = el.parentElement; n && n !== document.documentElement; n = n.parentElement) {
    const cs = getComputedStyle(n);
    const hits = [];
    if (cs.transform !== 'none') hits.push('transform');
    if (cs.filter !== 'none') hits.push('filter');
    if ((cs.backdropFilter || 'none') !== 'none') hits.push('backdrop-filter');
    if (cs.perspective !== 'none') hits.push('perspective');
    if (cs.translate !== 'none') hits.push('translate');
    if (cs.scale !== 'none') hits.push('scale');
    if (cs.rotate !== 'none') hits.push('rotate');
    if (cs.contain.split(' ').some((v) => contain.includes(v))) hits.push('contain');
    if (/transform|perspective|filter/.test(cs.willChange)) hits.push('will-change');
    if (cs.contentVisibility === 'auto') hits.push('content-visibility');
    if (hits.length) return { node: n, hits };
  }
  return null;
}

One call on the broken overlay names the wrapper. I keep that in Snippet Ark with the rest of my debugging one-offs, because retyping it from memory at 11pm is how typos happen. What the function cannot tell you is which ancestor you are allowed to change. That is a product decision.

Fixes that hold

The popular advice is to remove the transform. That is often true, and often the transform is the animation, which leaves you with no advice at all.

Move the element out of the subtree. A React portal or a Vue Teleport to body puts the drawer where no ancestor can reach it. Portaling into another wrapper that has its own transform puts you back in the same bug with less hair.

Use the top layer. <dialog> with showModal() renders in the top layer, which puts it beyond the reach of ordinary ancestors. I put a probe inside a fixed child of a dialog nested in a translateX(30px) wrapper and measured 0,0. The transform never reached it. You also get focus trapping and an inert background, which is more than most hand-rolled overlays manage.

Keep the animation and drop the trap. Do not leave will-change on the element after an entrance animation finishes. will-change: opacity leaves the viewport alone, will-change: transform does not. And if the animation is a fade, ask whether a transform needs to be involved at all.

The last measurement surprised me. I paused a translateX keyframe at halfway, computed transform matrix(1, 0, 0, 1, 100, 0), child trapped. Then I paused another one at 0%: matrix(1, 0, 0, 1, 0, 0), child still trapped. An identity matrix from an attached animation traps like any other. So a wrapper that merely has an animation attached stays broken while that animation sits at its first frame doing nothing visible.

If you have fought sticky elements that refuse to stick, it is the same class of problem. Something upstream changed the box you are being positioned against. My notes on z-index that will not stack came out of the same week.