CSS: colours, fonts and selectors

Lesson 5 of 17 · HTML, CSS and JavaScript

CSS (Cascading Style Sheets) determines the look: colours, fonts, spacing and layout. The same HTML can look completely different with a different CSS file. This lesson covers rules, selectors (element, class, id), colours, fonts and how the browser decides which rule wins.

Before you start: required prior knowledge – lists, tables and parts of a page.
How to learn: do the tasks at the end on your own in your editor first; open the hint and the solution only after that. Every example has the code and a live result below it; type the code into your own file and change it.

A CSS rule

h1 {                 ← selector: which elements it applies to
  color: navy;       ← property: value;
  font-size: 32px;
}
Where CSS is writtenExampleWhen
a separate file (best)<link rel="stylesheet" href="css/style.css"> in the headalways – one file for the whole site
in a style element<style> h1 { color: navy; } </style> in the headsmall experiments, a single page
in an attribute<p style="color: red">avoid it – hard to change

Selectors

<style>
  p { color: #333; }                  /* all paragraphs */
  .important { background: #fff3cd; } /* all elements with class="important" */
  #title { color: #2b7bb9; }          /* the one element with id="title" */
  li a { color: green; }              /* links INSIDE an li */
  h2, h3 { font-family: Georgia, serif; }   /* several selectors at once */
  a:hover { color: crimson; }         /* when the mouse is over the link */
</style>
<h2 id="title">Selectors</h2>
<p>An ordinary paragraph.</p>
<p class="important">A paragraph with the class "important".</p>
<ul><li><a href="#">a link in a list</a></li></ul>
<a href="#">a link outside the list (hover over it)</a>

Result:

SelectorSelectsNote
pall elements of that type
.nameall elements with class="name"a class is used on several elements; an element can have several classes: class="card important"
#namethe element with id="name"an id is unique on the page
A Ba B that is anywhere inside Adescendant
A > Ba B that is a direct child of A
:hover, :first-child, :nth-child(2n)a state or a positionpseudo-classes

Classes are used most often for styling – they are flexible and easy to reuse.

Colours and units

Colour notationExample
a namered, navy, white
hexadecimal #RRGGBB#2b7bb9 – two hex digits each for red, green and blue (00–ff)
rgb() / rgba()rgb(43, 123, 185), rgba(0, 0, 0, 0.5) – the last number is the opacity
hsl()hsl(205, 62%, 45%) – hue, saturation, lightness
UnitMeaning
pxpixel – a fixed size
%a percentage of the parent's size
remrelative to the base font size (usually 16px): 1.5rem = 24px – recommended for text
emrelative to the parent's font size
vw, vha percentage of the window's width / height

A link with programming: in hexadecimal notation ff = 255, so #ff0000 is the same as rgb(255, 0, 0). The contrast between text and background must be high enough for the text to be easy to read.

Fonts and text

<style>
  body { font-family: "Segoe UI", Arial, sans-serif; line-height: 1.6; }
  h1 { font-size: 2rem; text-align: center; color: #1f5f91; }
  .subtitle { font-style: italic; text-align: center; color: #666; }
  .notice { font-weight: bold; text-transform: uppercase; letter-spacing: 2px; font-size: 0.8rem; }
  a { text-decoration: none; border-bottom: 2px solid #2b7bb9; }
</style>
<h1>Computer club</h1>
<p class="subtitle">Every Tuesday at 2 pm</p>
<p class="notice">Sign-up is open</p>
<p>More on the <a href="#">club's page</a>.</p>

Result:

PropertyExample values
font-familya list of fonts, the last one is generic: Arial, sans-serif
font-size16px, 1.2rem
font-weight / font-stylebold, 400, 700 / italic
text-alignleft, center, right, justify
line-heightthe space between lines, e.g. 1.6
text-decorationnone (no underline), underline

Special fonts can be included for free from Google Fonts – the site gives a ready-made <link> for the head.

The cascade: which rule wins

When several rules apply to the same element, the browser chooses in this order:

  1. Specificity – the more precise selector wins: style="" > #id > .class > element.
  2. Order – with the same specificity, the rule written later wins.
  3. Inheritance – some properties (colour, font) pass from the parent to the children; that is why it is enough to set the font for the body.
<style>
  p { color: gray; }
  .blue { color: blue; }
  p { color: green; }        /* later than the first p rule → it beats it */
  #special { color: red; }   /* an id is more specific than a class */
</style>
<p>An ordinary paragraph – green (the later rule).</p>
<p class="blue">A paragraph with a class – blue (a class beats an element).</p>
<p class="blue" id="special">A paragraph with a class and an id – red.</p>

Result:

Tip: right-click any element on a page → Inspect (or F12) shows its HTML and CSS. You can change the values live – the change lasts until you refresh the page.

Practice tasks

Each task has an example, a hint and a solution. The solutions have been checked in the browser.

TASK 01Difficulty: easy

Style the "About me" page

Make the file css/style.css, link it to the "About me" page and set: a font for the whole site, the colour of the main heading, a class .highlighted with a light background and links without an underline that change colour when the mouse is over them.

Common mistakes and tips
  • A semicolon after every value: color: red; – without it the next property is not applied.
  • A class is written without a dot in HTML (class="important"), and with a dot in CSS (.important).
  • If a style is not applied: check the path in the <link> and look in F12 whether the rule is crossed out (another rule beat it).
  • Do not use an id to style several elements – an id is unique.

Check your knowledge

Open the quiz

CSS basics

1. How do you select an element with class="card" in CSS?




2. Which colour is #ff0000?




3. Which selector is the most specific?




4. Where is it best to write the CSS for a whole site?