Skip to content

Instantly share code, notes, and snippets.

@ciases
Last active January 25, 2018 09:09
Show Gist options
  • Save ciases/aeff65950f2bbafc8da699d1711fb7f3 to your computer and use it in GitHub Desktop.
Save ciases/aeff65950f2bbafc8da699d1711fb7f3 to your computer and use it in GitHub Desktop.
Get scrollbar width

Get scrollbar width

JS:

  function getScrollWidth() {
    var scrollDiv = document.createElement('div');
    scrollDiv.className = 'helper-scrollbar-width';
    document.body.appendChild(scrollDiv);

    var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;

    document.body.removeChild(scrollDiv);

    return scrollbarWidth;
  }

CSS:

.helper-scrollbar-width {
  position: absolute;
  top: -9999px;
  width: 100px;
  height: 100px;
  overflow: scroll;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment