Set a background color for a document:
document.body.style.backgroundColor = "red";
<!DOCTYPE html> <html> <body> <h1>Hello World!</h1> <button type="button" onclick="myFunction()">Set background color</button> <script> function myFunction() {/*from ww w.j a va 2s. com*/ document.body.style.backgroundColor = "red"; } </script> </body> </html>
The backgroundColor property sets or gets the background color of an element.
Property Values
Value | Description |
---|---|
color | Specifies the background color. |
transparent | Default . The background color is transparent |
initial | Sets this property to its default value. |
inherit | Inherits this property from its parent element. |
The backgroundColor property returns a String representing the background color.