<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[whynotsaloni]]></title><description><![CDATA[just trying to create a net positive effect on the world]]></description><link>https://saloni-nahak.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/69b7d4e91cf26228b3de267a/d5c12fb5-9f2c-40dd-acc4-a0ee513283c6.jpg</url><title>whynotsaloni</title><link>https://saloni-nahak.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Fri, 20 Nov 2026 17:16:54 GMT</lastBuildDate><atom:link href="https://saloni-nahak.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Create a Custom Scrollbar in React]]></title><description><![CDATA[Scrollbars are one of the most overlooked UI elements in web applications. The default browser scrollbar often doesn’t match the design language of modern…]]></description><link>https://saloni-nahak.hashnode.dev/how-to-create-a-custom-scrollbar-in-react</link><guid isPermaLink="true">https://saloni-nahak.hashnode.dev/how-to-create-a-custom-scrollbar-in-react</guid><category><![CDATA[CSS]]></category><dc:creator><![CDATA[saloni]]></dc:creator><pubDate>Mon, 16 Mar 2026 10:51:35 GMT</pubDate><content:encoded><![CDATA[<p>Scrollbars are one of the most overlooked UI elements in web applications. The default browser scrollbar often doesn’t match the design language of modern apps.</p>
<p>In this guide, you’ll learn how to create a custom scrollbar in React, ranging from simple CSS styling to a fully custom React-powered scrollbar component.</p>
<p>We’ll cover:</p>
<p>Styling the native scrollbar with CSS</p>
<p>Creating a reusable custom scrollbar component in React</p>
<p>Handling scroll synchronization and thumb dragging</p>
<ol>
<li>Styling the Native Scrollbar (Quick Method)</li>
</ol>
<p>The easiest way to customize scrollbars in React is with CSS pseudo-elements.</p>
<p>This works in Chrome, Edge, and Safari using ::-webkit-scrollbar.</p>
<p>Example /* scrollbar width */ ::-webkit-scrollbar { width: 10px; }</p>
<p>/* scrollbar track */ ::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 10px; }</p>
<p>/* scrollbar thumb */ ::-webkit-scrollbar-thumb { background: #888; border-radius: 10px; }</p>
<p>/* hover effect */ ::-webkit-scrollbar-thumb:hover { background: #555; }</p>
<p>Then apply it to your scroll container.</p>
<p>function ScrollContainer() { return ( &lt;div style={{ height: "300px", overflowY: "scroll" }}&gt; {Array.from({ length: 50 }).map((_, i) =&gt; (</p>
<p>Item {i}</p>
<p>))} ); } Pros</p>
<p>Very simple</p>
<p>No JavaScript needed</p>
<p>Good for quick styling</p>
<p>Cons</p>
<p>Limited control</p>
<p>Not fully cross-browser</p>
<p>Cannot fully customize behavior</p>
<p>For full control, we need to build a custom scrollbar component.</p>
<ol>
<li>Building a Custom Scrollbar Component in React</li>
</ol>
<p>Instead of styling the browser scrollbar, we hide it and build our own scrollbar UI.</p>
<p>Concept</p>
<p>The custom scrollbar consists of three parts:</p>
<p>Container — wrapper component</p>
<p>Scrollable Content</p>
<p>Scrollbar Track + Thumb</p>
<p>Project Structure components/ CustomScrollbar.jsx CustomScrollbar.css 3. Creating the Custom Scrollbar Component CustomScrollbar.jsx import { useRef, useState, useEffect } from "react"; import "./CustomScrollbar.css";</p>
<p>export default function CustomScrollbar({ children }) { const contentRef = useRef(null); const [thumbHeight, setThumbHeight] = useState(20); const [thumbTop, setThumbTop] = useState(0);</p>
<p>useEffect(() =&gt; { const el = contentRef.current;</p>
<pre><code class="language-plaintext">const updateThumb = () =&gt; {
  const { scrollHeight, clientHeight, scrollTop } = el;

  const height = Math.max(
    (clientHeight / scrollHeight) * clientHeight,
    20
  );

  const top = (scrollTop / scrollHeight) * clientHeight;

  setThumbHeight(height);
  setThumbTop(top);
};

updateThumb();
el.addEventListener("scroll", updateThumb);

return () =&gt; el.removeEventListener("scroll", updateThumb);
</code></pre>
<p>}, []);</p>
<p>return (</p>
<p>{children}</p>
<pre><code class="language-plaintext">  &lt;div className="scrollbar"&gt;
    &lt;div
      className="scroll-thumb"
      style={{
        height: thumbHeight,
        transform: `translateY(${thumbTop}px)`
      }}
    /&gt;
  &lt;/div&gt;
&lt;/div&gt;
</code></pre>
<p>); } 4. Styling the Scrollbar CustomScrollbar.css .scroll-container { position: relative; height: 300px; }</p>
<p>.scroll-content { height: 100%; overflow-y: scroll; padding-right: 10px; }</p>
<p>/* hide native scrollbar */ .scroll-content::-webkit-scrollbar { display: none; }</p>
<p>.scrollbar { position: absolute; right: 2px; top: 0; width: 8px; height: 100%; background: transparent; }</p>
<p>.scroll-thumb { width: 100%; background: #888; border-radius: 4px; position: absolute; cursor: pointer; } 5. Using the Custom Scrollbar import CustomScrollbar from "./CustomScrollbar";</p>
<p>function App() { return ( {Array.from({ length: 50 }).map((_, i) =&gt; (</p>
<p>Item {i}</p>
<p>))} ); }</p>
<p>export default App;</p>
<p>Now your React app uses a custom scrollbar instead of the native one.</p>
<ol>
<li>Adding Drag Support (Advanced)</li>
</ol>
<p>To make the scrollbar thumb draggable, we handle mouse events.</p>
<p>Example logic:</p>
<p>const handleMouseDown = (e) =&gt; { const startY = e.clientY;</p>
<p>const onMouseMove = (moveEvent) =&gt; { const delta = moveEvent.clientY - startY; contentRef.current.scrollTop += delta * 2; };</p>
<p>const onMouseUp = () =&gt; { document.removeEventListener("mousemove", onMouseMove); document.removeEventListener("mouseup", onMouseUp); };</p>
<p>document.addEventListener("mousemove", onMouseMove); document.addEventListener("mouseup", onMouseUp); };</p>
<p>Attach it to the thumb:</p>
<p>7. Libraries That Provide Custom Scrollbars</p>
<p>If you prefer a production-ready solution, consider these libraries:</p>
<p>react-custom-scrollbars</p>
<p>simplebar-react</p>
<p>react-scrollbars-custom</p>
<p>These libraries handle:</p>
<p>touch support</p>
<p>accessibility</p>
<p>performance optimization</p>
<p>cross-browser compatibility</p>
<p>Conclusion</p>
<p>Custom scrollbars can significantly improve the visual polish and user experience of your React applications.</p>
<p>You have three main options:</p>
<p>Method Difficulty Flexibility CSS Scrollbar Styling Easy Low Custom React Component Medium High Library Solution Easy Very High</p>
<p>For small projects, CSS styling is enough. For design-heavy applications like dashboards or design systems, building a custom React scrollbar provides much greater control.</p>
]]></content:encoded></item></channel></rss>