THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Style backgroundColor Property

Style Object Reference Style Object

Example

Set a background color for a document:

document.body.style.backgroundColor = "red";
Try it yourself »

More "Try it Yourself" examples below.


Definition and Usage

The backgroundColor property sets or returns the background color of an element.


Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
backgroundColor 1.0 4.0 1.0 1.0 3.5

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

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:

alert(document.getElementById("myDiv").style.backgroundColor);
Try it yourself »

Example

Return the background color of a document:

alert(document.body.style.backgroundColor);
Try it yourself »

Related Pages

CSS tutorial: CSS Background

CSS reference: background-color property

HTML DOM reference: background property


Style Object Reference Style Object