JavaScript / Web pages

CSS basics

Change a web page's colours, font size, weight and alignment with CSS rules, and target chosen elements with classes.

HTML builds the structure of a page, but not its look. To colour or enlarge text we use a second language, CSS. It is written in a separate editor, and the browser applies it to the page.

A CSS rule has three parts: the selector (which element), then inside { } the property (what to change) and the value (how), with a colon between them and a semicolon after. Change red to blue:

CSS
h1 {
  color: red;
}
Browser
PropertyChangesExample values
colortext colourred, blue, navy
background-colorbackground colouryellow, orange
font-weightfont weightbold, normal, 100 … 900
font-sizefont size10px, 50px
text-aligntext alignmentleft, center, right

One rule can hold several properties, one per line. Sizes are written in px (pixels):

CSS
h1 {
  color: blue;
  background-color: yellow;
  text-align: center;
}
p {
  font-weight: bold;
  font-size: 24px;
}
Browser
If you forget the semicolon after a value, that property and the next one both silently stop working. Check for ; after every line. And no space between the number and px: 24px, not 24 px.

Classes

The selector p changes every paragraph. To pick out one paragraph, give it a class name in its start tag: class="sea", then in CSS write a dot before the name: .sea.

CSS
.sea {
  color: blue;
}
.mountains {
  color: green;
}
Browser

A CSS comment goes between /* and */ (not <!-- --> as in HTML):

CSS
/* Sea in blue */
.sea {
  color: blue;
}
Browser
The classic mistake: forgetting the dot. sea { } looks for a tag called <sea> and finds nothing; the correct form is .sea { }. The dot goes only in CSS, not in class="sea".

Practice

1Write the programCurriculum practice

Make the text of the p tag red (red).

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

Complete the rule so the h1 background becomes yellow (yellow).

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
h1 {
  color: blue;
  ___: ___;
}
Browser
3Write the programCurriculum practice

In one rule for the p tag: set font-weight to 900, then font-size to 50px (in that order).

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

The CSS is ready. Give the first paragraph the class apple and the second the class watermelon.

HTML Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
<p>Apple</p>
<p>Watermelon</p>
Browser
5Write the programCurriculum practice

Colour each greeting by its class, in this order: .arabic lime, then .english purple, then .french navy.

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

Make the first line a CSS comment.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
___ Change the text color to navy. ___
p {
  color: navy;
}
Browser
7Write the programExtra practice

Style the class notice board with two rules in this order: (1) h1 with text-align center. (2) The exam class: colour red, then font weight bold.

HintRemember the dot before exam.
CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Browser

Key points

  • A CSS rule: selector { property: value; }.
  • color, background-color, font-weight, font-size (in px) and text-align.
  • A class class="x" in HTML matches .x in CSS; comments use /* */.