Skip to content

Instantly share code, notes, and snippets.

@j-
Created September 27, 2018 00:22
Show Gist options
  • Save j-/22bcf8f182c2e511b7e6f486a9dbee15 to your computer and use it in GitHub Desktop.
Save j-/22bcf8f182c2e511b7e6f486a9dbee15 to your computer and use it in GitHub Desktop.
Easily measure scrollbar width and height
<div id="outer">
<div id="inner">
</div>
</div>
<style>
#outer {
width: 2em;
height: 2em;
overflow: scroll;
border: 1px solid red;
position: relative;
}
#inner {
width: 4em;
height: 4em;
border: 1px solid blue;
position: relative;
bottom: 0;
right: 0;
}
</style>
<script>
const outerRect = document.getElementById('outer').getClientRects()[0];
const innerRect = document.getElementById('inner').getClientRects()[0];
const scrollWidth = outerRect.right - innerRect.right;
const scrollHeight = outerRect.bottom - innerRect.bottom;
console.log(scrollWidth, scrollHeight);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment