CSS Colors

Welcome to the CSS Colors tutorial on codeswithpankaj.com. In this tutorial, we will explore the various ways to specify colors in CSS, including color names, HEX values, RGB values, RGBA values, HSL values, and HSLA values.

Ways to Specify Colors in CSS

1. Color Names

CSS supports 140 standard color names. You can use these predefined names to specify colors directly.

Example:

h1 {
    color: blue;
}
p {
    color: red;
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS Color Names - codeswithpankaj.com</title>
    <style>
        h1 {
            color: blue;
        }
        p {
            color: red;
        }
    </style>
</head>
<body>
    <h1>This is a blue heading</h1>
    <p>This is a red paragraph.</p>
</body>
</html>

2. HEX Values

HEX values are a way to specify colors using hexadecimal values. A HEX value starts with a # followed by six hexadecimal digits.

Example:

h1 {
    color: #0000FF; /* Blue */
}
p {
    color: #FF0000; /* Red */
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS HEX Values - codeswithpankaj.com</title>
    <style>
        h1 {
            color: #0000FF;
        }
        p {
            color: #FF0000;
        }
    </style>
</head>
<body>
    <h1>This is a blue heading</h1>
    <p>This is a red paragraph.</p>
</body>
</html>

3. RGB Values

RGB values define colors using the Red-Green-Blue (RGB) color model. An RGB value is specified using the rgb() function, which takes three parameters: red, green, and blue.

Example:

h1 {
    color: rgb(0, 0, 255); /* Blue */
}
p {
    color: rgb(255, 0, 0); /* Red */
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS RGB Values - codeswithpankaj.com</title>
    <style>
        h1 {
            color: rgb(0, 0, 255);
        }
        p {
            color: rgb(255, 0, 0);
        }
    </style>
</head>
<body>
    <h1>This is a blue heading</h1>
    <p>This is a red paragraph.</p>
</body>
</html>

4. RGBA Values

RGBA values are an extension of RGB values that include an alpha channel to specify the opacity. The alpha parameter is a number between 0.0 (completely transparent) and 1.0 (completely opaque).

Example:

h1 {
    color: rgba(0, 0, 255, 0.5); /* Blue with 50% opacity */
}
p {
    color: rgba(255, 0, 0, 0.5); /* Red with 50% opacity */
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS RGBA Values - codeswithpankaj.com</title>
    <style>
        h1 {
            color: rgba(0, 0, 255, 0.5);
        }
        p {
            color: rgba(255, 0, 0, 0.5);
        }
    </style>
</head>
<body>
    <h1>This is a semi-transparent blue heading</h1>
    <p>This is a semi-transparent red paragraph.</p>
</body>
</html>

5. HSL Values

HSL stands for Hue, Saturation, and Lightness. An HSL value is specified using the hsl() function, which takes three parameters: hue (0-360), saturation (0%-100%), and lightness (0%-100%).

Example:

h1 {
    color: hsl(240, 100%, 50%); /* Blue */
}
p {
    color: hsl(0, 100%, 50%); /* Red */
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS HSL Values - codeswithpankaj.com</title>
    <style>
        h1 {
            color: hsl(240, 100%, 50%);
        }
        p {
            color: hsl(0, 100%, 50%);
        }
    </head>
<body>
    <h1>This is a blue heading</h1>
    <p>This is a red paragraph.</p>
</body>
</html>

6. HSLA Values

HSLA values are an extension of HSL values that include an alpha channel to specify the opacity. The alpha parameter is a number between 0.0 (completely transparent) and 1.0 (completely opaque).

Example:

h1 {
    color: hsla(240, 100%, 50%, 0.5); /* Blue with 50% opacity */
}
p {
    color: hsla(0, 100%, 50%, 0.5); /* Red with 50% opacity */
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS HSLA Values - codeswithpankaj.com</title>
    <style>
        h1 {
            color: hsla(240, 100%, 50%, 0.5);
        }
        p {
            color: hsla(0, 100%, 50%, 0.5);
        }
    </style>
</head>
<body>
    <h1>This is a semi-transparent blue heading</h1>
    <p>This is a semi-transparent red paragraph.</p>
</body>
</html>

Conclusion

CSS provides various ways to specify colors, allowing you to create visually appealing web pages. By understanding and using different color values like color names, HEX values, RGB, RGBA, HSL, and HSLA, you can have precise control over the color styling of your web pages. Stay tuned to codeswithpankaj.com for more tutorials and web development tips!

Last updated