fixed position
For a long time I believed that an element with position: fixed would always be positioned relative to the viewport. However, this is not always the case, as outlined in the MDN documentation:
It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has a transform, perspective, or filter property set to something other than none
Simply put, if any ancestor has certain CSS properties set, that ancestor becomes the containing block instead of the viewport.
Properties that create a new containing block
The following properties, when set to values other than their defaults, will create a new containing block for fixed-positioned descendants:
transform- Any value exceptnone(e.g.,transform: translateX(0))perspective- Any value exceptnonefilter- Any value exceptnone(e.g.,filter: blur(0))will-change- When set totransform,perspective, orfiltercontain- Values oflayout,paint, or a combination including eitherbackdrop-filter- Any value exceptnone
Try it yourself: Scroll the page and observe the fixed box. With the checkbox enabled, it's positioned relative to the wrapper box (with transform: translateX(0)) rather than the viewport. Toggle the checkbox to see the difference.
Why this happens: the CSS specification
Specific CSS properties establish a containing block for fixed-position descendants. For example, a non-none transform creates a local coordinate system, so a fixed child is positioned against that ancestor instead of the viewport.
A containing block and a stacking context are different concepts. Some properties create both, but creating a stacking context alone is not enough: opacity: 0.5 changes stacking without making the ancestor the containing block for fixed descendants. See how containing blocks are established.
Practical use cases
1. Positioning third-party components within a container
When using a third-party plugin/component with position: fixed but you want it positioned relative to your container rather than the viewport. If you can't modify the source code, wrap it with a container that has transform: translateX(0):
.wrapper {
transform: translateX(0); /* Creates new containing block */
}
2. Modal dialogs within containers
Create modals that are fixed within a specific section of your page rather than the entire viewport:
.modal-container {
transform: translate3d(0, 0, 0);
overflow: auto;
}
.modal {
position: fixed;
/* Now fixed relative to .modal-container */
}
Common pitfalls and debugging
Unexpected positioning
If your fixed element isn't staying at the viewport edge, check all ancestors for:
/* Any of these will break viewport-relative fixed positioning */
.ancestor {
transform: scale(1);
filter: brightness(1);
will-change: transform;
contain: paint;
}
Debugging tip: Use browser DevTools to inspect computed styles and search for these properties up the DOM tree.
Best practices
- Be intentional: When adding transforms or filters for a visual effect, check whether their containing-block behavior also changes fixed descendants
- Document it: Comment your CSS when intentionally creating a new containing block
- Test thoroughly: Check fixed positioning behavior at different scroll positions and viewport sizes
Alternatives
If you need viewport-relative positioning despite transformed ancestors:
- Use
position: stickyif appropriate for your use case - Restructure your DOM to avoid transformed ancestors
- Use portal-based solutions (React Portals, Vue Teleport) to render outside the transformed tree