All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

CSS colours, opacity, gradients and length units

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Colours can be written as names, hexadecimal, RGB or HSL, made see-through with opacity and blended with gradients; sizes use absolute or relative length units (spec 8.2.1 to 8.2.3).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Specifying colours (8.2.1)

NotationExample (all are the same orange)How it works
Colour name`orange`One of about 140 named colours
Hexadecimal`#ffa500`#RRGGBB: two hex digits (00 to ff, which is 0 to 255) for each of red, green and blue
Short hex`#f80`#RGB is short for #RRGGBB when each pair repeats: #f80 is #ff8800 (a slightly different orange)
RGB`rgb(255, 165, 0)`Red, green and blue from 0 to 255
HSL`hsl(39, 100%, 50%)`Hue (0 to 360 degrees round the colour wheel: 0 red, 120 green, 240 blue), saturation (0% grey to 100% full colour), lightness (0% black, 50% normal, 100% white)

Opacity and gradients (8.2.2)

header {
  background-image: linear-gradient(to right, #003366, #3399cc);
}
.badge {
  background-image: radial-gradient(circle, yellow, orange);
}

HSL makes colour schemes easy: keep the hue and change lightness for lighter and darker shades of one colour.

Length units (8.2.3)

UnitTypeMeaning
pxAbsoluteA CSS pixel
pt, cm, mm, inAbsolutePhysical units, mainly for print (1in = 96px)
%RelativeA percentage of the parent's size (for width, of the parent's width)
emRelativeRelative to the font size of the element (for font-size, of the parent's font size)
remRelativeRelative to the font size of the root html element (16px by default)
vw, vhRelative1% of the viewport (browser window) width or height

Relative units scale with the screen or the user's text size, which suits responsive and accessible design

Calculating relative sizes

With the browser default root font size of 16px:

Using rem or em for text lets users who increase their browser's text size get bigger text everywhere.

Worked example

Convert rgb(0, 128, 255) to hexadecimal.

  1. 0 in hex is 00.
  2. 128 = 8 x 16 + 0, which is 80 in hex.
  3. 255 = 15 x 16 + 15, which is ff.

Answer: #0080ff

Key idea: Colours: names, #RRGGBB hex, rgb(r, g, b) from 0 to 255, hsl(hue, saturation%, lightness%); opacity fades the whole element, rgba/hsla only the colour; linear-gradient and radial-gradient make backgrounds. px is absolute; %, em, rem, vw and vh are relative.

Check yourself

  1. What is rgb(255, 0, 255) written as a six-digit hexadecimal colour? Type it with the #.
    Show answer

    #ff00ff. 255 is ff and 0 is 00, so #ff00ff.

  2. The root font size is 16px. How many pixels is 2.5rem?
    Show answer

    40 px. 2.5 x 16 = 40.

  3. A div has font-size: 18px. A span inside it has font-size: 1.5em. What is the span's font size in pixels?
    Show answer

    27 px. em for font-size is relative to the parent: 1.5 x 18 = 27.

  4. A banner needs a semi-transparent black background but solid white text. Which declaration should be used on the banner?
    • background-color: rgba(0, 0, 0, 0.6);
    • opacity: 0.6;
    • background-color: black; opacity: 0.6;
    • color: rgba(255, 255, 255, 0.6);
    Show answer

    background-color: rgba(0, 0, 0, 0.6);. rgba makes only the background colour transparent. opacity would fade the text as well.

  5. In hsl(120, 100%, 50%), what does 120 set?
    • The amount of red from 0 to 255
    • The lightness as a percentage
    • The hue, as an angle on the colour wheel (green)
    • The opacity
    Show answer

    The hue, as an angle on the colour wheel (green). Hue is an angle from 0 to 360: 0 red, 120 green, 240 blue.

  6. Which unit is relative to the width of the browser window?
    • vw
    • px
    • pt
    • cm
    Show answer

    vw. 1vw is 1% of the viewport width.

  7. The browser window is 1440px wide. How many pixels wide is an element with width: 25vw?
    Show answer

    360 px. 25% of 1440 = 360.

  8. Which value creates a background that fades from white at the top to grey at the bottom?
    • radial-gradient(to bottom, white, grey)
    • gradient(white, grey)
    • linear-gradient(grey, white, to top-left)
    • linear-gradient(to bottom, white, grey)
    Show answer

    linear-gradient(to bottom, white, grey). linear-gradient takes a direction and then the colours in order.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.