Hide scroll bar, but while still being able to scroll

前端 未结 30 3119
悲哀的现实
悲哀的现实 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:50

    This is how I do it for horizontal scroll; only CSS and works well with frameworks like Bootstrap / col-*. It only needs two extra divs and the parent with a width or max-width set:

    You can select the text to make it scroll or scroll it with fingers if you have a touchscreen.

    .overflow-x-scroll-no-scrollbar {
        overflow: hidden;
    }
    .overflow-x-scroll-no-scrollbar div {
        overflow-x: hidden;
        margin-bottom: -17px;
        overflow-y: hidden;
        width: 100%;
    }
    .overflow-x-scroll-no-scrollbar div * {
        overflow-x: auto;
        width: 100%;
        padding-bottom: 17px;
        white-space: nowrap;
        cursor: pointer
    }
    
    /* The following classes are only here to make the example looks nicer */
    .row {
        width: 100%
    }
    .col-xs-4 {
        width: 33%;
        float: left
    }
    .col-xs-3 {
        width:25%;
        float:left
    }
    .bg-gray {
        background-color: #DDDDDD
    }
    .bg-orange {
        background-color:#FF9966
    }
    .bg-blue {
        background-color: #6699FF
    }
    .bg-orange-light{
        background-color: #FFAA88
    }
    .bg-blue-light{
        background-color: #88AAFF
    }
    <html><body>
      <div class="row">
        <div class="col-xs-4 bg-orange">Column 1</div>
        <div class="col-xs-3 bg-gray">Column 2</div>
        <div class="col-xs-4 bg-blue">Column 3</div>
      </div>
      <div class="row">
        <div class="col-xs-4 bg-orange-light">Content 1</div>
        <div class="col-xs-3 overflow-x-scroll-no-scrollbar">
          <div>
            <div>This content too long for the container, so it needs to be hidden but scrollable without scrollbars</div>
          </div>
        </div>
        <div class="col-xs-4 bg-blue-light">Content 3</div>
      </div>
    </body></html>

    Short version for lazy people:

    .overflow-x-scroll-no-scrollbar {
        overflow: hidden;
    }
    .overflow-x-scroll-no-scrollbar div {
      overflow-x: hidden;
      margin-bottom: -17px;
      overflow-y: hidden;
      width: 100%;
    }
    .overflow-x-scroll-no-scrollbar div * {
      overflow-x: auto;
      width: 100%;
      padding-bottom: 17px;
      white-space: nowrap;
      cursor:pointer
    }
    
    /* The following classes are only here to make the example looks nicer */
    .parent-style {
        width: 100px;
        background-color: #FF9966
    }
    <div class="parent-style overflow-x-scroll-no-scrollbar">
      <div>
        <div>This content too long for the container, so it needs to be hidden but scrollable without scrollbars</div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 04:51

    This answer doesn't include the code, so here is the solution from page:

    According to the page this approach doesn't need to know the width of the scrollbar ahead of time in order to work and the solution works for all browsers too, and can be seen here.

    The good thing is that you are not forced to use padding or width differences to hide the scrollbar.

    This is also zoom safe. Padding/width solutions show the scrollbar when zoomed to minimum.

    Firefox fix: http://jsbin.com/mugiqoveko/1/edit?output

    .element,
    .outer-container {
      width: 200px;
      height: 200px;
    }
    .outer-container {
      border: 5px solid purple;
      position: relative;
      overflow: hidden;
    }
    .inner-container {
      position: absolute;
      left: 0;
      overflow-x: hidden;
      overflow-y: scroll;
      padding-right: 150px;
    }
    .inner-container::-webkit-scrollbar {
      display: none;
    }
    <div class="outer-container">
      <div class="inner-container">
        <div class="element">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vehicula quam nibh, eu tristique tellus dignissim quis. Integer condimentum ultrices elit ut mattis. Praesent rhoncus tortor metus, nec pellentesque enim mattis nec. Nulla vitae turpis ut
          dui consectetur pellentesque quis vel est. Curabitur rutrum, mauris ut mollis lobortis, sem est congue lectus, ut sodales nunc leo a libero. Cras quis sapien in mi fringilla tempus condimentum quis velit. Aliquam id aliquam arcu. Morbi tristique
          aliquam rutrum. Duis tincidunt, orci suscipit cursus molestie, purus nisi pharetra dui, tempor dignissim felis turpis in mi. Vivamus ullamcorper arcu sit amet mauris egestas egestas. Vestibulum turpis neque, condimentum a tincidunt quis, molestie
          vel justo. Sed molestie nunc dapibus arcu feugiat, ut sollicitudin metus sagittis. Aliquam a volutpat sem. Quisque id magna ultrices, lobortis dui eget, pretium libero. Curabitur aliquam in ante eu ultricies.
        </div>
      </div>
    </div>

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-21 04:54

    Just a test which is working fine.

    #parent{
        width: 100%;
        height: 100%;
        overflow: hidden;
    }
    
    #child{
        width: 100%;
        height: 100%;
        overflow-y: scroll;
        padding-right: 17px; /* Increase/decrease this value for cross-browser compatibility */
        box-sizing: content-box; /* So the width will be 100% + 17px */
    }
    

    Working Fiddle

    JavaScript:

    Since the scrollbar width differs in different browsers, it is better to handle it with JavaScript. If you do Element.offsetWidth - Element.clientWidth, the exact scrollbar width will show up.

    JavaScript Working Fiddle

    Or

    Using Position: absolute,

    #parent{
        width: 100%;
        height: 100%;
        overflow: hidden;
        position: relative;
    }
    
    #child{
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: -17px; /* Increase/Decrease this value for cross-browser compatibility */
        overflow-y: scroll;
    }
    

    Working Fiddle

    JavaScript Working Fiddle

    Information:

    Based on this answer, I created a simple scroll plugin.

    0 讨论(0)
  • 2020-11-21 04:54

    This works for me:

    scroll-content {
        overflow-x: hidden;
        overflow-y: scroll;
    }
    
    scroll-content::-webkit-scrollbar {
        width: 0;
    }
    
    0 讨论(0)
  • 2020-11-21 04:56

    Use:

    CSS

    #subparent {
        overflow: hidden;
        width: 500px;
        border: 1px rgba(0, 0, 0, 1.00) solid;
    }
    
    #parent {
        width: 515px;
        height: 300px;
        overflow-y: auto;
        overflow-x: hidden;
        opacity: 10%;
    }
    
    #child {
        width: 511px;
        background-color: rgba(123, 8, 10, 0.42);
    }
    

    HTML

    <body>
        <div id="subparent">
            <div id="parent">
                <div id="child">
                    <!- Code here for scroll ->
                </div>
            </div>
         </div>
    </body>
    
    0 讨论(0)
提交回复
热议问题