How to get the Width of Scroll bar using JavaScript? Last Updated : 08 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth - clientWidth.Example 1: This example implements the above approach. html <!DOCTYPE HTML> <html> <head> <title> How to get the width of scroll bar using JavaScript ? </title> <style> body { text-align: center; } h1 { color: green; } #div { width: 200px; height: 150px; overflow: auto; margin: auto; text-align: justify; border: 1px solid black; } #GFG_UP { font-size: 17px; font-weight: bold; } #GFG_DOWN { font-size: 24px; font-weight: bold; color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <p id="GFG_UP"></p> <div id="div"> JavaScript has stormed the web technology and nowadays small software ventures to fortune 500, all are using node js for web apps. Recently wordpress.com has rewritten its dashboard in javascript, paypal also chose to rewrite some of its components in java script. Be it google/twitter/facebook, javascript is important for everyone. It is used in applications like single page applications, Geolocation APIs, net advertisements etc. </div> <br> <button onclick="GFG_FUN()"> click here </button> <p id="GFG_DOWN"></p> <script> let element = document.getElementById('div'); let el_up = document.getElementById('GFG_UP'); let el_down = document.getElementById('GFG_DOWN'); el_up.innerHTML = "Click on the button to get " + "the width of the scrollbar."; function GFG_FUN() { el_down.innerHTML = element.offsetWidth - element.clientWidth + "px"; } </script> </body> </html> Output: Example 2: In this example, we create an outer div element, adds an inner div inside it, and calculates the scrollbar width by subtracting the inner div's width from the outer div's width: html <!DOCTYPE HTML> <html> <head> <title> How to get the width of scroll bar using JavaScript ? </title> <style> body { text-align: center; } h1 { color: green; } .outer { width: 200px; height: 150px; overflow-y: auto; margin: auto; text-align: justify; border: 1px solid black; } #GFG_UP { font-size: 17px; font-weight: bold; } #GFG_DOWN { font-size: 24px; font-weight: bold; color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <p id="GFG_UP"></p> <div class="outer"> <div class="inner"> JavaScript has stormed the web technology and nowadays small software ventures to fortune 500, all are using node js for web apps. Recently wordpress.com has rewritten its dashboard in javascript, paypal also chose to rewrite some of its components in java script. Be it google/twitter/facebook, javascript is important for everyone. It is used in applications like single page applications, Geolocation APIs, net advertisements etc. </div> </div> <br> <button onclick="GFG_FUN()"> click here </button> <p id="GFG_DOWN"></p> <script> let element = document.getElementById('div'); let el_up = document.getElementById('GFG_UP'); let el_down = document.getElementById('GFG_DOWN'); el_up.innerHTML = "Click on the button to get " + "the width of the scrollbar."; function GFG_FUN() { let child = document.querySelector(".inner"); let scroll = child.parentNode.offsetWidth - child.offsetWidth; el_down.innerHTML = scroll + "px"; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to get the Width of Scroll bar using JavaScript? P PranchalKatiyar Follow Improve Article Tags : HTML Similar Reads How to find the width of a div using vanilla JavaScript? To measure the width of a div element we will utilize the offsetWidth property of JavaScript. This property of JavaScript returns an integer representing the layout width of an element and is measured in pixels. Below are the different approaches to finding the width of a div using vanilla JavaScrip 3 min read How to calculate Width and Height of the Window using JavaScript ? In this article, we will know to calculate the width & the height of the window using Javascript. Given an HTML document that is running on a Window & we need to find the height and width of the window. The Window innerWidth property is used for returning the width of a windowâs content area 2 min read How to get the diagonal length of the device screen using JavaScript ? Knowing the width and height of a browser window helps a web developer to enhancing the user experience. It can help in improving browser animations and the relative positioning of divisions and containers. Javascript provides a window object that represent an open browser window. It provides the va 3 min read How to get the Element's Actual Width and Height in JavaScript? In JavaScript, it is often necessary to retrieve the actual width and height of an HTML element. This information can be useful in a variety of scenarios, such as setting the size of an element dynamically, centering an element on the page, or determining the size of an element for animation purpose 5 min read How to detect page zoom level in all modern browsers using JavaScript ? In this article, we will discuss how the current amount of Zoom can be found on a webpage.These are the following methods: Table of ContentUsing outerWidth and innerWidth PropertiesUsing clientWidth and clientHeight PropertiesUsing the window.devicePixelRatio PropertyMethod 1: Using outerWidth and i 3 min read How to Get the Width of Hidden Element in jQuery? An HTML element can be hidden with the help of the .hide() jQuery function or we can hide easily by making visibility equals hidden in CSS. We can easily find the width of this hidden element in jQuery.Two kinds of width are defined with every HTML element i.e, innerWidth and the outerWidth of the e 3 min read How to hide scroll bar for inactivity? There are many ways to hide the scroll bar when the page is inactive. One such way is using the onscroll, onmousewheel, onclick, and onmousemove events, which help us to achieve our goal using basic HTML and JavaScript. Approach:The onscroll event is used to disable the scroll bar.The onscroll event 3 min read How to Add Scroll Bar in HTML? In HTML and CSS, scrollbars can allow users to navigate through the overflowed content within the specified area. They can appear when the content overflows the visible area of the element, such as the <div>, allowing users to scroll horizontally or vertically. we are going to add the scroll b 7 min read Web Window API | Window scrollbars property In Web APIWindow.scrollbars property returns the object of scrollbars, for which we can check the visibility. Syntax : objectReference = window.scrollbars Example : Check the visibility html <!DOCTYPE html> <html> <head> <title> Window scrollbars property </title> <s 1 min read HTML DOM scrollWidth Property The DOM scrollWidth property is used to return the width of an element. This property includes padding and also content that is not visible on the screen due to overflow but does not include a border, scrollbar, or margin. It is a read-only property. Syntax: element.scrollWidth Return Value: It retu 1 min read Like