The property background-color defines the color of an element’s background.
Example
The page’s background color is configured in the following manner:
body { |
In CSS, colors are typically defined using:
Refer to CSS Color Values for a comprehensive range of potential color specifications.
You have the ability to define the background color for any HTML elements.
Example
In this context, the <h1>, <p>, and <div> elements will each possess distinct background colors.
h1 { |
The opacity property determines the transparency of an element, ranging from 0.0 to 1.0. Lower values correspond to higher transparency.
Example
div { |
Please be aware: When employing the opacity property to introduce transparency to an element’s background, all child elements inherit the same level of transparency. Consequently, this may result in difficulties in reading text within an element that is fully transparent.
|
To avoid applying opacity to child elements, as demonstrated in our example above, utilize RGBA color values.
The subsequent example demonstrates setting the opacity solely for the background color while leaving the text unaffected.
In our CSS Colors Chapter, you’ve discovered the use of RGB as a color value. In addition to RGB, RGBA color values include an alpha channel, specifying the color’s opacity.
An RGBA color value is structured as follows: rgba(red, green, blue, alpha). The alpha parameter ranges from 0.0 (fully transparent) to 1.0 (fully opaque).
Keep in mind: Further insights into RGBA Colors will be provided in our CSS Colors Chapter.
Example
div { |
Property |
Description |
background color |
Defines the background color of an element. |