DEV Community

Cover image for A guide to CSS Color functions (rgb(), hsl(), rgba(), hsla()) in 2023
Rahul
Rahul

Posted on • Originally published at rahul.biz

A guide to CSS Color functions (rgb(), hsl(), rgba(), hsla()) in 2023

Hey, there fellow developers! Today we're going to dive into the world of CSS color functions.

You might have used CSS to change the color of an element on a web page, but have you ever heard of CSS color functions? If not, tighten your seat belts because you're about to learn something new and super useful!

Let's start with a Quote I read yesterday on GoodReads.

Color is the keyboard, the eyes are the harmonies, the soul is the piano with many strings. The artist is the hand that plays, touching one key or another, to cause vibrations in the soul.

What exactly are CSS Color functions?

CSS color functions are a way to specify colors in CSS using mathematical functions rather than just a simple color code. The functions provide more control and flexibility over the colors being used in your stylesheet.

With color functions, you can adjust the hue, saturation, lightness, and opacity of a color, and even mix two or more colors together.

There are multiple CSS Functions available, let's look at some:

  1. rgb() - It takes three values, representing red, green, and blue respectively, and returns a color. The values can range from 0 to 255. Example: color: rgb(255, 0, 0) results in a red color.

  2. rgba() - It is similar to rgb(), but it also allows you to set the opacity of the color. The fourth value, alpha, can range from 0 to 1. Example: color: rgba(255, 0, 0, 0.5) results in a semi-transparent red color.

  3. hsl() - It takes three values, representing hue, saturation, and lightness, and returns a color. Example: color: hsl(0, 100%, 50%) results in a red color.

  4. hsla() - It is similar to hsl(), but it also allows you to set the opacity of the color. Example: color: hsla(0, 100%, 50%, 0.5) results in a semi-transparent red color.

  5. mix() - It allows you to mix two colors together, with an optional weight parameter. Example: color: mix(red, blue) results in a shade of purple.

Le't learn about them in detail.


RGB()

Alright, let's continue our journey into the world of CSS color functions and dive into the RGB function. The RGB function is one of the most commonly used color functions in CSS, and for good reason! It's easy to understand and gives you a lot of control over the colors you use on your website.

The RGB function takes three values, each representing the intensity of

  • red,
  • green, and
  • blue respectively.

These values can range from 0 to 255. By mixing different intensities of red, green, and blue, you can create any color you like.

Example:

  • For red color - rgb(255, 0, 0)
  • For green color - rgb(0, 255, 0)
  • For blue color - rgb(0, 0, 255)
div {
  background-color: rgb(0, 255, 0);
}
/*this sets all th div element with green color*/
Enter fullscreen mode Exit fullscreen mode

HSL()

Alrighty, let's move on to another useful color function in CSS - the HSL function!

The HSL function is similar to the RGB function, but instead of using values for red, green, and blue, it uses values for

  • hue,
  • saturation, and
  • lightness. This makes it a little easier to understand and use, especially for those who are new to color theory.

The hue value in the HSL function represents the color itself, with values ranging from 0 to 360.

  • The hue value of 0 represents red, a hue value of 120 represents green, and a hue value of 240 represents blue.

  • The saturation value in the HSL function represents the intensity of the color, with values ranging from 0% to 100%. A saturation value of 100% means the color is fully saturated, while a value of 0% means the color is gray.

  • The lightness value in the HSL function represents the brightness of the color, with values ranging from 0% to 100%. A lightness value of 50% means the color is a neutral gray, while a value of 100% means the color is fully light, and a value of 0% means the color is fully dark.

div {
  background-color: hsl(120, 100%, 50%);
}

/* All `<div>` elements to green, fully saturated, and with a neutral lightness. */
Enter fullscreen mode Exit fullscreen mode

RGBA()

Alright folks, let's talk about the RGBA function!

The RGBA function is just like the RGB function, but with one added bonus:

  • it allows you to specify the opacity of a color.

This can come in handy when you want to create a translucent effect for your elements, such as when you want a background color to be partially transparent.

The RGBA function takes four values, the first three being the red, green, and blue values just like the RGB function.

The fourth value is the alpha value, which represents the opacity of the color and ranges from 0 to 1.

A value of 0 means the color is fully transparent, while a value of 1 means the color is fully opaque.

div {
  color: rgba(0, 0, 255, 0.75);
}
/* all `<div>` elements to blue with an opacity of 75%. */
Enter fullscreen mode Exit fullscreen mode

HSLA()

Alright folks, let's talk about the HSLA function!

The HSLA function is just like the HSL function, but with one added bonus: it allows you to specify the opacity of a color.

This can come in handy when you want to create a translucent effect for your elements, such as when you want a background color to be partially transparent.

The HSLA function takes four values, the first three being the hue, saturation, and lightness values just like the HSL function.

The fourth value is the alpha value, which represents the opacity of the color and ranges from 0 to 1. A value of 0 means the color is fully transparent, while a value of 1 means the color is fully opaque.

div {
  color: hsla(240, 100%, 50%, 0.75);
}
/* all `<div>` elements to blue with an opacity of 75%. */

