Skip to: content, navigation

by Tommy Olsson and Paul O’Brien

background-color (CSS property)

Browser support full matrix
IE5.5+ FF1+ Saf1.3+ Op9.2+
Buggy Full Full Full
Spec
Inherited Initial Version
No transparent CSS1
background-color: { color | transparent | inherit } ;

Example

This style rule assigns a white background (#fff) to the element with ID "example" :

#example{
  background-color: #fff;
}

Try it yourself!View all demos

Description

This property sets the background-color of an element; it’s good practice to specify a foreground color (color) at the same time, to ensure that conflicts don’t arise with colors or backgrounds that are defined elsewhere.

The background of an element is the area covered by the width and height of that element (whether those dimensions are set explicitly, or the content dictates them); it also includes the area covered by padding and borders. A background-color (or background-image) that’s applied to an element will appear beneath the foreground content of that element, and the area covered by the padding and border properties for the element. This coverage area is evident where an element has transparent (or dotted or dashed) borders, and the background is seen beneath the borders (or between the dots). Note that Internet Explorer versions up to and including 6 don’t support transparent borders.

Some area of the element in question must be visible if the background-color is to show through. If the element has no intrinsic height (defined either by its content or its dimensions), the background will not be seen. If an element contains only floated children which haven’t been cleared—see clear—then again the background won’t show, as the element’s height will be zero.

Value

color takes any valid CSS color value or color keyword.

The keyword transparent sets the background-color to be transparent. This value ensures that the content of any element that’s beneath the current element will be visible through the transparent background. The default for background-color is transparent, so there is no need to specify this value unless you’re overwriting previous definitions.

Compatibility

IE5.5Buggy
6.0Buggy
7.0Buggy
Firefox1.0Full
1.5Full
2.0Full
Safari1.3Full
2.0Full
3.0Full
Opera9.2Full
9.5Full

Internet Explorer for Windows versions up to and including 7 will only apply the background from inside the border’s edge when the element in question has a layout. If the element does not have a layout, the background-color or background-image will slide under the borders as per the specifications.

Internet Explorer for Windows versions up to and including 7 don’t support the value inherit.

Related Reading

User-contributed notes

There are no comments yet.

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters