Style Sheets
  Home arrow Style Sheets arrow Page 3 - Learn CSS, Manipulating Colors
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Learn CSS, Manipulating Colors
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2005-05-30

    Table of Contents:
  • Learn CSS, Manipulating Colors
  • Keywords
  • Using RGB Color Values
  • Haxadecimal Color Values
  • CSS Comments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Learn CSS, Manipulating Colors - Using RGB Color Values


    (Page 3 of 5 )

    You might know that the light spectrum consists of waves of energy. At one side of the spectrum is the color red, and at the other side is blue. Between them, there's green. Actually, this Red, Green, Blue (RGB) concept is very scientific and comes from outside the scope of CSS. It's enough to say that combining these colors can produce millions of colors. Computer monitors use RGB combinations to produce the colors you see in the screen. For example, when you mix the blue and the green colors you get cyan.

    Let's use RGB combinations to get some colors. To use the CSS RGB color system you have 2 options: a percentage or a value range from 0 to 255. Uusing this range we can have millions of colors due to the fact that we have color combinations of 256 multiplied by 256 multiplied by 256. Note that we need to mix the colors to get a specific color so we use the following syntax to get the color:

    rgb(the red color value, the green color value, the blue color value)

    Let's take an example, copy the following CSS code into a file and save it as colors.css and copy the HTML code too and save it as colorsTest.html

    body
    {
      font-family: Tahoma;
    }
    .TheRed
    {
      color: rgb(255,0,0);
    }
    .TheGreen
    {
      color: rgb(0,255,0);
    }
    .TheBlue
    {
      color: rgb(0,0,255);
    }
    .TheYellow
    {
      color: rgb(255,255,0);
    }
    .TheCyan
    {
      color: rgb(0,255,255);
    }
    .TheMagenta
    {
      color: rgb(255,0,255);
    }
    .TheBlack
    {
      color: rgb(0,0,0);
    }

     

    And here's the HTML code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <link rel="stylesheet" href="Colors.css" type="text/css">
        <title>Colors with CSS</title>
      </head>
      <body> 
        <h4 class="TheRed">This is a red text</h4>
        <h4 class="TheGreen">This is a green text</h4>
        <h4 class="TheBlue">This is a blue text</h4>
        <h4 class="TheYellow">This is a yellow text</h4>
        <h4 class="TheCyan">This is a cyan text</h4>
        <h4 class="TheMagenta">This is a magenta text</h4>
        <h4 class="TheBlack">This is a black text</h4>
      </body>
    </html>

     

    You will get the following result.

    The first thing that you have to notice is the new css syntax, which is using a dot before the selector. I will talk about selectors in great detail in the next 2 articles, but for now just understand that this syntax is related to the class attribute in HTML. The first class selector (.TheRed Selector) set the value rgb(255,0,0), meaning it has a lot of red but no green or blue. With the other colors generated through combinations of multiple colors, the rule is different because we have to mix the colors to produce them. To produce yellow, you need to mix red and green like this: rgb(255,255,0);, leaving a zero in the blue channel.

    We can write the RGB values using percentage instead of integer numbers, but you must use a percentage in all the 3 channels. In other words, you can't write an RGB value like this rgb(255, 40%, 90%). The next CSS code produces the same colors as the above RGB integer numbers values

    body
    {
      font-family: Tahoma;
    }
    .TheRed
    {
      color: rgb(100%,0%,0%);
    }
    .TheGreen
    {
      color: rgb(0%,100%,0%);
    }
    .TheBlue
    {
      color: rgb(0%,0%,100%);
    }
    .TheYellow
    {
      color: rgb(100%,100%,0%);
    }
    .TheCyan
    {
      color: rgb(0%,100%,100%);
    }
    .TheMagenta
    {
      color: rgb(100%,0%,100%);
    }
    .TheBlack
    {
      color: rgb(0%,0%,0%);
    }

    More Style Sheets Articles
    More By Michael Youssef


     

    STYLE SHEETS ARTICLES

    - Creating Gradients for Individual Containers...
    - Creating Gradients for Web Page Headers with...
    - SEO Scrolling Frames Problem Solved
    - Building Cross-Browser Background Effects wi...
    - CSS: Pseudo
    - Using PNG Images to Build Background Effects
    - CSS: Continuing the Clarification of CSS Cla...
    - CSS: Top Secret Classification
    - CSS: Dimensions
    - CSS: Margins and Padding
    - CSS: Crossing the Border
    - CSS: Text, Fonts, and Tables
    - CSS: Working with Text
    - CSS: Backgrounds
    - CSS for the Newbie







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway