You build a custom scrollbar in JavaScript by hiding the browser's native scrollbar with CSS and styling your own track and thumb, then using a scroll event listener to keep the thumb's position and size in sync with the real scroll offset. The scroll event still fires on the scrolling element, so you are not reinventing scrolling — you are only drawing an indicator over it. Before you start, you need a page with a tall element (a fixed-height container with overflow content is the usual choice), basic HTML and CSS knowledge, and a browser's developer tools open so you can watch the console and inspect the result.

What You'll Need

  • A working HTML file you can open in a browser — no build tools, frameworks or libraries required.
  • A scrollable container: a fixed height (for example height: 300px) with overflow-y: scroll and content taller than that height. Without overflow content there is nothing to scroll and nothing to size.
  • A browser with developer tools (Chrome, Firefox, Safari or Edge) so you can check the console for errors and inspect computed styles.
  • A text editor. Everything below is plain HTML, CSS and JavaScript.
  • Optional but useful: a basic understanding of how position: absolute works inside a relatively positioned parent, since the thumb is positioned that way.

Step-by-Step

  1. Build the markup. Wrap your scrolling content in an outer container and add two extra divs for the scrollbar: one for the track and one for the thumb inside it. A common structure is <div class="scroller"><div class="content">…</div><div class="track"><div class="thumb"></div></div></div>. Keep the track a sibling of the content, not a child of it, so it does not scroll away with the text.
  2. Set up the container's CSS. Give .scroller a fixed height (or max-height), position: relative, and overflow: hidden. Put overflow-y: auto and height: 100% on .content. The content element is the one that actually scrolls; the parent stays still. Reserve space on the right so text does not run under your custom bar — either add right padding to .content or position the track with an offset.
  3. Hide the browser's own scrollbar. On the scrolling element, use scrollbar-width: none; (Firefox) and .content::-webkit-scrollbar { display: none; } (Chrome, Edge, Safari). Both are needed because the two vendor paths cover different browsers. Scrolling still works with the scrollbar hidden; keyboard arrows, the mouse wheel and touch still scroll the element.
  4. Position and style the track and thumb with CSS. The track should be absolutely positioned down the right edge (position: absolute; top: 0; right: 0; width: 8px; height: 100%;). The thumb sits inside it with position: absolute; left: 0; width: 100%; border-radius: 4px; background: #888;. Give the track a faint background so users can see the full scroll range. These are placeholder values — pick widths and colours that match your design.
  5. Calculate the thumb height once the page loads. In JavaScript, read the visible height and the full scroll height of the content element, then size the thumb proportionally: thumbHeight = (clientHeight / scrollHeight) * trackHeight. If the ratio is 1 or more, there is nothing to scroll, so hide the track entirely with display: none — otherwise you show a full-height bar that never moves, which looks broken.
  6. Update the thumb position on scroll. Add a scroll listener to the content element. On each event, read el.scrollTop, divide it by the maximum scroll (scrollHeight - clientHeight) to get a ratio between 0 and 1, and multiply that by the free travel of the thumb (trackHeight - thumbHeight). Set the result as thumb.style.transform = 'translateY(' + offset + 'px)' or use top. Thumb travel is the key detail: the thumb cannot move the full track height, only the track height minus its own height.
  7. Re-run the size calculation when the layout changes. Resizing the window, loading fonts, images finishing, or content being added all change scrollHeight. Wrap your sizing code in a named function and call it again from a resize listener and, ideally, from a ResizeObserver on the content element. Without this, the thumb keeps its old size and its position drifts out of sync.
  8. Add pointer support so the thumb can be dragged. On pointerdown for the thumb, record the starting pointer Y and the starting scrollTop. On pointermove, work out how far the pointer has moved in pixels, convert that distance to a scroll distance (multiply by maxScroll / thumbTravel), and set el.scrollTop accordingly. Use setPointerCapture so the drag continues if the pointer leaves the thumb. On pointerup, release the capture and stop tracking.
  9. Make clicking the track jump the view. Listen for a click on the track. If the target is the track itself (not the thumb), read the click position relative to the track, centre the thumb there, and set scrollTop to the matching value. This gives users the familiar behaviour of clicking above or below the thumb to page or jump.
  10. Test with the keyboard and a screen reader. Click into the content and press the arrow keys, Page Down and space — the content should still scroll and your thumb should follow. If you want the custom bar to be focusable, add a role="scrollbar", aria-valuenow, aria-valuemin and aria-valuemax and update them in your scroll handler. A decorative bar that duplicates the native one needs no ARIA at all; the content itself remains keyboard-scrollable.

Common Mistakes

The most frequent problem is hiding the scrollbar on the wrong element. If you set overflow: hidden on the container but the content wrapper is the scroller, the ::-webkit-scrollbar rule must target the wrapper too — check in dev tools which element actually has a scroll offset.

Second, many people compute thumb travel as the full track height. That makes the thumb jump to the very bottom while the page is still mid-scroll, or hit the bottom before the content ends. Subtract the thumb's own height.

Third, forgetting to handle the no-overflow case. A track that fills the whole height and never moves looks broken; hide it when scrollHeight is not greater than clientHeight. Fourth, sizing once on load and never again — any content change throws the bar out of sync. Finally, using mousemove alone breaks touch devices; pointer events cover mouse, pen and touch with one code path.

Tips

Prefer CSS-only where you can. Modern browsers support scrollbar-width, scrollbar-color and the ::-webkit-scrollbar pseudo-elements, which style the real scrollbar without JavaScript and keep native drag, track clicks and accessibility for free. Only go the JavaScript route when you need effects those can't do, such as a bar that fades in, animates, or sits outside the scrolling box.

Keep the custom bar purely visual and let the browser scroll — you avoid almost every bug by doing so. Make the thumb faint by default and darker while :hover or while dragging. Watch performance: scroll fires often, but writing a single transform is cheap. Avoid reading layout values inside the scroll handler; cache scrollHeight, clientHeight and track height, and recalculate them only on resize or content change.

Troubleshooting

The content does not scroll at all. The scrolling element needs a constrained height. If its parent has height: auto it will grow to fit the content instead of overflowing. Set the height on the container.

The native scrollbar is still visible. The vendor rule is applied to a different element, or a more specific selector is overriding it. Inspect the scroller and confirm which rule wins.

The thumb does not reach the bottom. Use scrollHeight - clientHeight as the divisor, not scrollHeight. A ratio slightly under 1 is the usual symptom.

The thumb runs past the track end. Your travel calculation uses the full track height instead of track height minus thumb height.

The bar is out of sync after rotating a phone or resizing. Recalculate on resize and via a ResizeObserver.

Dragging sticks or stops when the pointer leaves the thumb. Use setPointerCapture and listen for pointermove and pointerup on the element that captured the pointer. If scrolling feels slow or too fast, your drag-to-scroll conversion factor is wrong — it should be maxScroll / thumbTravel.

Frequently Asked Questions

Can I style the scrollbar without JavaScript?

Yes. scrollbar-width and scrollbar-color work in Firefox, and the ::-webkit-scrollbar pseudo-elements style it in Chrome, Edge and Safari. That is the simplest option if you only want different colours or a thinner bar.

Does hiding the scrollbar break scrolling for keyboard users?

No, as long as the element keeps its overflow and focusability. Arrow keys, Page Down and space still scroll the focused container. Just never set overflow: hidden on the element that should scroll — that disables scrolling rather than hiding the bar.

How do I make the thumb the right size?

Divide the visible height by the full scroll height and multiply by the track height: (clientHeight / scrollHeight) * trackHeight. This mirrors how long the native thumb is, so users get a familiar sense of how much content remains.

Why does the custom scrollbar stop working after I add content?

Because the thumb was sized once. Scroll height changed but your calculations did not. Call the sizing function again after the content updates, or observe the element with a ResizeObserver.

Does this work on touch screens?

The scroll event and position updates work fine. Dragging the thumb needs pointer events rather than mouse events, and on phones the native touch scrolling is often better left in charge — consider showing the custom bar only on devices with a mouse.

Should I build a custom scrollbar for a whole page or just a container?

Start with a container you control. Styling the document's own scrollbar is possible with the same CSS techniques, but a JavaScript replacement for the page scrollbar is fragile across browsers and offers little gain.