Hiding the scroll bar on an HTML page

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

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

21条回答
  •  没有蜡笔的小新
    2020-11-22 01:03

    You can accomplish this with a wrapper div that has its overflow hidden, and the inner div set to auto.

    To remove the inner div's scroll bar, you can pull it out of the outer div's viewport by applying a negative margin to the inner div. Then apply equal padding to the inner div so that the content stays in view.

    JSFiddle

    HTML

    ...

    CSS

    .hide-scroll {
        overflow: hidden;
    }
    
    .viewport {
        overflow: auto;
    
        /* Make sure the inner div is not larger than the container
         * so that we have room to scroll.
         */
        max-height: 100%;
    
        /* Pick an arbitrary margin/padding that should be bigger
         * than the max width of all the scroll bars across
         * the devices you are targeting.
         * padding = -margin
         */
        margin-right: -100px;
        padding-right: 100px;
    }
    

提交回复
热议问题