Get Computed Style : Computed Style « Style « JavaScript Tutorial






This example uses the DOM style methods and will fail in Internet Explorer.

<html>
    <head>
        <title>Computed Style Example</title>
        <style type="text/css">
            div.special {
                background-color: red;
            }
        </style>
        <script type="text/javascript">
            function getBackgroundColor() {
                var oDiv = document.getElementById("div1");
                alert(document.defaultView.getComputedStyle(oDiv, null).backgroundColor);
            }
        </script>
 
    </head>
    <body>
        <div id="div1" class="special"></div>
        <input type="button" value="Get Background Color" onclick="getBackgroundColor()" />

    </body>
</html>








22.3.Computed Style
22.3.1.Get Computed Style