Cool Web Scrollbars — Examples, Code, and Best Practices
What they are
Custom web scrollbars replace or style the browser default scrollbar to better match a site’s design, improve usability, or add subtle interaction cues.
When to use them
- Brand fit: when a site’s visual identity benefits from matched UI elements.
- Micro-interactions: to add animated or responsive cues.
- Large content areas: when improved visual prominence or custom behavior helps navigation.
Examples (design patterns)
- Minimal thin track: slim bar with subtle hover enlargement.
- Overlay translucent: semi-transparent thumb over content for immersive layouts.
- Persistent themed: full-height colored track matching brand palette.
- Auto-hide with fade: appears on scroll, fades out after inactivity.
- Scrollable progress bar: top-of-page thin bar indicating scroll progress.
CSS examples
- For WebKit browsers (Chrome, Safari, Edge Chromium):
css
/Track /::-webkit-scrollbar { width: 10px; height: 10px; }::-webkit-scrollbar-track { background: transparent; }::-webkit-scrollbar-thumb { background: linear-gradient(180deg,#666,#333); border-radius: 8px; border: 2px solid rgba(255,255,255,0.06);}::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg,#777,#222); }
- For Firefox (using scrollbar-width and scrollbar-color):
css
/ Applies to the root or specific element */:root { scrollbar-width: thin; scrollbar-color: #444 transparent;}
- Cross-browser progressive enhancement: provide functional defaults for all browsers, then add WebKit and Firefox rules for enhanced visuals.
JavaScript examples (for advanced behavior)
- Auto-hide on inactivity:
js
const el = document.querySelector(‘.scrollable’);let hideTimer;el.addEventListener(‘scroll’, () => { el.classList.add(‘scrolling’); clearTimeout(hideTimer); hideTimer = setTimeout(() => el.classList.remove(‘scrolling’), 1000);});
- Custom scrollbar using a separate DOM element (for complete control):
- Create a track/thumb element, sync thumb size/position to scrollTop/scrollHeight, and handle pointer events to drag the thumb.
Accessibility considerations
- Don’t remove native behavior—ensure keyboard and touch scrolling continue to work.
- Contrast & size: maintain sufficient color contrast and an accessible hit target (recommend ~24px) or provide alternative controls.
- Reduce motion: respect prefers-reduced-motion for animations.
- Screen readers: avoid changing focus order; ensure content remains accessible.
Performance tips
- Prefer CSS-only solutions when possible.
- Avoid layout thrashing in JS thumb-sync (use requestAnimationFrame).
- For large scrollable areas, avoid oversized DOM updates during scroll.
Best practices checklist
- Progressive enhancement: functional defaults then style where supported.
- Test across devices: desktop, mobile, touch, and keyboard-only.
- Respect platform conventions: mobile OS scroll behavior and momentum expectations.
- Accessibility first: keyboard focus, contrast, and reduced motion.
- Optimize for performance: CSS where possible, rAF for JS.
Tools & libraries
- SimpleBar — lightweight cross-browser custom scrollbars.
- OverlayScrollbars — feature-rich with theming and accessibility options.
- PerfectScrollbar — minimal footprint but
Leave a Reply