Fixed position but relative to container

后端 未结 25 1598
独厮守ぢ
独厮守ぢ 2020-11-21 23:52

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,

25条回答
  •  -上瘾入骨i
    2020-11-22 00:33

    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.

提交回复
热议问题