CSS Colors
Color in CSS can be written as one of more than 140 names, or as a HEX, RGB, RGBA, HSL or HSLA value, and there is also opacity.
RGBA Colors
Take an RGB color, add an alpha channel to control its opacity, and you have RGBA.
The notation is rgba(red, green, blue, alpha).
Alpha is a number running from 0.0 (fully transparent) up to 1.0 (fully opaque).
Example
Three paragraphs, each with its own RGBA background.
Output: complete HTML page
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RGBA Colors</title>
</head>
<body>
<h1>RGBA Colors</h1>
<p style="background-color:rgba(255,0,0,0.3);padding:20px;">rgba(255, 0, 0, 0.3) - red with opacity</p>
<p style="background-color:rgba(0,255,0,0.3);padding:20px;">rgba(0, 255, 0, 0.3) - green with opacity</p>
<p style="background-color:rgba(0,0,255,0.3);padding:20px;">rgba(0, 0, 255, 0.3) - blue with opacity</p>
</body>
</html>
RGBA syntax
rgba(red, green, blue, alpha)
RGBA opacity values
The red stays constant here while the alpha value changes.
rgba(255, 0, 0, 0.2);
rgba(255, 0, 0, 0.4);
rgba(255, 0, 0, 0.6);
rgba(255, 0, 0, 0.8);
HSLA Colors
HSLA follows the same idea starting from HSL: the extra alpha channel decides how opaque the color is.
Its notation is hsla(hue, saturation, lightness, alpha).
Once again, alpha goes from 0.0 (fully transparent) up to 1.0 (fully opaque).
Example
Four paragraphs, each with its own HSLA background.
Output: complete HTML page
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HSLA Colors</title>
</head>
<body>
<h1>HSLA Colors</h1>
<p style="background-color:hsla(120,100%,50%,0.3);padding:20px;">hsla(120, 100%, 50%, 0.3) - green with opacity</p>
<p style="background-color:hsla(120,100%,75%,0.3);padding:20px;">hsla(120, 100%, 75%, 0.3) - light green with opacity</p>
<p style="background-color:hsla(120,100%,25%,0.3);padding:20px;">hsla(120, 100%, 25%, 0.3) - dark green with opacity</p>
<p style="background-color:hsla(120,60%,70%,0.3);padding:20px;">hsla(120, 60%, 70%, 0.3) - pastel green with opacity</p>
</body>
</html>
HSLA syntax
hsla(hue, saturation, lightness, alpha)
HSLA opacity values
The red hue and the 0.3 opacity stay constant here while the lightness changes.
hsla(0, 100%, 30%, 0.3);
hsla(0, 100%, 50%, 0.3);
hsla(0, 100%, 70%, 0.3);
hsla(0, 100%, 90%, 0.3);
CSS opacity Property
Rather than affecting one color, the opacity property applies to the whole element, so its background color and its text turn transparent together.
Its value has to be a number from 0.0 (fully transparent) to 1.0 (fully opaque).
Note: Any text in the element takes on the same transparency or opacity.
Example
All three paragraphs combine an RGB background color with an opacity of 0.6.
Output: complete HTML page
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Opacity Property</title>
</head>
<body>
<h1>CSS opacity Property</h1>
<p style="background-color:rgb(255,0,0);opacity:0.6;padding:20px;">rgb(255, 0, 0) with opacity 0.6 - red with opacity</p>
<p style="background-color:rgb(0,255,0);opacity:0.6;padding:20px;">rgb(0, 255, 0) with opacity 0.6 - green with opacity</p>
<p style="background-color:rgb(0,0,255);opacity:0.6;padding:20px;">rgb(0, 0, 255) with opacity 0.6 - blue with opacity</p>
<p>Notice that the text inside the elements is also transparent!</p>
</body>
</html>