Open In App

How to dynamically get the content width of a div using AngularJS ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to dynamically get the content width of a div using AngularJS, along with understanding its basic implementation through the examples.

The content width of a div can dynamically get using clientWidth and scrollWidth properties depending upon the user’s 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: In this example, the Content Width of the div using the ClientWidth property will return the width of the entire content of an element. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
  
    <style>
        #div1 {
            height: 100px;
            width: 300px;
            border: 2px solid black;
            overflow: scroll;
            margin: auto;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body style="text-align: 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>
  
    <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: In this example, the Content Width of div using the scrollWidth property will return the width of the entire content of an element.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
  
    <style>
        #div1 {
            height: 100px;
            width: 300px;
            border: 2px solid black;
            overflow: scroll;
            margin: auto;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body style="text-align: 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>
  
    <script>
        function contentwidth() {
            var ans = "Content-width: "
                + angular.element(document
                .getElementById("div1").scrollWidth)
                + "px<br>";
            document.getElementById("p1").innerHTML = ans;
        }
    </script>
</body>
  
</html>


Output:



Last Updated : 14 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads