Skip to content

Instantly share code, notes, and snippets.

@kosciolek
Last active July 3, 2021 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosciolek/02a31bcfa666652b7e0ada57b6e7651d to your computer and use it in GitHub Desktop.
Save kosciolek/02a31bcfa666652b7e0ada57b6e7651d to your computer and use it in GitHub Desktop.
Measure scrollbar width
function getScrollbarWidth() {
const outer = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.overflow = 'scroll';
document.body.appendChild(outer);
const inner = document.createElement('div');
outer.appendChild(inner);
const scrollbarWidth = (outer.offsetWidth - inner.offsetWidth);
outer.parentNode.removeChild(outer);
return scrollbarWidth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment