CSS Color& Background Color Tutorial

G:hackHTMLmyCSS-Tutorial15- color�2.jpg

The “color” property is used to change the color of a text within an element.

We can set the value for this property in 4 ways.

  1. Using color name: currently there are 147 color names that we can
    use as the value of the “color” property.

Example:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         color:blue;
      }
      #red{
         color:red;
      }
      #green{
         color:green;
      }
      #yellow{
         color:yellow;
      }
  </style> 
</head>
   <body>
      <p id="blue">

         The color of this text is blue.

      </p>
      <p id="red">

         The color of this text is red.

      </p>
      <p id="green">

         The color of this text is green.

      </p>
      <p id="yellow">

         The color of this text is yellow.

      </p>
   </body>
</html>
  1. RGB values: RGB stands for Red Green Blue and using some value of
    each of these colors can result a new color.

Here’s the structure of RGB value:

selector{
         property-name:rgb(R, G, B);
      }
      selector{
         property-name:rgb(0-255,0-255,0-255);
      }

The value we can set for each color in RGB, ranges from 0 to 255. Changing the value for each color (Red, Green, and Blue) can increase or decrease the intensity of that color and the combination of three values result to a new color.

You can do some experiment here:

(Put RGB script right here so the user can work and test RGB colors)

For example, we can create the same purple color that we used in the example above using RGB value:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         color:rgb(4, 0, 255);
      }
      #red{
         color:rgb(255, 0, 0);
      }
      #green{
         color:rgb(0, 255, 55);
      }
      #yellow{
         color:rgb(255, 255, 0);
      }
  </style> 
</head>
   <body>
      <p id="blue">

         The color of this text is blue.

      </p>
      <p id="red">

         The color of this text is red.

      </p>
      <p id="green">

         The color of this text is green.

      </p>
      <p id="yellow">

         The color of this text is yellow.

      </p>
   </body>
</html>

There’s an extension to RGB and is called RGBA. The “A” part stands for alpha and is used to change the transparency of an element. The value for this channel is between 0 and 1 which 0 means fully transparent (basically the target element becomes invisible to the eyes of audience) and the value 1 turns the target element to fully opaque (users can easily see the element in the page).

You can do some experiment here:

(Place the code you’ve wrote for this section here)

Example:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      p{
         color:rgba(128, 0, 128, 0);
      }
  </style> 
</head>
   <body>
      <p>

         You won’t see me unless highlight me. 

      </p>
   </body>
</html>

Note: In the example above, Even though the content is invisible to audience, it’s still there and you can see if you highlight the content.

  1. HEX value: Another type of value we can set for properties like
    “color” is Hexadecimal values (abbreviated hex) which are another type
    of numbers that are used in computer science. For example the number 255
    in Hexadecimal world is “ff” or 100 in Hexadecimal World is “64”
    (Basically numbers are equally the same but we represent them
    differently in Hexadecimal world).

Here’s the structure of Hex when is used for the value of properties like “color”:

#rrggbb

      

      selector{
         property-name: #rrggbb;
      }

Note: Hex values start with ‘#’ sign.

We still use the combination of Red, Green and Blue in order to create the color we want but the formula for Hex is a little different as we shall explain now:

‘rr’ stands for the amount of “red” color , ‘gg’ stands for the amount of “green” color and ‘bb’ stands for the amount of “blue” color that we want to use.

We should replace each ‘rr’,’gg’ and ‘bb’ with a hex value range between 00 to ff where 00 is equal to 0 and ff is equal to 255.

Example:

p{

         color:#ffff00

      }

In the example above we can divide the value ‘ffffff’ in 3 sections (ff,ff,ff): in the first section ‘ff’ is for ‘red(rr)’ which basically means the amount of ‘red’ color is 255. The second section also has the value of ‘ff’ which means the amount of ‘green (gg)’ color is 255. And the third section has the value of ’00’ which means the amount of ‘blue (bb)’ color is 0.

You can do some experiment here:

(Add the script you’ve wrote for HEX here)

Example:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         color:#0000ff;
      }
      #red{
         color:#ff0000;
      }
      #green{
         color:#008000;
      }
      #yellow{
         color:#ffff00;
      }
  </style> 
</head>
   <body>
      <p id="blue">

         The color of this text is blue.

      </p>
      <p id="red">

         The color of this text is red.

      </p>
      <p id="green">

         The color of this text is green.

      </p>
      <p id="yellow">

         The color of this text is yellow.

      </p>
   </body>
