The color property of CSS helps to set the color of the HTML element(s).
This helps to set the foreground color of text, text decorations, and borders.
Syntax:
/* Syntax
selector {
color: value
}
*/
selector {
/* colorname can be any colour, such as red, blue, yellow, purple, green, etc. */
color: colorname
}
Note: In CSS we use color, not colour.
In CSS we have these methods to add colours:-
1. Pre-defined Name
2. Hexadecimal notation
3. RGB
4. RGBA
5. HSL
6. HSLA
The first method of defining the colour in the CSS is directly writing the particular colour name.
Example:
<head>
<style>
p{
color: purple;
}
</style>
</head>
<body>
<p>Hello World</p>
<p>CodeWithHarry</p>
</body>
</html>
Output:
There are many ways to set the property-value of color, with some of the most common listed below:
The hex code consists of a hash(#) symbol followed by six characters. These six characters are arranged into a set of three pairs (RR, GG, and BB).
Each character pair defines the intensity level of the colour, where R stands for red, G stands for green, and B stands for blue.
The intensity value lies between 00 (no intensity) and ff (maximum intensity).
Breaking the Character Set (RRGGBB):
Syntax:
seletor {
color: [[RRGGBB]];
}
Example:
<head>
<style>
h1 {
color: [[FF0000]];
/*Pure Red*/
}
h2 {
color: [[00FF00]];
/* Pure Green */
}
h3 {
color: [[0000FF]];
/* Pure Blue */
}
h4 {
color: [[b700ff]];
/* Custom Color */
}
</style>
</head>
<body>
<h1>CodeWithHarry</h1>
<h2>A Developer</h2>
<h3>Founder CodeWithHarry.com</h3>
<h4>Hello World</h4>
</body>
</html>
Output:
RGB stands for “Red, Green, Blue” and
Syntax:
selector{
color: rgb(red, green, blue);
}
Example:
<head>
<style>
h1 {
color: rgb(0, 0, 0);
/* red:0, green:0, blue:0 */
}
h2 {
color: rgb(255, 255, 255);
/* red:255, green:255, blue:255 */
}
h3 {
color: rgb(30, 150, 220);
}
</style>
</head>
<body>
<h1>CodeWithHarry</h1>
<h2>A Developer</h2>
<h3>CodeWithHarry.com founder</h3>
</body>
</html>
Output:
Similar to RGB, in RGBA, A stands for alpha value, which defines the opacity of the color.
Syntax:
selector{
color: rgb(red, green, blue, opacity);
}
Example:
<head>
<style>
h1 {
color: rgba(0, 0, 0, 0.8);
/* red:0, green:0, blue:0, Alpha: 0.8 = 80% */
}
h2 {
color: rgb(255, 255, 255, 0.6);
/* red:255, green:255, blue:255 */
}
h3 {
color: rgba(30 150 220 / 60% );
/* red:30, green:150, blue:200, alpha:60% */
}
</style>
</head>
<body>
<h1>CodeWithHarry</h1>
<h2>A Developer</h2>
<h3>CodeWithHarry.com founder</h3>
</body>
</html>
Output:
HSL stands for hue, saturation, and lightness. This is another way to set colour properties.
Breaking each keyword:
Syntax:
selector{
color: hsl(hue, saturation, lightness);
}
Example:
<head>
<style>
h1 {
color: hsl(235, 100%, 50%);
}
p {
color: hsl(0, 0%, 0%);
}
</style>
</head>
<body>
<h1>CodeWithHarry</h1>
<p>A Developer</p>
</body>
</html>
Output:
HSLA is similar to HSL; here, A stands for alpha value, which is used to set the opacity.
Syntax:
selector{
color: hsla(hue, saturation, lightness, alpha);
}
Example:
<head>
<style>
h1 {
color: hsla(235, 100%, 50%, 0.7);
}
p {
color: hsl(0, 0%, 0%, 0.4); /* a for alpha */
}
</style>
</head>
<body>
<h1>CodeWithHarry</h1>
<p>A Developer</p>
</body>
</html>
Output: