Style backgroundColor Property
Example
Set a background color for a document:
document.body.style.backgroundColor = "red";
Try it Yourself »
More "Try it Yourself" examples below.
Description
The backgroundColor property sets or returns the background color of an element.
See Also:
HTML Styles: The background Property
CSS Tutorial: CSS Backgrounds
CSS Reference: The background-color Property
Syntax
Return the backgroundColor property:
object.style.backgroundColor
Set the backgroundColor property:
object.style.backgroundColor = "color|transparent|initial|inherit"
Property Values
Value | Description |
---|---|
color | Specifies the background color. Look at CSS Color Values for a complete list of possible color values |
transparent | Default. The background color is transparent (underlying content will shine through) |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Technical Details
Default Value: | transparent |
---|---|
Return Value: | A String, representing the background color |
CSS Version | CSS1 |
Browser Support
backgroundColor
is a CSS1 (1996) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
More Examples
Example
Set a background color of a specific <div> element:
document.getElementById("myDiv").style.backgroundColor = "lightblue";
Try it Yourself »
Example
Return the background color of a specific <div> element:
let color = document.getElementById("myDiv").style.backgroundColor;
Try it Yourself »
Example
Return the background color of a document:
let color = document.body.style.backgroundColor;
Try it Yourself »
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.