Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to get the width of device screen in JavaScript ?
Next article icon

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:

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:

output


Next Article
How to get the width of device screen in JavaScript ?

P

PranchalKatiyar
Improve
Article Tags :
  • HTML
  • JavaScript
  • Web Technologies

Similar Reads

  • How to get the height of scroll bar using JavaScript ?
    To get the height of scroll bar we could use different approaches. In this article, we are given an HTML document and the task is to get the height of the scrollbar using JavaScript. Following are the different approaches to solving this problem which are discussed below: Table of Content Using Cont
    3 min read
  • How to get the position of scrollbar using JavaScript ?
    JavaScript is an amazing language and there are many functions available through which we can access any element of the HTML page through javascript. There are some simple techniques to get the scrollbar position that are discussed below: Approach 1: Whenever the function getScroll() is encountered,
    3 min read
  • How to get the width of device screen in JavaScript ?
    Given an HTML document that is running on a device and the task is to find the width of the working screen device using JavaScript. Example 1: This example uses window.innerWidth to get the width of the device screen. The innerWidth property is used to return the width of the device. [GFGTABS] HTML
    2 min read
  • How to get the Width and Height of an Image using JavaScript?
    Given an image and the task is to get the width and height of the image using JavaScript. The width and height property is used to display the image width and height. Syntax for width:let width = this.width;Syntax for height:let height = this.height;Example 1: This example selects the image and then
    2 min read
  • How to get the image size (height & width) using JavaScript?
    To get the size of an image (height and width), Element.clientHeight and Element.clientWidth properties are used. Element.clientHeight: We can access the inner height of an element with this property. This height includes the padding but not the margin and border. Element.clientWidth: We can access
    2 min read
  • 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 Get and Set Scroll Position of an Element using JavaScript ?
    In this article, we will learn how to get and set the scroll position of an HTML element using JavaScript. Approach: We will be using the HTML DOM querySelector() and addEventListener() methods and the HTML DOM innerHTML, scrollTop and scrollLeft properties. We create an HTML div element with an id
    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 div height using JavaScript ?
    In this article, we will see How to get the div height using Javascript. We can do this by following ways: Using the offsetHeight property.Using the clientHeight property.Using getBoundingClientRect() Method. Method 1: Using the offsetHeight property: The offsetHeight property of an element is a rea
    3 min read
  • How to Disable Scrolling Temporarily using JavaScript?
    Disabling scrolling temporarily using JavaScript means preventing users from scrolling a webpage during specific interactions, such as when a modal is open or a loading screen is displayed. Scrolling can be disabled using JavaScript using 2 methods: Table of Content Using window.onscroll functionSet
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences