What is the css code for background color?


The CSS background properties are used to add background effects for elements.


In these chapters, you will learn about the following CSS background properties:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
  • background (shorthand property)


CSS background-color

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;
}

Try it Yourself »

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 <h2>, <p>, and <div> elements will have different background colors: 

h2 {
  background-color: green;
}

div {
  background-color: lightblue;
}

p {
  background-color: yellow;
}

Try it Yourself »



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:

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:

You learned from our CSS Colors Chapter, that you can use RGB as a color value. In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color.

An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.

Example

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

Try it Yourself »


The CSS Background Color Property

PropertyDescription
background-color Sets the background color of an element



Example

Set the background color for a page:

body {background-color: coral;}

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

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

The background of an element is the total size of the element, including padding and border (but not the margin).

Tip: Use a background color and a text color that makes the text easy to read.

Show demo ❯

Default value:transparent
Inherited:no
Animatable:yes. Read about animatable Try it
Version:CSS1
JavaScript syntax: object.style.backgroundColor="#00FF00" Try it


Browser Support

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

Property
background-color 1.0 4.0 1.0 1.0 3.5



CSS Syntax

background-color: color|transparent|initial|inherit;

Property Values

ValueDescriptionDemo
color Specifies the background color. Look at CSS Color Values for a complete list of possible color values. Demo ❯
transparent Specifies that the background color should be transparent. This is default Demo ❯
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit


More Examples

Example

Specify the background color with a HEX value:

body {background-color: #92a8d1;}

Try it Yourself »

Example

Specify the background color with an RGB value:

body {background-color: rgb(201, 76, 76);}

Try it Yourself »

Example

Specify the background color with an RGBA value:

body {background-color: rgba(201, 76, 76, 0.3);}

Try it Yourself »

Example

Specify the background color with a HSL value:

body {background-color: hsl(89, 43%, 51%);}

Try it Yourself »

Example

Specify the background color with a HSLA value:

body {background-color: hsla(89, 43%, 51%, 0.3);}

Try it Yourself »

Example

Set background colors for different elements:

body {
  background-color: #fefbd8;
}

h2 {
  background-color: #80ced6;
}

div {
  background-color: #d5f4e6;
}

span {
  background-color: #f18973;
}

Try it Yourself »


CSS tutorial: CSS Background

HTML DOM reference: backgroundColor property



How do you get a full background color in CSS?

The CSS background properties are used to add background effects for elements..
a valid color name - like "red".
a HEX value - like "#ff0000".
an RGB value - like "rgb(255,0,0)".

What is HTML code for background color?

HTML <body> bgcolor Attribute color_name: It specifies the name of the Background color of the Document. hex_number: It specifies the hex code of the Background color in the Document. rgb_number: It specifies the rgb value of the Background color in the Document.

What are the colors in CSS?

Basic Colors.