The Academy

Loading the search index…

Searches the academy on this device only.

CSS basics: selectors, colours and typography

Style an HTML page with CSS rules: pick elements with selectors, set colours and fonts, and see a plain page transform into a designed one.

30 minbeginnerPractical task needs a laptop or desktop

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

By the end of this lesson you will be able to

  • Explain that CSS controls the appearance of HTML elements.
  • Write a CSS rule made of a selector, properties and values.
  • Select elements by type, by class and by id, and say which to prefer.
  • Set text colour, background colour, font family and size.
  • Link a stylesheet to an HTML page and check that it loads.

HTML on its own looks plain: black text on white, in the browser's default font. CSS, short for Cascading Style Sheets, is the language that turns the same page into something designed, with colours, fonts, spacing and layout. Because CSS is kept separate from HTML, you can change the look of a whole site by editing one file. This lesson gives you the core vocabulary and a few rules you can try straight away.

What CSS does

CSS describes how HTML elements should look. HTML says "this is a heading"; CSS says "headings are dark green, in a serif font, with space below". Think of HTML as the plan of a house and CSS as the paint, furniture and curtains. The same plan can look warm or stark depending on the styling.

The word cascading means that several rules can apply to one element, and the browser has fixed ways of choosing between them. In short: more specific rules win over general ones, and when rules are equal the later one wins. You will not need the detail yet, but it explains why a rule sometimes seems to be ignored.

Diagram

The same HTML, drawn live before and after adding CSS.

Without CSS

With CSS

The same HTML, drawn live before and after adding CSS.
Read this figure as text

Two small live web pages with identical content, one unstyled and one styled with colour and fonts.

Two live mini pages with identical HTML: a heading Garden club, a sentence about meeting on Saturdays and a link. The before page has no CSS: black text in the browser's default serif font on white. The after page has CSS: a dark green heading in a sans-serif font, a pale green background, darker grey body text and a rounded orange link.

The anatomy of a CSS rule

CSS is written as rules. A rule has a selector, which says which elements to style, followed by curly brackets containing one or more declarations. Each declaration is a property, a colon, a value and a semicolon. The semicolon matters: if you forget it, the next declaration may be ignored.

CSS
h1 {
  color: darkgreen;
  font-size: 2rem;
}
One rule with two declarations.
Diagram

A CSS rule split into selector, property and value.

h1SelectorWhich elements to style
{ Opening bracketStarts the declarations
colorPropertyWhat to change
: ColonSeparates name and value
darkgreenValueWhat to change it to
;SemicolonEnds the declaration
}Closing bracketEnds the rule
A CSS rule split into selector, property and value.
Read this figure as text

A CSS rule with selector h1 and the declaration color: darkgreen split into labelled parts.

The text h1 { color: darkgreen; } is split into labelled parts. h1 is the selector: it chooses every level-one heading. The curly brackets hold the declarations. color is the property, which says what to change. darkgreen is the value, which says what to change it to. The semicolon ends the declaration.

Selectors: choosing what to style

There are three selectors you will use constantly. A type selector is just an element name, such as p, and matches every element of that type. A class selector starts with a full stop, such as .button, and matches every element that has class="button" in its HTML. An id selector starts with a hash, such as #top, and matches the one element with that id. An id must be unique on a page.

Common selectors
SelectorMatchesHTML it matches
pEvery paragraph<p>Hello</p>
.noteEvery element with class note<p class="note">Careful</p>
#topThe one element with id top<h1 id="top">Welcome</h1>
nav aLinks inside a nav element<nav><a href="#">Home</a></nav>
h1, h2Both h1 and h2 (a list of selectors)<h2>Sessions</h2>

Prefer classes for most styling. They are reusable, you can put several on one element, and they are easy to change. Use ids sparingly. Choose class names that say what something is, such as note or button, rather than how it looks, such as big-red, because looks change.

Colours and fonts

The color property sets text colour and background-color sets the background. You can write a colour as a name (darkgreen), as a hex code (#1b5e20, a hash then six hexadecimal digits for red, green and blue) or as rgb(27, 94, 32). They are three ways of writing the same colour. Pick colours with strong contrast between text and background so that people with weaker eyesight, or anyone in bright sunlight, can read them.

Fonts are set with font-family. List several fonts in order, ending with a generic family such as sans-serif or serif, because not every computer has every font. Set the size with font-size. The unit rem means a multiple of the browser's base text size, so 1.25rem is a quarter larger than normal and respects a reader's own size setting. Use line-height for spacing between lines: around 1.5 is comfortable. font-weight: bold and text-align: center do what they say.

CSS
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 1.125rem;
  line-height: 1.5;
  color: #2b2b2b;
  background-color: #eef7ee;
}

h1 {
  color: #1b5e20;
}

.note {
  background-color: #fff3cd;
  color: #5a4500;
}

