RGB and RGBA Functions
You'll learn how to define colors using numeric values for red, green, and blue channels, with an optional transparency layer.
What Are RGB and RGBA?
Instead of memorizing color names or hexadecimal codes, you can mix colors like paint using the rgb() and rgba() functions. RGB stands for Red, Green, Blue—the three primary colors of light. Each channel accepts a value from 0 to 255, where 0 means "none of that color" and 255 means "maximum intensity."
Think of it like three dimmer switches: one for red light, one for green, and one for blue. By adjusting each switch, you create any color imaginable.
Basic RGB Syntax
color: rgb(255, 0, 0); /* Pure red */
color: rgb(0, 255, 0); /* Pure green */
color: rgb(0, 0, 255); /* Pure blue */
color: rgb(255, 255, 0); /* Yellow (red + green) */
color: rgb(128, 128, 128); /* Medium gray */
Adding Transparency with RGBA
The rgba() function adds a fourth value: alpha (transparency). Alpha ranges from 0 (completely invisible) to 1 (completely opaque). You can use decimals like 0.5 for half-transparent.
background-color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */
background-color: rgba(0, 0, 0, 0.8); /* Mostly opaque black */
background-color: rgba(255, 255, 255, 0.1); /* Nearly invisible white */
Why Use RGB/RGBA?
- Precision: Exact numeric control over each color channel
- Transparency: RGBA lets backgrounds show through
- Dynamic: Easier to adjust programmatically than hex codes
Key Takeaway: RGB lets you mix colors using red, green, and blue values (0–255), while RGBA adds an alpha channel (0–1) for transparency control.