1. 程式人生 > 實用技巧 >CSS元素超出部分滾動,並隱藏滾動條【轉】

CSS元素超出部分滾動,並隱藏滾動條【轉】

利用 css 3 的新特性 -webkit-scrollbar, 但是這種方式不相容 火狐 和 IE

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>超出部分隱藏滾動條</title>
</head>
<style type="text/css">
    #box {
width: 500px;
height: 300px;
overflow-x: hidden;
overflow-y: scroll;
line-height: 30px;
text-align: center;
}
#box::-webkit-scrollbar {
display: none;
}
</style>
<body>
    <!-- 相容所有瀏覽器的超出部分滾動不顯示滾動條 -->
    <div id="box">
    你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
    </div>
</body>
</html>

利用內外層巢狀, 模擬, 相容所有瀏覽器, 相對於方法一比較麻煩, 使用時不能對滾動條宣告任何樣式

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>超出部分滾動條</title>
</head>
<style type="text/css">
    #box {
    /* 父容器設定寬度, 並超出部分不顯示 */
    width: 500px;
        height: 300px;
        overflow: hidden;
    }
    #box > div {
        /* 子容器比父容器的寬度多 17 px, 經測正好是滾動條的預設寬度 */
        width: 517px;
        height: 300px;
        line-height: 30px;
        text-align: center;
        overflow-y: scroll;
    }
</style>
<body>
    <!-- 相容所有瀏覽器的超出部分滾動不顯示滾動條 -->
    <div id="box">
        <div>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
你好 </br>你好 </br>
        </div>
    </div>
</body>
</html>

來源:https://www.cnblogs.com/zouwangblog/p/11140912.html