Previous Next

CSS Colors

CSS Color property is employed to define the color of HTML elements. This property is employed to define the font color, background color, and border color.

The color of an element can be defined in the following ways:

Predefined Color

CSS contains a set of predefined colors that you can utilize to define colors in your style sheets.

Syntax:

h1 {
    color: color-name;
}

Example:

HTML

<!DOCTYPE html>
<html>

<head>
    <title> CSS Colors </title>
</head>

<body>
   <h1> Tutorials4Coding </h1>
   <p> A code learning platform </p>
</body>
</html>

CSS

h1 {
    color: blue;
}

p {
    color: red;
}

Output

Predefined Color

RGB Format

RGB stands for Red, Green, Blue.

Syntax:

h1 {
    color: rgb(red, green, blue);
}

Example:

HTML

<!DOCTYPE html>
<html>

<head>
    <title> CSS Colors </title>
</head>

<body>
   <h1> Tutorials4Coding </h1>
   <h3> A code learning platform </h3>
   <p> Learn Coding Easily! </p>
</body>
</html>

CSS

h1 {
    color: rgb(255, 38, 0);
}

h3 {
    color: rgb(77, 238, 3);
}

p {
    color: rgb(5, 2, 204);
}

Output

RGB Format

RGBA Format

RGBA is the same as RGB, only it contains an additional opacity (alpha) value that ranges from 0 to 1.

Syntax:

h1 {
    color: rgba(red, green, blue, alpha);
}

Example:

HTML

<!DOCTYPE html>
<html>

<head>
    <title> CSS Colors </title>
</head>

<body>
   <h1> Tutorials4Coding </h1>
   <p> Learn Coding Easily! </p>
</body>
</html>

CSS

h1 {
    color: rgba(4, 181, 235, 0.5);
}

p {
    color: rgb(5, 2, 204, 0.6);
}

Output

RGBA Format

Hexadecimal Notation

The hexadecimal representation consists of a hash (#) character and six characters.

Syntax:

h1 {
    color: #rrggbb;
}

Example:

HTML

<!DOCTYPE html>
<html>

<head>
    <title> CSS Colors </title>
</head>

<body>
   <h1> Tutorials4Coding </h1>
   <p> Learn Coding Easily! </p>
</body>
</html>

CSS

h1 {
    color: #07eeff;
}

p {
    color: #234EDD;
}

Output

Hexadecimal Notation

HSL

HSL represents hue, saturation, and lightness. It is another means of specifying color.

Syntax:

h1 {
    color: hsl(hue, saturation, lightness);
}

Example:

HTML

<!DOCTYPE html>
<html>

<head>
    <title> CSS Colors </title>
</head>

<body>
   <h1> Tutorials4Coding </h1>
   <p> Learn Coding Easily! </p>
</body>
</html>

CSS

h1 {
    color: hsl(0, 100%, 50%);
}

p {
    color: hsl(240, 100%, 50%);
}

Output

HSL

HSLA

HSLA is the same as HSL, only it includes an additional opacity (alpha) value that ranges from 0 to 1.

Syntax:

 h1 {
    color: hsla(hue, saturation, lightness, alpha);
}

Example:

HTML

<!DOCTYPE html>
<html>

<head>
    <title> CSS Colors </title>
</head>

<body>
   <h1> Tutorials4Coding </h1>
   <p> Learn Coding Easily! </p>
</body>
</html>

CSS

h1 {
    color: hsla(9, 100%, 64%, 0.8);
}

p {
    color: hsla(240, 100%, 50%, 1);
}

Output

HSLA
Previous Next