The background-color property specifies the background color of an element.

Example

The background color of a page is set like this:


body {

  background-color: lightblue;

}


With CSS, a color is most often specified by:

  • a valid color name - like "red"
  • a HEX value - like "#ff0000"
  • an RGB value - like "rgb(255,0,0)"

Look at CSS Color Values for a complete list of possible color values.


Other Elements


You can set the background color for any HTML elements:

Example

Here, the <h1>, <p>, and <div> elements will have different background colors: 

h1 {

  background-color: green;

}


div {

  background-color: lightblue;

}


{

  background-color: yellow;

}


Opacity / Transparency

The opacity property specifies the opacity/transparency of an element. It can take a value from 0.0 - 1.0. The lower value, the more transparent:


Example

div {

 background-color: green;

  opacity: 0.3;

}


Note: When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read.


Transparency using RGBA

If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:


Example

div {

 background: rgba(0, 128, 0, 0.3) /* Green background with 30% opacity */

}