</html>
  1. HSL values: HSL stands for Hue, Saturation and Lightness. We can
    use this type of value in order to set the color of properties like
    “color” property.

This is the structure of HSL:

selector{
         property-name: hsl(hue, saturation, lightness);
      }

Hue:

Hue is used to set the color and its value ranged from 0 to 360.

Saturation:

Saturation is used to declare the percentage of “grayness” in the color. The value we set here is from 0% to 100% where 100% means without any grayness in the color and 0% means fully gray.

Lightness:

Lightness is used to declare the percentage of “blackness” in the color. The value we set here is from 0% to 100% where 100% means without any blackness in the color (basically it is 100% bright) and 0% means fully black.

Here you can do some experiment:

(Place the HSL script you’ve wrote for this section here)

Example:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         color:hsl(240, 0%, 50%);
      }
      #red{
         color:hsl(0, 100%, 50%);
      }
      #green{
         color:hsl(120, 100%, 25%);
      }
      #yellow{
         color:hsl(60, 100%, 50%);
      }

      

  </style> 
</head>
   <body>
      <p id="blue">

         I supposed to be blue but you see me in gray color.

      </p>

      

   </body>
</html>

In the example above, the first value is in blue range but because we set the second value (grayness of the color) to 0% (fully gray) you no-longer see blue color.

The last value is 100% lightness. If we changed this value to 0% then the output would be completely dark as the example below shows:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         color:hsl(240, 0%, 0%);
      }
      #red{
         color:hsl(0, 100%, 50%);
      }
      #green{
         color:hsl(120, 100%, 25%);
      }
      #yellow{
         color:hsl(60, 100%, 50%);
      }

      

  </style> 
</head>
   <body>
      <p id="blue">

         I supposed to be blue but you see me in black color.

      </p>

 

   </body>
</html>

Here’s another example:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         color:hsl(219, 100%, 50%);
      }
      #red{
         color:hsl(0, 100%, 50%);
      }
      #green{
         color:hsl(120, 100%, 25%);
      }
      #yellow{
         color:hsl(60, 100%, 50%);
      }

      

  </style> 
</head>
   <body>
      <p id="blue">

         I supposed to be blue but you see me in gray color.

      </p>
      <p id="red">

         The color of this text is red.

      </p>
      <p id="green">

         The color of this text is green.

      </p>
      <p id="yellow">

         The color of this text is yellow.

      </p>
   </body>
</html>

There’s an extension to HSL and is called HSLA. The “A” part stands for alpha and is used to change the transparency of an element. The value for this channel is between 0 and 1 which 0 means fully transparent (basically the target element becomes invisible to the eyes of audience) and the value 1 turns the target element to fully opaque (users can easily see the element in the page).

You can do some experiment here:

(Place the code you’ve wrote to this section here)

Example:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         color:hsla(219, 100%, 50%,0.3);
      }
      #red{
         color:hsl(0, 100%, 50%,0.5);
      }
      #green{
         color:hsl(120, 100%, 25%,0.7);
      }
      #pink{
         color:hsl(305, 100%, 50%,1);
      }

      

  </style> 
</head>
   <body>
      <p id="blue">

         I supposed to be blue but you see me in gray color with alpha value set to 0.3.

      </p>
      <p id="red">

         The color of this text is red with alpha value set to 0.5.

      </p>
      <p id="green">

         The color of this text is green with alpha value set to 0.7.

      </p>
      <p id="pink">

         The color of this text is pink with alpha value set to 1.

      </p>
   </body>
</html>

We can change the background color of an element using background-color property.

Just like the way we set the value for “color” property, the same types can be used for this property which is color-name, RGB, RGBA, HEX, HSL and HSLA.

Example:

<!DOCTYPE html>
<html>
<head>
   <title>CSS is fun :)</title>

   

  <style type="text/css">
      #blue{
         background-color:blue;
      }
      #red{
         background-color:hsl(0, 100%, 50%,0.5);
      }
      #green{
         background-color:rgba(0, 128, 0, 0.7);
      }
      #pink{
         background-color:#ff00ea;
      }

      

  </style> 
</head>
   <body>
      <p id="blue">

         Blue background color.

      </p>
      <p id="red">

         The color of this text is red with alpha value set to 0.5.

      </p>
      <p id="green">

         The color of this text is green with alpha value set to 0.7.

      </p>
      <p id="pink">

         The color of this text is pink with alpha value set to 1.

      </p>
   </body>
</html>