Open In App

CSS Colors

CSS Colors are an essential part of web design, providing the ability to bring your HTML elements to life. This feature allows developers to set the color of various HTML elements, including font color, background color, and more.

There are several ways to define the color of an element in CSS:

By understanding and utilizing these different methods, you can create vibrant and dynamic web pages that captivate your audience. Let’s dive in and explore the CSS Colors!

Built-In Color: These are a set of predefined colors which are used by its name. For example: red, blue, green etc.

Syntax:

h1 {
color: color-name;
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>CSS color property</title>
    <style>
        h1 {
            color: green;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
</body>

</html>

Output:

color property

RGB Format: The RGB(Red, Green, Blue) format is used to define the color of an HTML element by specifying the R, G, B values range between 0 to 255. For example: RGB value of Red color is (255, 0, 0), Green color is (0, 255, 0), Blue color is (0, 0, 255) etc.

Syntax:

h1 {
color: rgb(R, G, B);
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>CSS color property</title>
    <style>
        h1 {
            color: rgb(0, 153, 0);
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
</body>

</html>

Output:

color property

RGBA Format: The RGBA format is similar to the RGB, but the difference is RGBA contains A (Alpha) which specifies the transparency of elements. The value of alpha lies between 0.0 to 1.0 where 0.0. represents fully transparent and 1.0 represents not transparent.

Syntax:

h1 {
color:rgba(R, G, B, A);
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>CSS RGBA color property</title>
    <style>
        h1 {
            color: rgba(0, 153, 0, 0.5);
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
</body>

</html>

Output:

color property

Hexadecimal Notation: The hexadecimal notation begins with # symbol followed by 6 characters each ranging from 0 to F. For example: Red #FF0000, Green #00FF00, Blue #0000FF etc.

Syntax:

h1 {
color:#(0-F)(0-F)(0-F)(0-F)(0-F)(0-F);
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>CSS hex property</title>
    <style>
        h1 {
            color: #009900;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
</body>

</html>

Output:

color property

HSL: HSL stands for Hue, Saturation, and Lightness respectively. This format uses the cylindrical coordinate system.

Syntax:

h1 {
color:hsl(H, S, L);
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>CSS hsl color property</title>
    <style>
        h1 {
            color: hsl(120, 100%, 30%);
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
</body>

</html>

Output:

color property

HSLA:

The HSLA color property is similar to HSL property, but the difference is HSLA contains A (Alpha) which specifies the transparency of elements. The value of alpha lies between 0.0 to 1.0 where 0.0. represents fully transparent and 1.0 represents not transparent.

Syntax:

h1 {
color:hsla(H, S, L, A);
}

Example:

<!DOCTYPE html>
<html>

<head>
    <title>CSS hsla color property</title>
    <style>
        h1 {
            color: hsla(120, 100%, 50%, 0.50);
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
</body>

</html>

Output:

color property


Article Tags :