Detect Scrollbar Width with JavaScript

By  on  

I've recently been working on an advanced JavaScript-based grid solution and let me tell you: it's quite an undertaking. Making sure the grid is accessible, reactive, efficient, and cross-browser compatible is difficult, but even the minor workings of each of those are hard. One small task was detecting the width of the vertical scrollbar so that I know how wide the grid body truly was. Here's the tiny snippet to do it:

The CSS

The element we create for measurement will need to be positioned off screen so the user doesn't notice it:

/* way the hell off screen */
.scrollbar-measure {
	width: 100px;
	height: 100px;
	overflow: scroll;
	position: absolute;
	top: -9999px;
}

You could add these styles directly to the element, but they'd bloat the JavaScript portion a bit.

The JavaScript

The obvious parts is creating a DIV to inject into the DOM and adding the CSS class we created above:

// Create the measurement node
var scrollDiv = document.createElement("div");
scrollDiv.className = "scrollbar-measure";
document.body.appendChild(scrollDiv);

// Get the scrollbar width
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
console.warn(scrollbarWidth); // Mac:  15

// Delete the DIV 
document.body.removeChild(scrollDiv);

With the element in the page, subtracting the clientWidth from the offsetWidth gives the scrollbar size! The last step is removing the DIV from the DOM and done!

Since the scrollbar size is different between Mac and Windows (and even Internet Explorer 7 vs. other IE versions), this small but dynamic snippet is just what I needed to find the true content area of the container. Feel free to convert this JavaScript snippet into whatever JavaScript framework your prefer!

Recent Features

  • By
    From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    CSS Vertical Center with Flexbox

    I'm 31 years old and feel like I've been in the web development game for centuries.  We knew forever that layouts in CSS were a nightmare and we all considered flexbox our savior.  Whether it turns out that way remains to be seen but flexbox does easily...

  • By
    CSS Transforms

    CSS has become more and more powerful over the past few years and CSS transforms are a prime example. CSS transforms allow for sophisticated, powerful transformations of HTML elements.  One or more transformations can be applied to a given element and transforms can even be animated...

Discussion

  1. Handy! I’ve done something similar to an individual element but didn’t think to abstract it like you did.

    Fun thing: if you give a scrollable element a negative right-margin of the scrollbar width and a parent with its overflow hidden, you can have a mousewheel-scrollable element that looks overflow-hidden, Kinda esoteric, but useful if you’re trying to make a UI w/o any browser chrome.

  2. Graeme Coultrip

    Have you tried this on Safari on OS X Lion?

    • OS X Lion (but not earlier versions) floats scrollbars on top of the content. As a result there is no resize when a scrollbar appears. Which means this method of measuring scrollbar width does not work in Lion.

      I’m not aware of any method of getting the scrollbar width in Lion. :(

  3. swhiteman

    @Tommy: I do this with a all over the “control panel” area of our site. I don’t use a wrapper element, though, I use CSS clip to remove the scrollbar.

    I think it creates a really useful widget — obviously not as beautiful as you can create from scratch, but for basic form use it looks nice and “flat” and, as you note, is mousewheel/two-finger scrollable and retains other native behaviors (ctrl-click, etc.) without having to rewrite them.

  4. swhiteman

    Hmm, I deliberately didn’t wrap a tag hoping it would stay inline, shmoops.

    What I was saying is that, along similar lines, I like to clip: a multi-select to remove the scrollbar. Creates a very useful widget which retains native behaviors (mousewheel/two-finger, ctrl-click to select) while still fitting nicely into a flat design.

  5. Interesting. You can read some other interesting JavaScript tips and things at my blog: http://joezimjs.com
    It’s new and needs some more content, but it’s on its way.

  6. towry

    I don’t know why,It always show document.body is null in my firebug console.

  7. Daniel

    Hey man, I’m using Chrome and this line doesn’t work on my browser: “document.body.appendChild(scrollDiv);”

    I get this error in the web inspector console: “Uncaught TypeError: Cannot call method ‘appendChild’ of null”

    • You’ll want to put that in a domReady block, or at the bottom of the page.

  8. That’s brilliant, thanks very much.

    I’ve had some alignment issues caused with Lion’s hidden scrollbars and so now I use this to detect if the scrollbars are shown and then add a padding class to the affected divs ala

          if (scrollbarWidth > 0) {
            $(".trpChatContainer").addClass("scrollchat");
            $(".trpChatInputArea").addClass("scrollpush");
          }
    

    I then also style the scrollbar if it’s visible so that it looks more elegant:

    /* Track */
    .scrollchat::-webkit-scrollbar-track {
    /*    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); */
    }
     
    /* Handle */
    .scrollchat::-webkit-scrollbar-thumb {
        -webkit-border-radius: 10px;
        border-radius: 10px;
        background: rgba(0,0,0,0.2);
        margin-right: 1px;
        .transition300ms;
    }
    .scrollchat:hover::-webkit-scrollbar-thumb {
        -webkit-border-radius: 10px;
        border-radius: 10px;
        background: rgba(0,0,0,0.4);
        margin-right: 1px;
        .transition300ms;
    }
    .scrollchat::-webkit-scrollbar-thumb:window-inactive {
      background: rgba(0,0,0,0.1); 
    }
    
  9. Hey David, thnx you saved the day :-) A simple and elegant solution for a common problem. I’m using the script in combination with jQuery to correct a centered div maximage background script:

    $('.container').css('padding-left', scrollbarWidth); 
    
  10. Sakthi

    Thanks. Its working fine in IE

  11. Hi David, thank you for this post!
    Unfortunately I get a value of 0 in both Chrome and Firefox. Any idea why it isn’t working?

    • On Mac? Possibly because the scrollbar is technically over the content :/

  12. Oh, you beautiful genius. This is lovely.

  13. sanbor

    How about this:

    var scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
    
  14. Renzo

    I think the trick is to measure a div inside a div with scrollbar. Here’s a CodePen: http://codepen.io/parziphal/pen/qrZGLw

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!