
- 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 clientWidth Property
The HTML DOM Element clientWidth property is used to retrieve the client width of an element, including padding but excluding margin, border, or scrollbar width. The client width refers to the width of an element within the document.
As it is a read-only property, it is useful for responsive design and layout calculations.
Syntax
Following is the syntax of the HTML DOM Element clientWidth property −
element.clientWidth;
Parameters
Since this is a property, it will not accept any parameter.
Return Value
This property returns a number(in pixels) that holds the viewable width of the element.
Example 1: Retrieving the Width of an Element
The following program demonstrates the usage of the HTML DOM Element clientWidth property by retrieving an element width excluding border −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element clientWidth</title> <style> .example-element { width: 390px; border: 2px solid #333; padding: 10px; background-color: lightblue; } </style> </head> <body> <h3>HTML DOM Element clientWidth property</h3> <div class="example-element" id="exampleElement"> <p>This element has some content.</p> </div> <div id="output"> <p>Element Client Width:</p> <p>Width: <span id="width"> </span> pixels</p> </div> <script> document.addEventListener("DOMContentLoaded", function() { var element = document.getElementById("exampleElement"); var width = element.clientWidth; document.getElementById("width").textContent = width; }); </script> </body> </html>
Once the above program is executed, it displays the width of the "div" element.
Example 2: Width of a Specific Element
Here is another example of the HTML DOM Element clientWidth property. We use this property to retrieve the width of the first <header> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element clientWidth</title> <style> h1 { border-top: 10px solid black; padding: 10px; } </style> </head> <body> <h3>HTML DOM Element clientWidth Property</h3> <h1 id="firstHeader">First Header Element</h1> <div id="output"> <p>Top Border Width of the First Header Element:</p> <p>Width: <span id="width"></span> pixels</p> </div> <script> document.addEventListener("DOMContentLoaded", function() { var firstHeader = document.getElementById("firstHeader"); var outputWidth = document.getElementById("width"); function updateHeaderWidth() { var width = firstHeader.clientWidth; outputWidth.textContent = width; } // Initial update updateHeaderWidth(); // Update on window resize window.addEventListener("resize", updateHeaderWidth); }); </script> </body> </html>
The above program displays the width of the first "header" element.
Example 3: Updating the Width Dynamically
In the example below, we use the clientWidth property to update the width of an element dynamically whenever the cursor moves over it −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element clientWidth</title> <style> .box { background-color: lightblue; border: 2px solid blue; text-align: center; line-height: 100px; transition: width 0.3s ease; } </style> </head> <body> <h3>HTML DOM Element clientWidth Property</h3> <div class="box" id="box">Resize Me!</div> <div id="output">Client Width: <span id="width"> </span> pixels</div> <script> document.addEventListener("DOMContentLoaded", function() { var box = document.getElementById("box"); var outputWidth = document.getElementById("width"); // Function to update client width function updateClientWidth() { var width = box.clientWidth; outputWidth.textContent = width; } // Initial update updateClientWidth(); // Update on box resize (simulated) box.addEventListener("mouseover", function() { box.style.width = (box.clientWidth + 20)+ "px"; updateClientWidth(); }); box.addEventListener("mouseout", function(){ box.style.width = "200px"; updateClientWidth(); }); }); </script> </body> </html>
The above program displays a container (box), and when mouse over it, the width of the container will change (update) dynamically.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
clientWidth | Yes | Yes | Yes | Yes | Yes |