next up previous
Next: Image Backgrounds Up: Web Page Layout Previous: Web Page Backgrounds

Using Colour

Colour is widely used in many HTML contexts. We, briefly, introduce the concept of colour in HTML here.

There are two ways to specify colour:

Hexadecimal Colour Representation

Colour for each red, green and blue colour component is represented by a range 0 to 255 decimal (FF hex) where

Thus, we can form:
  Red    Green   Blue     Colour   
      Black
255 255 255 White
255     red (full)
  255   green (full)
  255   blue (full)
x x x x = 0-255
      Shade of grey
255   255 Magenta
  255 255 Yellow
and so on ...

Choosing a predefined colour

You can choose one of 16 predefined colours:

Black, White, Green, Maroon, Olive, Navy, Purple, Gray, Red, Yellow, Blue, Teal, Lime, Aqua, Fuchsia and Silver.

Colour names might be easier to remember but you only have a restricted choice compared with several million possible colours possible with hexadecimal representation.

The quality and choice of colour will depend on the hardware you run the browser on.

To change the background colour you must set the BGCOLOR attribute in the BODY tag.

To specify a hexadecimal number you must put a # before the number and then two hex digits for each red, green and blue component respectively.

For example:

<BODY BGCOLOR = "#FFFFFF">

gives us a white background.

The CGI script rgb.pl introduced in the last lecture (Section 15) provides an HTML form that helps you compute the value and exact form of output.

RGBtoHex

RGBtoHex converts standard RGB values (three 0 to 255 ASCII numbers indicating red, green, and blue), into a hexadecimal triplet that can be used for the background and text colors in Netscape 1.1b1 or in any other program that requires colors in this format.

RGB2toHex was written by Eric Murray and Laura Lemay, and is in the public domain. Feel free to install the source for this program on your own system (it requires the perl language and cgi-lib.pl) but please credit us for it (you can use the signature at the bottom of this form).


Please enter the RGB values for your color:

Red (0-255):
Green (0-255):
Blue (0-255):


RGBtoHex was written by Laura Lemay and Eric Murray


Download Source code:


To set BGCOLOR with a predefined name simply refer to the name. For example:

<BODY BGCOLOR = "green">

gives us a green background.

Some example backgrounds and HTML listing are as follows:

Hexadecimal Colour Settings

Predefined Colour Settings


next up previous
Next: Image Backgrounds Up: Web Page Layout Previous: Web Page Backgrounds
Dave Marshall
9/28/2001