Open In App

HTML | DOM Style opacity Property

The Style opacity property in HTML DOM used to set opacity level for an element. Opacity level defines the transparency level where value 1 represents not transparent, 0.5 represents 50% transparency and 0 represents completely transparent.It also returns opacity level of an element.

Syntax: 



object.style.opacity
object.style.opacity = "number|initial|inherit"

Property Values: 

Return Value: It returns a string representing the opacity level of an element. 



Example 1: 




<!DOCTYPE html>
<html>
<head>
    <title>DOM Style opacity Property </title>
</head>
<body>
    <center>
        <h1 style="color:green;">
                GeeksForGeeks
        </h1>
        <h2>DOM Style opacity Property </h2>
        <div id="gfg"> A Computer science portal for geeks</div>
 
        <button type="button" onclick="geeks()">
            set Decoration
        </button>
        <script>
            function geeks() {
                document.getElementById("gfg").style.opacity
                                                      = "0.3";
            }
        </script>
    </center>
</body>
</html>

Output: 

Example 2: 




<!DOCTYPE html>
<html>
<head>
    <title>DOM Style opacity Property </title>
    <style>
        #gfg {
            width: 150px;
            height: 40px;
            background-color: green;
        }
    </style>
</head>
<body>
    <center>
        <h1 style="color:green;">
                GeeksForGeeks
        </h1>
        <h2>DOM Style opacity Property </h2>
        <div id="gfg" style="color:white">
        A Computer science portal for geeks</div>
        <button type="button" onclick="geeks()">
            change opacity
        </button>
        <script>
            function geeks() {
                document.getElementById("gfg").style.opacity
                                                       = "0";
            }
        </script>
    </center>
</body>
</html>

Output: 

Supported Browsers: The browser supported by DOM Style opacity property are listed below: 


Article Tags :