Setting color via rgb in CSS
At the very beginning of the tutorial, when we were studying colors, I told you that a color can be specified either by an English word, or by rgb, or by #. The last two formats allow you to get a color of any shade. Let's figure out how exactly these formats work.
To understand these methods, we first need to understand how the desired color is obtained on the computer screen.
In fact, a single screen point (pixel) cannot glow with all the colors needed, as this would be technically very difficult. Each screen point can glow with only three colors: red, green and blue. But at the same time and in different proportions.
By combining these colors we can get any color we need, just like artists do with paints: by mixing several colors they get another new one.
To mix colors in CSS, you need to specify the keyword rgb as the property value, followed by a comma-separated list specifying the proportions in which these three basic colors should be taken. The colors themselves can vary from 0 to 255. Moreover, zero is the absence of color, and 255 is a pure color (for example, pure red).
The letters rgb themselves stand for red, green, blue.
Let's look at some examples.
Example
Let's mix pure red and pure blue. To do this, set the first value to 255, the second to 0, and the third to 255. We'll get purple:
<p>
text word version document book
</p>
p {
color: rgb(255, 0, 255);
}
:
Example
Let's now set the first value to 255 and all the others to zero. The result will be pure red:
<p>
text word version document book
</p>
p {
color: rgb(255, 0, 0);
}
:
Example
This is how we get pure green color:
<p>
text word version document book
</p>
p {
color: rgb(0, 255, 0);
}
:
Example
And if for green you set not 255, but 100, then you get partially green:
<p>
text word version document book
</p>
p {
color: rgb(0, 100, 0);
}
:
Example
Mix all three colors in arbitrary proportions. See what you get:
<p>
text word version document book
</p>
p {
color: rgb(200, 100, 125);
}
:
Example
If you put all the values in 255, you get pure white:
p {
color: rgb(255, 255, 255);
}
Example
If you put all the values in 0, you get pure black:
p {
color: rgb(0, 0, 0);
}
Practical tasks
Set the color via rgb to make all paragraphs red.
By setting the color to rgb make all h2 green.
By setting the color to rgb make all h3 blue.
Mix pure red and pure green. What color do you get?