What You'll Learn
You'll learn how to apply background colors using multiple color formats and control transparency for layered, semi-transparent effects.
The Core Idea
The background-color property paints the background of any element's content and padding area. You already know several ways to define colors—now you'll apply them specifically to backgrounds and master transparency for creating overlapping, see-through designs.
Think of it like colored glass: A solid background is like painted wood, but transparent backgrounds are like tinted windows—you can see through them to what's behind.
How Background Color Works
You can use any color format you've learned:
.box1 {
background-color: coral; /* Named color */
}
.box2 {
background-color: #3498db; /* Hexadecimal */
}
.box3 {
background-color: rgb(231, 76, 60); /* RGB */
}
.box4 {
background-color: hsl(120, 100%, 50%); /* HSL */
}
Adding Transparency
For semi-transparent backgrounds, use alpha channels with rgba() or hsla():
.overlay {
background-color: rgba(0, 0, 0, 0.5); /* 50% transparent black */
}
.highlight {
background-color: hsla(45, 100%, 50%, 0.3); /* 30% opaque yellow */
}
The alpha value ranges from 0 (completely transparent) to 1 (completely opaque). Values like 0.5 mean 50% see-through.
Transparency vs Opacity
While background-color with alpha only affects the background, the separate opacity property affects the entire element and all its children—text, borders, everything.
.semi-transparent-bg {
background-color: rgba(255, 0, 0, 0.5); /* Only background transparent */
}
.entire-element {
opacity: 0.5; /* Everything inside becomes transparent */
}
Key Takeaway: Use background-color with rgba() or hsla() for transparent backgrounds that let underlying content show through, while keeping text and other content fully visible.