CSS Scrollbar
In CSS, you can customize the appearance of scrollbars on a web page to match your design and enhance the user experience. The scrollbar properties are non-standard CSS properties that are implemented by some browsers. As of my last update in September 2021, the scrollbar properties were only supported in certain versions of Firefox, Edge, and Internet Explorer (with the -ms-
prefix). Chrome, Safari, and most other browsers did not support these properties.
The scrollbar properties include:
::-webkit-scrollbar
: Targets the scrollbar elements in WebKit-based browsers (e.g., Chrome, Safari).::-webkit-scrollbar-thumb
: Targets the thumb (the draggable part) of the scrollbar.::-webkit-scrollbar-track
: Targets the track (the non-draggable part) of the scrollbar.::-webkit-scrollbar-button
: Targets the scrollbar buttons, if they are visible.::-webkit-scrollbar-track-piece
: Targets the track piece between the thumb and the buttons.::-webkit-scrollbar-corner
: Targets the corner where horizontal and vertical scrollbars meet.::-ms-scrollbar
: Targets the scrollbar elements in Internet Explorer.
Please note that the appearance of scrollbars may vary based on the user’s operating system and browser version.
Here’s an example of how to customize the scrollbar in WebKit-based browsers:
/* For WebKit-based browsers */
::-webkit-scrollbar {
width: 12px;
}
::-webkit-scrollbar-thumb {
background-color: #007bff;
border-radius: 6px;
}
::-webkit-scrollbar-thumb:hover {
background-color: #0056b3;
}
::-webkit-scrollbar-track {
background-color: #f2f2f2;
}
/* To target Internet Explorer */
/* Note: The -ms- prefix is used for Internet Explorer */
::-ms-scrollbar {
width: 12px;
}
::-ms-scrollbar-thumb {
background-color: #007bff;
border-radius: 6px;
}
::-ms-scrollbar-thumb:hover {
background-color: #0056b3;
}
::-ms-scrollbar-track {
background-color: #f2f2f2;
}
In this example, we are targeting WebKit-based browsers (e.g., Chrome, Safari) and Internet Explorer to customize the appearance of the scrollbar. We change the scrollbar’s width, thumb color, track color, and add a hover effect on the thumb.
Keep in mind that the use of these scrollbar properties may change or become more widely supported in the future. Always test your design in different browsers to ensure compatibility and fallback gracefully if these properties are not supported.