Colors in CSS (HEX, RGB, HSL)

Colors are an essential part of web design. They help improve visual appearance, readability, and user experience. In CSS, colors can be defined using different formats such as HEX, RGB, and HSL.

HEX Colors

HEX stands for hexadecimal and is one of the most commonly used color formats in CSS. A HEX color code starts with a hash symbol followed by six characters. These characters represent red, green, and blue values.

Example of HEX color:

h1 {
color: #ff5733;
}

In this example, ff represents red, 57 represents green, and 33 represents blue. HEX values range from 00 to ff.

Short HEX codes can also be used when pairs are the same. For example, #fff represents white.

RGB Colors

RGB stands for Red, Green, and Blue. Each value ranges from 0 to 255, allowing you to mix different intensities of these three colors.

Example of RGB color:

p {
color: rgb(255, 87, 51);
}

This represents the same color as the HEX example above.

You can also use RGBA, where “A” stands for alpha, which controls transparency.

Example with transparency:

div {
background-color: rgba(255, 87, 51, 0.5);
}

The value 0.5 makes the color semi-transparent.

HSL Colors

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

Hue represents the color type and is measured in degrees from 0 to 360.
Saturation represents the intensity of the color and is given as a percentage.
Lightness represents how light or dark the color is and is also given as a percentage.

Example of HSL color:

h2 {
color: hsl(9, 100%, 60%);
}

You can also use HSLA to add transparency.

Example:

section {
background-color: hsla(9, 100%, 60%, 0.5);
}

When to Use Each Format

HEX is widely used for its simplicity and compatibility.
RGB is useful when working with transparency using RGBA.
HSL is helpful when you want to adjust color tones, brightness, or saturation easily.

Summary

HEX, RGB, and HSL are all different ways to define colors in CSS. Each format has its own advantages, and understanding all three helps you design better and more flexible web interfaces.

Home » CSS Fundamentals (Beginner) > Selectors & Properties > Colors (HEX, RGB, HSL