有些設計稿會要求自訂捲軸樣式。雖然目前仍很難做到跨瀏覽器完全一致,但在特定環境中,還是可以透過 CSS 調整捲軸外觀。
IE 很早便提供捲軸相關屬性,例如:
scrollbar-arrow-color: color; scrollbar-face-color: color; scrollbar-3dlight-color: color; scrollbar-highlight-color: color; scrollbar-shadow-color: color; scrollbar-darkshadow-color: color; scrollbar-track-color: color; scrollbar-base-color: color;
這些屬性可以調整箭頭、軌道、亮邊與陰影等部分,但其他瀏覽器支援度有限。
在 WebKit 核心瀏覽器中,可以使用偽元素來自訂捲軸:
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
}
::-webkit-scrollbar-thumb {
background: #888;
border-radius: 5px;
}
::-webkit-scrollbar-thumb:hover {
background: #555;
}常見的偽元素包含:
此外,也可以搭配 :horizontal、:vertical、:decrement、:increment 等狀態,進一步控制不同部位。
實務建議:自訂捲軸可以提升視覺一致性,但仍應保留清楚的可操作狀態,例如 hover、active、disabled,避免影響可用性。