scrollbar on browser and div margin 0 auto jumping

后端 未结 4 1530
逝去的感伤
逝去的感伤 2020-12-30 04:07

i am using div#wrapper margin: 0 auto to center the div, there is scroll bar on this page however when it transition to second page where there is no scroll bar

相关标签:
4条回答
  • 2020-12-30 04:38

    Set html width to be equal viewport and turn off horizontal scroll to avoid appearing of horizontal scrollbar when vertical one will expand html.

    html {
      width: 100vw;
      overflow-x: hidden;
    }
    
    0 讨论(0)
  • 2020-12-30 04:44

    Adding a calculated margin to your html tag will create a margin on the left equal to the scrollbar on the right if a scrollbar exists. This will stop the jumping.

    html { 
        margin-left: calc(100vw - 100%); 
    }
    

    Original Source

    0 讨论(0)
  • 2020-12-30 04:47

    I've run into this a few times, the best thing I've found is to force a Y scroll-bar on every page, even if it isn't needed using in your style sheet:

    html { overflow-y: scroll; }
    

    This will mean there is always a right scroll bar on the page, but it will be enabled/disabled as needed, and prevent the jump.

    0 讨论(0)
  • 2020-12-30 04:54

    If you don't want to force a Y scroll-bar on every page, you can calculate the margins using view-port width. This stopped the auto jumping for me.

    @media (min-width: 960px) {
        .container {
            margin-left: calc(50vw - 480px);
            width: 960px;
        }
    }
    
    0 讨论(0)
提交回复
热议问题