Content jumps horizontally whenever browser adds a scrollbar

后端 未结 6 1695
星月不相逢
星月不相逢 2021-01-04 03:28

I\'m using a fixed width body and auto margins to center my content in the middle of the page. When the content exceeds the page\'s height and the browser adds a scrollbar,

6条回答
  •  星月不相逢
    2021-01-04 04:12

    I'll just leave this link here because it seems an elegant solution to me:

    https://aykevl.nl/2014/09/fix-jumping-scrollbar

    What he does is add this css:

    @media screen and (min-width: 960px) {
        html {
            margin-left: calc(100vw - 100%);
            margin-right: 0;
        }
    }
    

    This will move the content to the left just the size of the scrollbar, so when it appears the content is already moved. This works for centered content with overflow: auto; applied to the html tag. The media query disables this for mobile phones, as its very obvious the difference in margin widths.

    You can see an example here:

    http://codepen.io/anon/pen/NPgbKP

提交回复
热议问题