Hide scroll bar, but while still being able to scroll

前端 未结 30 3287
悲哀的现实
悲哀的现实 2020-11-21 04:22

I want to be able to scroll through the whole page, but without the scrollbar being shown.

In Google Chrome it\'s:

::-webkit-scrollbar {
    display:         


        
30条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 04:51

    My problem: I don't want any style in my HTML content. I want my body directly scrollable without any scrollbar, and only a vertical scroll, working with CSS grids for any screen size.

    The box-sizing value impact padding or margin solutions, they works with box-sizing:content-box.

    I still need the "-moz-scrollbars-none" directive, and like gdoron and Mr_Green, I had to hide the scrollbar. I tried -moz-transform and -moz-padding-start, to impact only Firefox, but there was responsive side effects that needed too much work.

    This solution works for HTML body content with "display: grid" style, and it is responsive.

    /* Hide HTML and body scroll bar in CSS grid context */
    html, body {
      position: static; /* Or relative or fixed ... */
      box-sizing: content-box; /* Important for hidding scrollbar */
      display: grid; /* For CSS grid */
    
      /* Full screen */
      width: 100vw;
      min-width: 100vw;
      max-width: 100vw;
      height: 100vh;
      min-height: 100vh;
      max-height: 100vh;
      margin: 0;
      padding: 0;
    }
    
    html {
      -ms-overflow-style: none;  /* Internet Explorer 10+ */
      overflow: -moz-scrollbars-none; /* Should hide the scroll bar */
    }
    
    /* No scroll bar for Safari and Chrome */
    html::-webkit-scrollbar,
    body::-webkit-scrollbar {
      display: none; /* Might be enough */
      background: transparent;
      visibility: hidden;
      width: 0px;
    }
    
    /* Firefox only workaround */
    @-moz-document url-prefix() {
      /* Make HTML with overflow hidden */
      html {
        overflow: hidden;
      }
    
      /* Make body max height auto */
      /* Set right scroll bar out the screen  */
      body {
        /* Enable scrolling content */
        max-height: auto;
    
        /* 100vw +15px: trick to set the scroll bar out the screen */
        width: calc(100vw + 15px);
        min-width: calc(100vw + 15px);
        max-width: calc(100vw + 15px);
    
        /* Set back the content inside the screen */
        padding-right: 15px;
      }
    }
    
    body {
      /* Allow vertical scroll */
      overflow-y: scroll;
    }
    

提交回复
热议问题