Free CSS course. Sign Up for tracking progress →

CSS: RGB color model

During this course, we encountered properties whose values are colors. These properties included:

  • color
  • background-color
  • border-color

The hexadecimal RGB model, which stands for Red Green Blue, was used to represent colors. CSS uses two basic types of notation to represent colors in RGB:

  • Hexadecimal. This is a 6-character notation, divided into three blocks of two values. Each block is responsible for one of the colors: red, green or blue
  • The rgb(red, green, blue) function. The function accepts three numbers from 0 to 255, each of which determines the amount of red, green and blue

Hexadecimal notation

This one uses hexadecimal notation. The notation is divided into three blocks of two numbers each:

  1. from 00 to ff - amount of red color
  2. from 00 to ff - amount of green color
  3. from 00 to ff - quantity of blue color
/* White Color */
color: #ffffff;

/* Black Color */
color: #000000;

Suppose you want purple for the text. How is it made? To achieve this, red and blue are mixed. There is no green in purple. So, we need to input the maximum in the first and third block. The result is the color #ff00ff.

Purple text

You can get different variations of this color by adding or reducing the number of compound colors

The rgb function

The second way to specify a color using the RGB color model is to use the special function rgb(). It takes three numbers from 0 to 255, where the first number specifies the amount of red, the second the amount of green, and the third the amount of blue. Does that remind you of anything?

If you think it looks like a hexadecimal system, you are right - the idea is exactly the same. Only we write the colors in the numbers we are accustomed to. Otherwise, everything is the same, which means you can create purple using the rgb() function:

<p class="text">Purple text</p>
.text {
  color: rgb(255, 0, 255);
}
Purple text

And this is what white and black will look like when using rgb():

/* White Color */
color: rgb(255, 255, 255);

/* Black Color */
color: rgb(0, 0, 0);

Transparency and the rgba() function

When you use background colors, you often use not just a color on its own, you may also add transparency to it. The RGB color model uses the term "alpha" channel for this purpose. This defines how transparent color should be output and is specified by a number from 0 to 1, where 0 is completely transparent and 1 is completely opaque.

In order to be able to add an alpha channel, the rgba() function has been created, where a is alpha. Otherwise, everything is exactly the same as we learned earlier. Let's make a semi-transparent purple background:

<div class="background">A block with a semi-transparent purple background</div>
.background {
  background-color: rgba(255, 0, 255, 0.5);
}
Block with a semi-transparent purple background

Instructions

Create a paragraph with the class set to background-black-50 and give it a semi-transparent black background. Use the rgba() function. Write styles in the <style> tag.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

If you got stuck and don't know what to do, you can ask a question in our huge and friendly community