CSS Colors
CSS colors are used to set the color of text, backgrounds, borders, and other elements. Colors make websites visually appealing and improve readability.
What are CSS Colors?
CSS allows you to define colors in multiple ways, including color names, HEX codes, RGB, RGBA, HSL, and HSLA. Choosing the right color enhances user experience.
1. Named Colors
CSS provides predefined color names that you can use directly.
Example 1: Named Colors
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Colors</title>
<style>
p {
color: red;
font-size: 18px;
}
</style>
</head>
<body>
<p>This text uses a named color.</p>
</body>
</html>
Output
• Uses a predefined CSS color name
• Easy to remember and use
2. HEX Colors
HEX colors are defined using a hash (#) followed by six hexadecimal digits representing red, green, and blue.
Example 2: HEX Color
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p {
color: #1e90ff;
}
</style>
</head>
<body>
<p>This text uses a HEX color.</p>
</body>
</html>
Output
• #1e90ff represents a specific shade of blue
• Offers precise color control
3. RGB and RGBA Colors
RGB defines colors using red, green, and blue values. RGBA adds an alpha value for transparency.
Example 3: RGB and RGBA
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p {
color: rgb(255, 99, 71);
background-color: rgba(255, 99, 71, 0.2);
}
</style>
</head>
<body>
<p>This text uses RGB and RGBA colors.</p>
</body>
</html>
Output
• RGB values range from 0 to 255
• RGBA adds transparency (0 = fully transparent, 1 = fully opaque)
4. HSL and HSLA Colors
HSL uses hue, saturation, and lightness to define colors. HSLA adds an alpha channel for transparency.
Example 4: HSL Color
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p {
color: hsl(120, 60%, 50%);
background-color: hsla(120, 60%, 50%, 0.3);
}
</style>
</head>
<body>
<p>This text uses HSL and HSLA colors.</p>
</body>
</html>
Output
• Hue = color type (0–360)
• Saturation = color intensity
• Lightness = brightness
• Alpha controls transparency
Important Notes
✔ Use RGBA or HSLA for transparency
✔ Named colors are easiest for beginners
✔ HEX, RGB, and HSL offer precise control