#top {
  text-align: center;
}
A small stylesheet that uses type, class and id selectors.

Adding CSS to a page

The tidy way is a separate file. Save your rules in style.css in the same folder as your page and link it from the <head>. The <link> element has rel="stylesheet" to say what the file is and href to say where it is. You can also write rules inside a <style> element in the head, which is handy for a single small page. The live editor below gives you both HTML and CSS panels.

HTML
<head>
  <meta charset="utf-8">
  <title>Garden club</title>
  <link rel="stylesheet" href="style.css">
</head>
Linking a stylesheet from the head of the page.

Use the CSS panel to change the heading colour, the page background and the font. Then add a class to one paragraph in the HTML and style it.

Loading lab…

Style a page step by step

  1. Step 1: Colour the heading

    In the CSS panel write h1 { color: darkgreen; }.

    You should see: Every h1 in the preview turns dark green.

  2. Step 2: Change the background

    Add body { background-color: #eef7ee; }.

    You should see: The whole page background becomes pale green.

  3. Step 3: Choose a font

    Inside the body rule add font-family: Arial, sans-serif;.

    You should see: Text changes to a sans-serif font.

  4. Step 4: Make a class

    In the HTML give one paragraph class="note". In the CSS add .note { background-color: #fff3cd; }.

    You should see: Only that paragraph gets a pale yellow background.

Common mistakes

  • ✖ Instead of: Writing color = red; or color - red;.

    ✔ Do this: A declaration is a property, a colon, a value and a semicolon: color: red;.

  • ✖ Instead of: Styling with a class in HTML but writing the selector without the full stop (note { ... }).

    ✔ Do this: A class selector needs a dot: .note { ... }.

  • ✖ Instead of: Naming classes after their looks, such as big-red-text.

    ✔ Do this: Name by purpose, such as warning, so changing the colour later does not make the name a lie.

  • ✖ Instead of: Choosing pale text on a pale background because it looks elegant.

    ✔ Do this: Pick strong contrast so everyone can read it.

If something goes wrong

My CSS changes do not show in the browser.
Check that you saved the file, that the link href matches the file name and folder exactly, and then refresh with F5. A hard refresh with Ctrl and Shift and R also bypasses stored copies.
One rule seems to be ignored.
Look for a missing semicolon or a missing bracket in the line above it. Another, more specific rule may also override it.
My font is not the one I wrote.
That font may not be installed on this computer. The browser then uses the next one in your list. End the list with a generic family such as sans-serif.
The colour name I typed has no effect.
Check the spelling. Colour names such as darkgreen are written as one word and invalid names are ignored. Use a hex code if unsure.

Key terms

CSS selector
The part of a CSS rule that says which elements the rule applies to.
CSS property
The name of one thing CSS can change, written before a colon and its value.

Practise it for real

Reading is not the same as being able to do it. Complete these on your own computer, then tick only what you have actually done. These ticks are your own honest record: nothing checks them for you.

Guided exercise

Style a page with five rules

In the live editor, take a simple page with an h1, two paragraphs and a link. Write at least five CSS rules: a body font and background, a heading colour, a class for one highlighted paragraph, and a rule for links. Describe what each rule changes.

You have succeeded when:
Guided exerciseNeeds a laptop or desktop

Link a real stylesheet

On your laptop, create index.html and style.css in the same folder. Link the stylesheet in the head, set a background colour, and open the page in your browser. If nothing changes, check the name, folder and spelling of the href.

You have succeeded when:
Independent challenge

Challenge: two moods, one HTML file

Keep the HTML of one page exactly the same and write two different stylesheets for it: one calm and one bold. Choose different colours, fonts and text alignment. Say what stayed the same and what changed, and which one is easier to read and why.

You have succeeded when:

Knowledge check

6 questions. You need 80% to pass. Mistakes here are part of learning, and you can retry as often as you like.

  1. Question 1.What is the job of CSS?
  2. Question 2.Match each selector to what it picks.
  3. Question 3.Fatuma writes p { color: blue background-color: yellow; } and finds that the background colour is not applied.What is the likely cause?
  4. Question 4.An id such as #top should be used on many elements on the same page.
  5. Question 5.Put these steps in order to style a page with a separate file.
    1. Save it next to index.html
    2. Add a link element with rel and href to the head
    3. Refresh the browser to see the result
    4. Write the rules in style.css
  6. Question 6.Which text and background combination is most readable?

Summary

  • CSS controls appearance; HTML controls structure.
  • A rule is a selector followed by declarations: property, colon, value, semicolon.
  • Type, class and id selectors pick elements; prefer classes.
  • Set colour, background-color, font-family, font-size and line-height, and check contrast.
  • Link a stylesheet with a link element in the head.

Checklist: can you do these?

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

Related lessons

References and further reading

Official sources for details that can change between versions.

On this page