
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element offsetWidth Property
The HTML DOM Element offsetWidth property is used to retrieve the total viewable width of that element on the web page, including its content width,horizontal padding and borders, measured in pixels.
Syntax
Following is the syntax of the HTML DOM Element offsetWidth property −
element.offsetWidth
Parameters
Since this is a property, it does not accept any parameter.
Return Value
This property returns an integer value that holds the total pixel width of an element, including its padding, border, and optionally scrollbar width.
Example 1: Offset Width on Button Click
The following program demonstrates the usage of the HTML DOM Element offsetWidth property. It returns the total viewable width of the <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element offsetWidth</title> <style> #exampleElement { width: 200px; padding: 10px; border: 1px solid black; background-color: lightblue; } </style> </head> <body> <h3>HTML DOM Element offsetHeight Property</h3> <div id="exampleElement">Example Element</div><br> <button onclick="showWidth()">Show Width</button> <script> function showWidth() { const element = document.getElementById('exampleElement'); const width = element.offsetWidth; alert(`Width of the 'div' element: ${width}px`); } </script> </body> </html>
The above program alert the total viewable width of the 'div' element.
Example 2: Offset Width with Padding and Border
Here is another example of using the HTML DOM Element offsetWidth property. This property retrieves the total viewable width, including padding and border −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element offsetWidth</title> <style> #ex{ width: 200px; padding: 20px; border: 2px solid black; background-color: rgb(62, 161, 23); text-align: center; color: white; font-size: 22px; } #wi { color: green; } button{ padding: 10px; } </style> </head> <body> <h3>HTML DOM Element offsetWidth Property</h3> <div id="ex">Example Element</div> <p>Click below button to show Width including Padding and Border</p> <button onclick="showWidth()">Show</button> <p id="wi"></p> <script> const exampleElement=document.getElementById('ex'); const widthDisplay = document.getElementById('wi'); function showWidth() { const widthWithPaddingBorder = exampleElement.offsetWidth; widthDisplay.textContent = `Width including padding and border: ${widthWithPaddingBorder}px`; } </script> </body> </html>
When the button is clicked, the program displays the total viewable width, which includes padding and border.
Example 3: Calculating the Width of the Scrollbar Dynamically
The example below shows how to use the offsetWidth property to calculate the width of the scrollbar element dynamically −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element offsetWidth</title> <style> #scroll { width: 200px; height: 200px; overflow: scroll; border: 1px solid black; padding: 20px; } #cont { width: 100%; height: 400px; } button { padding: 10px; } </style> </head> <body> <h3>HTML DOM Element offsetHeight Property</h3> <div id="scroll"> <div id="cont">Scrollable Content</div> </div><br> <button onclick="calculate()">Calculate Scrollbar Width</button> <p id="scrollbar"></p> <script> function calculate() { const scCon = document.getElementById('scroll'); const scwid = scCon.offsetWidth - scCon.clientWidth; document.getElementById('scrollbar').innerHTML = `Scrollbar width: ${scwid}px`; } </script> </body> </html>
The above program displays the width of the scrollbar.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
offsetWidth | Yes | Yes | Yes | Yes | Yes |