MacOS hides scrollbars for trackpad users, which results in my users not knowing they can scroll a result set horizontally. I’m trying to use CSS to target the horizontal scrollbars only, so that I can make them permanently visible.
I’m able to override both scrollbar visual behavior with CSS:
::-webkit-scrollbar{
-webkit-appearance: none;
width: 7px;
}
::-webkit-scrollbar-thumb{
border-radius: 4px;
background-color: rgba(0,0,0,.5);
box-shadow: 0 0 1px rgba(255,255,255,.5);
}
https://jsfiddle.net/ypk62h8v/1/
But when I try to apply the :horizontal pseudo-element, it doesn’t work (Mac/Chrome):
HTML:
<div class="frame" style="">
<div style="width:500px;height:500px;">
SCROLLABLE
</div>
</div>
CSS:
.frame {
overflow-y: auto;
overflow-x: auto;
border: 1px solid black;
height: 3em;
width: 10em;
line-height: 1em;
}
::-webkit-scrollbar:horizontal {
-webkit-appearance: none;
width: 7px;
}
::-webkit-scrollbar-thumb:horizontal {
border-radius: 4px;
background-color: rgba(0,0,0,.5);
box-shadow: 0 0 1px rgba(255,255,255,.5);
}
2
Answers
There’s no direct CSS selector like :horizontal that you can use to specifically target horizontal scrollbars for customization in the same way you can target scrollbars without specifying orientation.
So, if you want to achieve this you should use following CSS and JavaScript Code:
I understand that you’re trying to target the horizontal scrollbar specifically using the :horizontal pseudo-element. Unfortunately, as of my knowledge the :horizontal and :vertical pseudo-elements are not supported in webkit-based browsers like Chrome and Safari.
However, you can still achieve your goal of making the horizontal scrollbar permanently visible by targeting the entire scrollbar area and thumb without using the :horizontal pseudo-element. Here’s how you can modify your CSS to achieve that:
This will target both horizontal and vertical scrollbars, but it will still make the horizontal scrollbar visible. Unfortunately, there’s no direct pseudo-element targeting for horizontal scrollbars alone in webkit-based browsers.
To make the horizontal scrollbar permanently visible for a specific element using JavaScript, you can follow these steps: