Set a background color for a document:
document.body.style.backgroundColor = "red";
Try it Yourself »
More "Try it Yourself" examples below.
The backgroundColor property sets or returns the background color of an element.
HTML Styles: The background Property
CSS Tutorial: CSS Backgrounds
CSS Reference: The background-color Property
Return the backgroundColor property:
object.style.backgroundColor
Set the backgroundColor property:
object.style.backgroundColor
= "color|transparent|initial|inherit"
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 |
Default Value: | transparent |
---|---|
Return Value: | A String, representing the background color |
CSS Version | CSS1 |
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 |
Set a background color of a specific <div> element:
document.getElementById("myDiv").style.backgroundColor = "lightblue";
Try it Yourself »
Return the background color of a specific <div> element:
let color = document.getElementById("myDiv").style.backgroundColor;
Try it Yourself »
Return the background color of a document:
let color = document.body.style.backgroundColor;
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!