As I’ve recently started developing a custom scrollbar plugin, I was looking for a way to detect the browser scrollbars width.
As you may know, there is no specification on the size of those elements and they are different from platform to platform (and from browser to browser).
Hopefully, calculating the size is not difficult thanks to two DOM properties:
offsetWidth returns the layout width of an element and it is a measurement which includes borders, padding and scrollbar (if any).
clientWidth instead returns the inner width of an element without borders and scrollbar.
So, getting the scrollbar width is as simple as subtracting
offsetWidth on an element with forced scrollbars and without padding.