HTML DOM Style backgroundColor Property


The backgroundColor property is used for setting or returning background color for an element. We can specify values by standard names, rgb(), rgba(), hsl() or hsla().

Syntax

Following is the syntax for −

Setting the backgroundColor property −

object.style.backgroundColor = "color|transparent

Values

The above property values are explained as follows −

Sr.NoValue & Description
1Color
For specifying the background color.
2Transparent
Sets the background color to transparent i.e the underlying content can be seen. It is the default value.

Example

Let us look at the example for the backgroundColor property −

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   #DIV1{
      background-color: rgba(10,122,102,0.1);
   }
</style>
<script>
   function changeBackColor(){
      document.getElementById("DIV1").style.backgroundColor="rgba(1,200,55,0.5)";
      document.getElementById("Sample").innerHTML="The background color is now changed";
   }
</script>
</head>
<body>
<div id="DIV1">Duis tincidunt urna a interdum consectetur. Pellentesque nec pretium ante.
In nisl magna, vestibulum non vulputate vel, feugiat ac tellus. Sed tincidunt id leo at mollis.
Praesent pellentesque purus vitae scelerisque gravida. Pellentesque pulvinar scelerisque sem,
at euismod sapien tristique et.</div>
<p>Change the above div background color by clicking the below button</p>
<button onclick="changeBackColor()">CHANGE COLOR</button>
<p id="Sample"></p>
</body>
</html>

Output

This will produce the following output −

On clicking the CHANGE CLIP button −

Updated on: 15-Feb-2021

857 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements