The background-color
property in CSS is used to set a solid color for the background of an HTML element. This color fills the entire content, padding, and border areas of the element, extending to the edge of the border. It's a simple yet powerful way to visually distinguish different sections of a webpage.
Example 1: Setting a Solid Color by Name
/* style.css */
.element-one {
/* Sets the background color to a predefined color name */
background-color: steelblue;
color: white; /* Sets the text color to white for better contrast */
padding: 20px;
}
Explanation
This code targets an element with the class .element-one
and changes its background to a light shade of blue called steelblue
. Color names are a straightforward way to apply colors in CSS.
Example 2: Using Hexadecimal Color Values
/* style.css */
.element-two {
/* Sets the background color using a hex code for a specific shade of green */
background-color: #2E8B57;
color: #FFFFFF; /* White text color using its hex code */
padding: 20px;
}
Explanation
Hexadecimal (hex) color codes offer a much wider range of colors than color names. The code #2E8B57
represents a specific shade of sea green, providing more precise design control.
Example 3: Applying RGB Color Values
/* style.css */
.element-three {
/* Sets the background color using the RGB color model */
background-color: rgb(218, 112, 214);
color: rgb(0, 0, 0); /* Black text color for readability */
padding: 20px;
}
Explanation
The rgb()
function defines a color by its red, green, and blue components, with each value ranging from 0 to 255. This example creates an orchid color by mixing 218 parts red, 112 parts green, and 214 parts blue.
Example 4: Using RGBA for Transparency
/* style.css */
.element-four {
/* Sets a semi-transparent red background color using RGBA */
background-color: rgba(255, 0, 0, 0.5);
color: black;
padding: 20px;
}
Explanation
The rgba()
function adds an alpha channel to rgb()
, allowing for transparency. The fourth value (alpha) ranges from 0 (fully transparent) to 1 (fully opaque); here, 0.5
creates a 50% transparent red background.
Example 5: HSL Color Values
/* style.css */
.element-five {
/* Sets the background using the HSL color model for a vibrant blue */
background-color: hsl(240, 100%, 50%);
color: white;
padding: 20px;
}
Explanation
HSL stands for Hue, Saturation, and Lightness. This is often more intuitive for making color adjustments; hsl(240, 100%, 50%)
defines a pure, fully saturated blue.
Example 6: HSLA for Hue, Saturation, Lightness, and Alpha
/* style.css */
.element-six {
/* Sets a semi-transparent yellow-green background using HSLA */
background-color: hsla(80, 61%, 50%, 0.7);
color: black;
padding: 20px;
}
Explanation
Similar to rgba()
, hsla()
adds an alpha channel for transparency to the HSL color model. This example creates a yellow-green background that is 70% opaque.
Example 7: The transparent
Keyword
/* style.css */
.element-seven {
/* The parent element has a background image */
background-image: url('https://via.placeholder.com/150/0000FF/808080');
}
.child-element {
/* Makes the child's background transparent to show the parent's background */
background-color: transparent;
color: white;
padding: 20px;
}
Explanation
The transparent
keyword makes the background of an element completely see-through. This is useful for child elements when you want the parent element's background to be visible.