Open In App

HTML DOM Style backgroundColor Property

The backgroundColor property in HTML DOM is used to set or return the background-color of an element.

Syntax:



object.style.backgroundColor
object.style.backgroundColor ="color|transparent|initial|inherit"

Parameter: There are four parameters accepted by backgroundColor property  

Return Values: It returns a string value, which represents the background color.



Example 1: In this example, we will use the backgroundColor property in HTML DOM.




<!DOCTYPE html>
<html>
   
<head>
    <title>
        Style backgroundColor Property
    </title>
</head>
   
<body style="text-align:center">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h2>
        Style backgroundColor Property
    </h2>
    <button type="button" onclick="geeks()">
        Submit
    </button>
       
      <script>
        /* script to set background color */
        function geeks() {
            document.body.style.backgroundColor =
                "lightgreen";
        }
    </script>
</body>
   
</html>

Output: 

Example 2: In this example, we will use the backgroundColor property in HTML DOM.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Style backgroundColor Property
    </title>
    <style>
        #geeks {
            width: 250px;
            height: 100px;
            background-color: coral;
        }
    </style>
</head>
   
<body>
    <center>
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
        <h2>
            DOM Style backgroundColor Property
        </h2>
        <button onclick="geeks()">Submit</button>
        <div id="geeks">
            <h1>GeeksForGeeks</h1>
        </div>
       
        <script>
            function geeks() {
                document.getElementById("geeks").style.backgroundColor =
                    "lightgreen";
            }
        </script>
    </center>
</body>
 
</html>

Output: 

Supported Browsers: The browser supported by DOM style backgroundColor Properties are listed below:  


Article Tags :