Skip to content

Instantly share code, notes, and snippets.

@argyleink
Created December 3, 2025 18:23
Show Gist options
  • Select an option

  • Save argyleink/5729927a2c55c73ab6962c9582bfd4fe to your computer and use it in GitHub Desktop.

Select an option

Save argyleink/5729927a2c55c73ab6962c9582bfd4fe to your computer and use it in GitHub Desktop.
get the scrollbar sizes
function mockScollport() {
const scrollport = document.createElement('div')
scrollport.style.position = 'absolute'
scrollport.style.top = '-100dvh'
scrollport.style.left = '-100vw'
scrollport.style.width = '100px'
scrollport.style.height = '100px'
scrollport.style.overflow = 'scroll'
scrollport.style.visibility = 'hidden'
return scrollport
}
function determineScrollbarSize() {
const inner = document.createElement('div')
inner.style.width = '100%'
inner.style.height = '200px'
const scrollport = mockScollport()
scrollport.style.scrollbarWidth = 'auto'
const scrollportThin = mockScollport()
scrollportThin.style.scrollbarWidth = 'thin'
document.body.appendChild(scrollport)
document.body.appendChild(scrollportThin)
scrollport.appendChild(inner)
const scrollbarWidth = scrollport.offsetWidth - inner.offsetWidth
document.firstElementChild.style.setProperty('--scrollbar-size', `${scrollbarWidth}px`)
document.body.removeChild(scrollport)
scrollportThin.appendChild(inner)
const scrollbarThinWidth = scrollportThin.offsetWidth - inner.offsetWidth
document.firstElementChild.style.setProperty('--scrollbarThin-size', `${scrollbarThinWidth}px`)
document.body.removeChild(scrollportThin)
}
document.addEventListener('DOMContentLoaded', determineScrollbarSize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment