Colors can be represented in a few ways in CSS. You can use the named color, like "red". You can declare the color with a hexadecimal code, like #ff0000. Alternatively, you can declare the color with the red, green, blue values (RGB) like rgb(255, 0, 0) or you can declare the color using the hue, saturation, and lightness values (HSL) like hsl(0, 100%, 50%). All of these will render the color "red."
So why use one over the other? Well, there are only 140 named colors. That's pretty limiting. Hexadecimal values only provide for base 16 values which is much greater, and will work in most instances, but what if you want an even more nuanced color?
Using RGB or HSL will give you that option. Both also have the capacity to set the opacity value as well which gives your code even more flexibility.
/* This sets the background color of the body element using a named color */
body {
background-color: red;
}
/* This sets the background color of the body element using hexadecimal values */
body {
background-color: #ff0000;
}
/* This sets the background color of the body element using rgb values */
body {
background-color: rgb(255, 0, 0);
}
/* This sets the background color of the body element using hsl values */
body {
background-color: hsl(0, 100%, 50%);
}
And if you want to use opacity, or otherwise known as setting the alpha level, hence the "a" at the end, the notation would look like the following for rgb and hsl values.
/* This sets the background color of the body element using rgba values where the alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all) */
body {
background-color: rgba(255, 0, 0, 0.75);
}
/* This sets the background color of the body element using hsla values where the alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all) */
body {
background-color: hsla(0, 100%, 50%, 0.75);
}