CSS StyleDeclaration length Property

The length property is used to find the number of style declarations used for the particular element.

Syntax: It is used to return the length property.

element.style.length

Return Value: It returns the number of style declarations specified for an element in the form of integers.

Example: To show the working of the length property:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <tiltle>
        CSS StyleDeclaration length Property
    </title>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <!-- Adding inline style -->
    <p id="p1" style=" color:green; 
            font-size:20;">
      length Property
  </p>
  
    <p>
      Click the button to 
      return the length property
  </p>
  
    <button onclick="myFunction()">
        Get length
    </button>
  
    <p id="gfg"></p>
    <!-- Script to get length property -->
    <script>
        function myFunction() {
            var x = document.getElementById(
              "p1").style.length;
            
            document.getElementById("gfg").innerHTML =
                "Number of styles = " + x;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:
  • Supported Browsers: The supported browsers by Style Declaration length Property are listed below:

    • Google Chrome
    • Internet Explorer
    • Firefox
    • Opera
    • Safari


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.