JavaScript | Window getComputedStyle() Method

The getComputedStyle() method is used to get all the computed CSS property and values of the specified element.
The use of computed style is displaying the element after stylings from multiple sources have been applied.
The getComputedStyle() method returns a CSSStyleDeclaration object.

Syntax:

window.getComputedStyle(element, pseudoElement)

Parameters:

  • element: The element to get the computed style for
  • pseudoElement: A pseudo-element to get. this is an optional parameter.

Example: Return font-family of text in div.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML | DOM Window getComputedStyle() Method
    </title>
  
    <style>
        div {
            color: white;
            text-align: justify;
        }
    </style>
</head>
  
<body>
    <center>
        <p>
            <button onclick="myFunction()">
                Try it
            </button>
        </p>
        <div id="test" style="height: 100px; 
                    background-color: green; 
                    font-size: 50px;
                    padding-left: 140px;
                    padding-top: 50px;">
            Geeks for Geeks
        </div>
        <p>
          The computed font-family of text 
          in the test div is:
          <span id="demo"></span>
        </p>
  
        <script>
            function myFunction() {
  
                var elem = document.getElementById("test");
                var theCSSprop =
                    window.getComputedStyle(
                        elem, null).getPropertyValue("font-family");
                document.getElementById("demo").innerHTML = 
                                  theCSSprop;
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:
Before click on button:

After click on button:

Supported Browsers: The browser supported by Window getComputedStyle() Method are listed below:

  • Google Chrome 11.0
  • Internet Explorer 9.0
  • Firefox 4.0
  • Opera 11.5
  • Safari 5


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.