The content width of a div can dynamically get using clientWidth and scrollWidth properties depending upon the user requirement. If a user wants to know the space required by actual displayed content including the space taken by padding but not including the scrollbars, margins or borders, then the user can use any of the following procedures that will return the width of the entire content of an element.
- Using Element.ClientWidth property
- Using Element.scrollWidth property
Example 1: Content Width of div using ClientWidth property will return the width of the entire content of an element.
<!DOCTYPE html> <html> <head> <script src= </script> <style> #div1 { height: 100px; width: 300px; border: 2px solid black; overflow: scroll; } h1 { color: green; } </style> </head> <body> <center> <h1>GeeksforGeeks</h1> <h3>Geeting Content width</h3> <div id="div1"> Calculate content width of a div on GeeksforGeek. GeeksforGeeks is a computer science portal which helps students to learn various programming language and master data structures and algorithms. There are various courses available to learn new skills. </div> <br> <button onclick="contentwidth()">Content Width of Div</button> <p id="p1"></p> </center> <script> function contentwidth() { var ans = "Content-width: " + angular.element(document.getElementById("div1").clientWidth) + "px<br>"; document.getElementById("p1").innerHTML = ans; } </script> </body> </html> |
Output:

Example 2: Content Width of div using scrollWidth property will return the width of the entire content of an element.
<!DOCTYPE html> <html> <head> <script src= </script> <style> #div1 { height: 100px; width: 300px; border: 2px solid black; overflow: scroll; } h1 { color: green; } </style> </head> <body> <center> <h1>GeeksforGeeks</h1> <h3>Getting Content width</h3> <div id="div1"> Calculate content width of a div on GeeksforGeek. GeeksforGeeks is a computer science portal which helps students to learn various programming language and master data structures and algorithms. There are various courses available to learn new skills. </div> <br> <button onclick="contentwidth()">Content Width of Div</button> <p id="p1"></p> </center> <script> function contentwidth() { var ans = "Content-width: " + angular.element(document.getElementById("div1").scrollWidth) + "px<br>"; document.getElementById("p1").innerHTML = ans; } </script> </body> </html> |
Output:

Recommended Posts:
- How to dynamically get the content height of a div using AngularJS ?
- How to dynamically set the height and width of a div element using jQuery ?
- How to copy the content of a div into another div using jQuery ?
- How to set div width to fit content using CSS ?
- How to make div width expand with its content using CSS ?
- How to clear all div's content inside a parent div ?
- How to overlay one div over another div using CSS
- How to Load External JS Scripts Dynamically in AngularJS ?
- How to add input fields dynamically on button click in AngularJS ?
- How to get file content and other details in AngularJS?
- How to find the width of a div using vanilla JavaScript?
- How to animate div width and height on mouse hover using jQuery ?
- How to center a div within another div?
- How to align content of a div to the bottom using CSS ?
- How to make div height expand with its content using CSS ?
- How to clear the content of a div using JavaScript?
- Print the content of a div element using JavaScript
- How to detect and change the content/style of a div using jQuery ?
- How to use a div as content for Twitter's Popover?
- How to get the javascript function parameter names/values dynamically?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

