JavaScript / Web pages

CSS colours and opacity

Write colours as numbers with RGB and hex codes, and make elements see-through with opacity. Baccalaureate

A CSS rule has three parts: the selector (which element), the property (what to change) and the value (how). The <span> tag wraps part of a text so you can style just that part.

CSS
span {
  color: orange;
}
Browser

RGB

A screen makes every colour from three lights: red, green and blue. Write rgb(R, G, B); each number goes from 0 (off) to 255 (full brightness).

ColourValue
Redrgb(255, 0, 0)
Yellowrgb(255, 255, 0)
Dark redrgb(150, 0, 0)
Whitergb(255, 255, 255)
Blackrgb(0, 0, 0)

Hex codes

The same RGB numbers written in base 16 after #: two characters per colour. ff means 255 and 00 means 0, so orange rgb(255, 140, 0) is #ff8c00.

CSS
span {
  color: #ff8c00;
}
Browser

Opacity

opacity goes from 1 (fully visible) to 0 (fully transparent). With :hover the style applies only while the mouse is over the element. Hover over the star:

CSS
.star:hover {
  opacity: 0.3;
}
Browser

Practice

1Write the programCurriculum practice

Make the span text red using RGB.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
span {
  
}
Browser
2Fill in the blankCurriculum practice

Make the span dark red: red brightness 150 only.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
span {
  color: rgb(___, 0, 0);
}
Browser
3Write the programCurriculum practice

Make the span purple with the code #a260bf.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
span {
  
}
Browser
4Write the programCurriculum practice

Make the star (class star) disappear completely on hover.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Browser
5Write the programExtra practice

Give the h1 a background of #19195C and white text #ffffff.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
h1 {
  
}
Browser

Key points

  • rgb(R, G, B): each value from 0 to 255.
  • Hex code #rrggbb: ff = 255 and 00 = 0.
  • opacity from 1 (visible) to 0 (transparent); :hover applies on mouse-over.