Custom CSS Scrollbars: Styling scrollbar-color, scrollbar-width & WebKit
To style scrollbars across all modern browsers, combine modern W3C standards with legacy WebKit pseudo-elements. Declare scrollbar-width: thin and scrollbar-color: var(--thumb) var(--track) for Firefox and Chromium 121+, and provide ::-webkit-scrollbar, ::-webkit-scrollbar-thumb, and ::-webkit-scrollbar-track rules for custom border radii, drop shadows, and hover transitions in Safari and older engines.
Step 1: Modern W3C Standards: scrollbar-color & scrollbar-width
The official CSS Scrollbars Module Level 1 defines two clean properties: scrollbar-color (accepting thumb and track color values) and scrollbar-width (auto, thin, or none). Supported in Firefox, Chrome 121+, and Edge.
:root {
--scrollbar-thumb: #6366f1;
--scrollbar-track: #0f172a;
}
/* Modern standard declaration */
* {
scrollbar-width: thin;
scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}
Step 2: Legacy WebKit Engine Styling for Safari & Chrome
For rounded corners, custom padding, and hover states, layer WebKit pseudo-elements alongside the standard declarations. Modern browsers gracefully use WebKit rules when detailed customization is provided.
/* Width of vertical scrollbar */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
/* Background track */
::-webkit-scrollbar-track {
background: #0f172a;
border-radius: 4px;
}
/* Draggable thumb pill */
::-webkit-scrollbar-thumb {
background: #6366f1;
border-radius: 4px;
border: 2px solid #0f172a; /* Creates an inset gap */
}
::-webkit-scrollbar-thumb:hover {
background: #818cf8;
}
Step 3: Dark Mode Adaptations via CSS Custom Properties
Never hardcode light or dark scrollbar colors. Bind thumb and track colors to semantic theme variables so scrollbars seamlessly swap colors when the user toggles dark mode.
:root[data-theme="light"] {
--sb-thumb: #cbd5e1;
--sb-track: #f8fafc;
}
:root[data-theme="dark"] {
--sb-thumb: #475569;
--sb-track: #0f172a;
}
.custom-scroller {
scrollbar-color: var(--sb-thumb) var(--sb-track);
}
Step 4: Overlay Scrollbars on macOS vs Classic Windows Tracks
macOS defaults to floating overlay scrollbars that disappear when idle, whereas Windows reserves physical layout space for scrollbars. Use scrollbar-gutter: stable to prevent annoying layout shifts when content height fluctuates.
.content-pane {
overflow-y: auto;
/* Reserves space for the scrollbar even when content doesn't overflow */
scrollbar-gutter: stable;
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Which browsers support the modern scrollbar-color and scrollbar-width properties?
Firefox has supported them since version 64. Chrome and Edge added full support in version 121 (early 2024), making modern CSS scrollbar properties the universal standard for over 90% of desktop users.
Why does my custom scrollbar disappear on macOS Safari?
macOS hides scrollbars by default unless the user is actively scrolling. To force scrollbars to stay visible in system settings, users must enable "Show scroll bars: Always" in System Settings > Appearance.
How does scrollbar-gutter: stable prevent layout shift (CLS)?
When content dynamically loads, the appearance of a physical scrollbar reduces viewport width by ~16px, causing page elements to suddenly jump. `scrollbar-gutter: stable` permanently reserves that space, eliminating Cumulative Layout Shift.
Can I add a gradient or border-radius to scrollbars using the standard syntax?
The official `scrollbar-color` specification only accepts flat color values. To add linear gradients, shadows, or rounded pill borders, you must include the `::-webkit-scrollbar-thumb` pseudo-elements.
Is styling scrollbars accessible for visually impaired users?
Yes, provided you maintain a strong contrast ratio (at least 3:1) between the scrollbar thumb and its track. Avoid making the thumb completely transparent or paper-thin.