Colour and type give a page its character and decide how easily it can be read. CSS offers several ways to write a colour and a rich set of properties for text.

Colour Values

A colour can be given by a name, such as red or navy. It can also be given as a hexadecimal code, such as #1F3864, where the pairs of digits give the red, green and blue amounts. The function rgb(31, 56, 100) gives the same three amounts as decimal numbers from 0 to 255. The function hsl(220, 53%, 26%) describes a colour by its position on the colour wheel, its saturation and its lightness.

Definition: Colour value. A way of writing a colour, such as a name, a hex code, rgb() or hsl().

The property color sets the colour of text, and background-color sets the background.

Table 2.4: Ways of writing the same shade of blue

Form Example Note
Name blue Limited set of names
Hex #0000FF Pairs of hex digits for red, green, blue
RGB rgb(0, 0, 255) Numbers from 0 to 255
HSL hsl(240, 100%, 50%) Hue, saturation, lightness

Fonts

The property font-family gives a list of fonts in order of preference. The browser uses the first one that is installed on the visitor's computer. The list should end with a generic family such as sans-serif, serif or monospace, which every system provides. The property font-size sets the size in pixels, em or rem, and font-weight sets bold text.

Text Styling

The property line-height sets the space between lines, and a value of 1.5 is comfortable for body text. The property text-align aligns text left, right, centre or justified. The property text-decoration adds or removes underlines, which is useful for links. The property letter-spacing changes the space between letters.

Readability

Good typography is mostly restraint. Use at most two or three fonts, keep body text near 16 pixels, use strong contrast between text and background, and keep lines from becoming too long.

Example 2.4: Styling an article

<style>
  body { font-family: Georgia, serif; }
  h1   { color: rgb(31, 56, 100); text-align: center; }
  p    { line-height: 1.6; color: #333333; text-align: justify; }
  a    { text-decoration: none; color: hsl(210, 80%, 40%); }
</style>
<h1>Why Read Slowly</h1>
<p>Comfortable spacing and strong contrast help readers follow
   a long paragraph without effort.</p>
<p><a href="#">Read more</a></p>

Output in the browser:

Common mistakes

  • Using pale text on a pale background, which is hard to read.
  • Listing a font without a generic fallback family at the end.
  • Setting very small text sizes, which strain the eyes.
  • Using too many fonts on one page.

Key takeaways

  • Colours can be written as names, hex codes, rgb() or hsl().
  • Use color for text, background-color for backgrounds and finish font lists with a generic family.
  • line-height around 1.5 makes body text easy to read.
  • Good typography relies on contrast, restraint and spacing.

Practice questions

  1. Write the hex form of pure red.
  2. Why should a font-family list end with a generic family?
  3. Which property sets the space between lines?

Answers to the practice questions

  1. #FF0000.
  2. So that the browser has a fallback if none of the named fonts is installed.
  3. line-height.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit II