Skip to content

Instantly share code, notes, and snippets.

@itrelease
Created October 25, 2011 14:10
Show Gist options
  • Save itrelease/1312855 to your computer and use it in GitHub Desktop.
Save itrelease/1312855 to your computer and use it in GitHub Desktop.
Calculate scrollbar width
/*
.scrollbar-measure {
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
}
*/
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment