Home 〉 CSS Tutorials 〉 CSS Colors Explained – HEX, RGB, RGBA, HSL, HSLA with Examples
CSS Colors Explained - HEX, RGB, RGBA, HSL, HSLA with Examples !
Want to know about how to apply colors in CSS?
This tutorial covers various color formats in CSS, including HEX codes, RGB, RGBA, HSL, and HSLA. Understand the differences between these formats and learn how to use them to style your web elements effectively.
Color is one of the most important aspects of web design. In CSS, you can apply colors using several different formats. In this guide, we’ll cover all major a CSS color types — step by step — with examples and meanings.
CSS supports over 140 named colors.
<!DOCTYPE html>
<html>
<head>
<style>
.color-name {
color: red;
}
</style>
</head>
<body>
<p class="color-name">This text is red using a color name.</p>
</body>
</html>
output 📌
This text is red using a color name.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Just type the color name directly. Easy to remember, but limited to predefined options.
HEX codes are written as '#RRGGBB'.
<!DOCTYPE html>
<html>
<head>
<style>
.hex-color {
color: #00ff00;
}
</style>
</head>
<body>
<p class="hex-color">This text is green using a HEX code.</p>
</body>
</html>
output 📌
This text is green using a HEX code.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
'#00ff00' means 0 red, 255 green, and 0 blue.
RGB stands for Red, Green, Blue.
<!DOCTYPE html>
<html>
<head>
<style>
.rgb-color {
color: rgb(0, 0, 255);
}
</style>
</head>
<body>
<p class="rgb-color">This text is blue using RGB.</p>
</body>
</html>
output 📌
This text is blue using RGB.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Each value ranges from 0 to 255. RGB gives you more control than named colors.
Same as RGB, but with an alpha (transparency) value.
<!DOCTYPE html>
<html>
<head>
<style>
.rgba-color {
background-color: rgba(255, 0, 0, 0.5);
}
</style>
</head>
<body>
<p class="rgba-color">This has a red background with 50% transparency.</p>
</body>
</html>
output 📌
This has a red background with 50% transparency.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
'a' stands for **alpha** — controls opacity (0 is transparent, 1 is fully opaque).
HSL stands for Hue, Saturation, Lightness.
<!DOCTYPE html>
<html>
<head>
<style>
.hsl-color {
color: hsl(120, 100%, 50%);
}
</style>
</head>
<body>
<p class="hsl-color">This text is green using HSL.</p>
</body>
</html>
output 📌
This text is green using HSL.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
HSL with alpha for transparency.
<!DOCTYPE html>
<html>
<head>
<style>
.hsla-color {
background-color: hsla(240, 100%, 50%, 0.3);
}
</style>
</head>
<body>
<p class="hsla-color">This has a transparent blue background using HSLA.</p>
</body>
</html>
output 📌
This has a transparent blue background using HSLA.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
HSLA is helpful when you want colors based on hue and control transparency too.
CSS offers many flexible ways to apply colors to your designs. Whether you use color names, HEX, RGB, or HSL, each method has its own use case.
HEX and RGB are the most commonly used formats in modern web design.
RGBA includes an alpha value for transparency, while RGB does not.
Yes, standard color names are supported in all major browsers.
Use RGBA or HSLA for transparency control.
Both are fine. HEX is shorter; RGB gives more flexibility in scripts and dynamic changes.