Hiding the scroll bar on an HTML page

前端 未结 21 1384
旧巷少年郎
旧巷少年郎 2020-11-22 00:07

Can CSS be used to hide the scroll bar? How would you do this?

21条回答
  •  余生分开走
    2020-11-22 01:07

    WebKit supports scrollbar pseudo elements that can be hidden with standard CSS rules:

    #element::-webkit-scrollbar {
        display: none;
    }
    

    If you want all scrollbars hidden, use

    ::-webkit-scrollbar {
        display: none;
    }
    

    I'm not sure about restoring - this did work, but there might be a right way to do it:

    ::-webkit-scrollbar {
        display: block;
    }
    

    You can of course always use width: 0, which can then be easily restored with width: auto, but I'm not a fan of abusing width for visibility tweaks.

    Firefox 64 now supports the experimental scrollbar-width property by default (63 requires a configuration flag to be set). To hide the scrollbar in Firefox 64:

    #element {
        scrollbar-width: none;
    }
    

    To see if your current browser supports either the pseudo element or scrollbar-width, try this snippet:

    .content {
      /* These rules create an artificially confined space, so we get
         a scrollbar that we can hide. They are not directly involved in
         hiding the scrollbar. */
    
      border: 1px dashed gray;
      padding: .5em;
    
      white-space: pre-wrap;
      height: 5em;
      overflow-y: scroll;
    }
    
    .content {
      /* This is the magic bit for Firefox */
      scrollbar-width: none;
    }
    
    .content::-webkit-scrollbar {
      /* This is the magic bit for WebKit */
      display: none;
    }
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eu urna et leo aliquet malesuada ut ac dolor. Fusce non arcu vel ligula fermentum sodales a quis sapien. Sed imperdiet justo sit amet venenatis egestas. Integer vitae tempor enim. In dapibus nisl sit amet purus congue tincidunt. Morbi tincidunt ut eros in rutrum. Sed quam erat, faucibus vel tempor et, elementum at tortor. Praesent ac libero at arcu eleifend mollis ut eget sapien. Duis placerat suscipit eros, eu tempor tellus facilisis a. Vivamus vulputate enim felis, a euismod diam elementum non. Duis efficitur ac elit non placerat. Integer porta viverra nunc, sed semper ipsum. Nam laoreet libero lacus. Sed sit amet tincidunt felis. Sed imperdiet, nunc ut porta elementum, eros mi egestas nibh, facilisis rutrum sapien dolor quis justo. Quisque nec magna erat. Phasellus vehicula porttitor nulla et dictum. Sed tincidunt scelerisque finibus. Maecenas consequat massa aliquam pretium volutpat. Duis elementum magna vel velit elementum, ut scelerisque odio faucibus.


    (Note that this is not really a correct answer to the question, because it hides the horizontal bars as well, but that's what I was looking for when Google pointed me here, so I figured I'd post it anyway.)

提交回复
热议问题