I am trying to fix a div
so it always sticks to the top of the screen, using:
position: fixed;
top: 0px;
right: 0px;
However,
Disclaimer:
This answers just the title: Fixed position but relative to container.
For the actual use case the user is asking for position: sticky
is the way to go.
https://developer.mozilla.org/en-US/docs/Web/CSS/position
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
You just need to add a transform to the container and the position of the fixed element will be relative to the container. I think a transform: translateX(0);
should be enough.