Hide scroll bar, but while still being able to scroll

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

    UPDATE:

    Firefox now supports hiding scrollbars with CSS, so all major browsers are now covered (Chrome, Firefox, Internet Explorer, Safari, etc.).

    Simply apply the following CSS to the element you want to remove scrollbars from:

    .container {
        overflow-y: scroll;
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none;  /* Internet Explorer 10+ */
    }
    .container::-webkit-scrollbar { /* WebKit */
        width: 0;
        height: 0;
    }
    

    This is the least hacky cross browser solution that I'm currently aware of. Check out the demo.


    ORIGINAL ANSWER:

    Here's another way that hasn't been mentioned yet. It's really simple and only involves two divs and CSS. No JavaScript or proprietary CSS is needed, and it works in all browsers. It doesn't require explicitly setting the width of the container either, thus making it fluid.

    This method uses a negative margin to move the scrollbar out of the parent and then the same amount of padding to push the content back to its original position. The technique works for vertical, horizontal and two way scrolling.

    Demos:

    • Vertical
    • Horizontal
    • Both directions

    Example code for the vertical version:

    HTML:

    Your content.

    CSS:

    .parent {
      width: 400px;
      height: 200px;
      border: 1px solid #AAA;
      overflow: hidden;
    }
    
    .child {
      height: 100%;
      margin-right: -50px; /* Maximum width of scrollbar */
      padding-right: 50px; /* Maximum width of scrollbar */
      overflow-y: scroll;
    }
    

提交回复
热议问题