Enter fullscreen mode Exit fullscreen mode

It's all same A means Alpha that is us (alpha value of 0.75)ed for the translucent effect in both the function, simple.


Custom Properties or CSS Variables

Now, let's talk about custom properties in CSS, also known as CSS variables. Custom properties allow you to store values that you can reuse throughout your stylesheet, making it easier to maintain your styles and making your code more modular and flexible.

To create a custom property, you simply use the -- syntax followed by the property name, followed by a value

:root {
  --primary-color: blue;
}
Enter fullscreen mode Exit fullscreen mode

Here we've created a custom property called --primary-color with the value of blue.

Now, to use this custom property, you can use the var() function in your CSS selectors.

button {
  background-color: var(--primary-color);
}
Enter fullscreen mode Exit fullscreen mode

The above code sets the background color of all <button> elements to blue, because we're using the custom property --primary-color.

If we want to change the value of --primary-color, we only need to change it in one place, and it will automatically update throughout the stylesheet.

Advantages of using custom properties include:

  • Code reuse: Instead of repeating values throughout your stylesheet, you can store values in custom properties and reuse them as needed.
  • Maintainability: Custom properties make it easier to maintain your styles, because you only need to update values in one place.
  • Flexibility: Custom properties allow you to change the appearance of your site by changing values in one place, instead of making changes in multiple selectors.

CSS Color functions Best Practices

Alright, let's talk about best practices for using CSS color functions in your projects. These tips will help you create a consistent and visually appealing color palette for your website, as well as make sure that your colors are readable and accessible for all users.

Creating a Color Palette for Your Website:

One of the first things you should do when working with CSS colors is to create a color palette for your website. This can be as simple as picking a few colors that you like and that work well together.

You can use RGB, HSL, RGBA, or HSLA functions to define your colors.

A good place to start is to choose a main color, and then pick 2-3 accent colors to complement it.

:root {
  --primary-color: hsl(180, 100%, 50%);
  --secondary-color: hsl(120, 100%, 50%);
  --tertiary-color: hsl(60, 100%, 50%);
}
Enter fullscreen mode Exit fullscreen mode

Using CSS Color Functions for Contrast and Hierarchy:

It's important to use color to create contrast and hierarchy in your designs. For example, you can use a lighter color for your background and a darker color for your text to make sure it's easily readable.

You can also use color to draw attention to specific elements, such as buttons or links.

body {
  background-color: var(--secondary-color);
  color: var(--primary-color);
}

a {
  color: var(--tertiary-color);
}
Enter fullscreen mode Exit fullscreen mode

Testing Color Functions for Readability and Accessibility:

Finally, it's important to test your colors for readability and accessibility. Make sure your colors have enough contrast to be easily readable, especially for users with color vision deficiencies.

You can use online tools to check the contrast of your colors and make sure they meet accessibility standards.


CSS Color Functions advanced techniques

In this section, we'll be diving into some advanced techniques with CSS color functions.

Using CSS Variables for Dynamic Color Schemes

Have you ever wanted to switch up your website's color scheme with just a few lines of code? With CSS variables, also known as custom properties, you can! You can create a set of variables to store your color palette and then use them throughout your stylesheet.

This way, if you ever want to change your color scheme, you only have to update the values in your variables.

:root {
  --primary-color: #00b0ff;
  --secondary-color: #00cc99;
}

h1 {
  color: var(--primary-color);
}

button {
  background-color: var(--secondary-color);
}
Enter fullscreen mode Exit fullscreen mode

In this example, we created two custom properties for our primary and secondary colors. We then used them to style our h1 and button elements.

Creating Animations with CSS Color Functions

One of the cool things you can do with CSS color functions is animate them! You can create smooth transitions between colors to add some dynamism to your website. For example, you could make a button change color when a user hovers over it.

button {
  background-color: hsl(120, 100%, 50%);
  transition: background-color 0.5s ease;
}

button:hover {
  background-color: hsl(240, 100%, 50%);
}
Enter fullscreen mode Exit fullscreen mode

Here, we set the default background color of our button to a green hue (hsl(120, 100%, 50%)) and added a transition to make the color change smoothly when the button is hovered over. We changed the color to a blue hue (hsl(240, 100%, 50%)) for the hover state.

Using CSS Color Functions for Gradients and Transparency

Another fun thing you can do with CSS color functions is create gradients and transparency. You can use the RGBA and HSLA color functions to create semi-transparent colors, and then combine them to create gradients.

.gradient {
  background: linear-gradient(to right, rgba(255, 0, 0, 0.5), hsla(120, 100%, 50%, 0.5));
}

Enter fullscreen mode Exit fullscreen mode

Here, we created a linear gradient that goes from a red RGBA color to a green HSLA color. Both colors have an alpha value of 0.5, making them semi-transparent.

And there you have it!

Some advanced techniques with CSS color functions. Have fun experimenting with these techniques and let your creativity run wild!

Resources


Conclusion

In conclusion, I hope this intro has been helpful in getting you started with CSS color functions and custom properties. These are powerful tools that can make your CSS code more flexible, maintainable, and easier to work with. Happy coding!

Top comments (0)