CSS Colors

Welcome to the vibrant and essential lesson on CSS Colors. Here, we'll explore different ways to apply, manipulate, and optimize colors in your web designs.

Colors are a fundamental aspect of web aesthetics, conveying emotions, drawing attention, and setting the tone for your website. 

In this lesson, you'll learn about the various methods to define colors in CSS, from basic color names to advanced color functions. Dive deep into the world of RGB, HEX, HSL, and beyond. We'll also touch upon the importance of color accessibility to ensure that your designs are inclusive for all users.

Desired Outcomes

  • Understand the importance of color in web design.
  • Differentiate and use various color systems: RGB, HEX, HSL, and named colors.
  • Apply colors to elements effectively using CSS properties.
  • Manipulate colors using CSS functions like rgba().

Named Colors

CSS has a predefined set of named colors. Some of the values can be: red, blue, yellow, green etc. For beginners, these are the easiest to remember and use.

Example:

p {
    color: red;
}

This styles all <p> elements with red text.

HEX Values

Hexadecimal (HEX) color values are based on the combination of red, green, and blue (RGB) values in a base-16 format. A HEX color code starts with a hash # followed by 6 characters - two for red, two for green, and two for blue.

Example:

div {
    background-color: #FF5733; /* A shade of orange */
}
HEX Colors
HEX Colors

HEX is one of the most common methods to define colors in web design. It’s ideal for situations where opacity changes aren't necessary. Short and easily copy-pasteable, especially with shorthand versions (e.g., #FFF for white)

RGB Values

Colors can be defined using the rgb() function, where values for red, green, and blue are provided in the range of 0-255.

Example:

div {
    background-color: rgb(0, 0, 0); /* This is black */
}
RGB Colors
RGB Colors

Using rgb() directly relates to the red, green, and blue light components, making it more intuitive for some. Often used in software that deals with screen displays and images.

RGBA Values

RGBA is similar to RGB but has an additional value for alpha (A) representing opacity. The alpha value is between 0 (completely transparent) and 1 (fully opaque).

Example:

div {
    background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent black */
}
RGBA Colors
RGBA Colors

In this example we are specifying the red, green and blue channels to zero, while adding a 50% opacity, hence we get a partly transparent background.

Use when you need to specify the opacity of a color. Useful for hover effects, modal backgrounds, or any overlaying component where you want the background to be partly visible.

HSL Values

HSL stands for Hue, Saturation, and Lightness. It offers a more intuitive way to pick colors.

Example:

button {
    background-color: hsl(120, 100%, 50%); /* Pure green */
}
HSL Colors
HSL Colors

Allows for easier theme changes. Altering the lightness or saturation while keeping the hue can give a different feel to a design while remaining consistent. Easier to understand in terms of human perception. Adjusting saturation and lightness can be more intuitive than tweaking RGB values.

HSLA Values

Like RGBA, HSLA includes an alpha value for opacity.

button {
    background-color: hsla(120, 100%, 50%, 0.5); /* Pure green */
}
HSLA Colors
HSLA Colors

Combines the benefits of HSL with the transparency feature, offering both themability and opacity controls. Useful when you need a deeper level of control over both color and transparency, especially in layered designs.