Skip to the page
Year 10 · ICT · Summer 2 · Website authoring and images; the Year 10 mock

CSS stylesheets

PracticalPresent ▶

What this lesson covers

21.3a CSS: rules, classes, backgrounds and fonts

Cambridge IGCSE ICT 0417 · Topic 21: Website authoring · Paper 3

21.3b CSS: page styles, tables and the cascade

Cambridge IGCSE ICT 0417 · Topic 21: Website authoring · Paper 3

You will learn to

  • Write CSS rules with selectors, properties and values.
  • Create an external stylesheet and attach it with a relative path.
  • Set colours in hex, font families with a generic fallback, sizes and alignment.
  • Apply the cascade to predict which style wins when an inline style is added.

Success criteria

  • I can attach my stylesheet and see the page change.
  • I can set h1 to a hex colour and a font with a generic fallback, like sans-serif.
  • I can explain why an inline style beats the external stylesheet.
The syllabus points for these sections
  • Write CSS rules with selectors, properties, values and comments.
  • Use inline styles and external stylesheets, and attach a stylesheet with a relative path.
  • Create and apply classes.
  • Set background colours and images.
  • Set font family with a generic fallback, size, colour, alignment, bold and italic.
  • Style h1, h2, h3, p and li in one external stylesheet.
  • Style tables and cells: borders, collapse, padding, spacing, size, colour and alignment.
  • Apply the cascading order to predict which style wins.
  • Give clear evidence and avoid the common ways website marks are lost.

Key words

Say each word, then read what it means. Every one is in the glossary too.

HTML
HyperText Markup Language: the language of the content layer of a web page.
CSS
Cascading Style Sheets: the language of the presentation layer, which sets how a page looks.
Tag
A word in angle brackets, such as `<p>`, that marks the start or end of an element.
Attribute
Extra information inside an opening tag, written name="value", such as src or alt.
Head
The part of a page holding information about it: title, meta tags, stylesheet links.
External stylesheet
A separate .css file attached to pages with the link element.
Inline style
CSS written in a style attribute inside one tag; it affects only that element.
Class
A label given to elements with the class attribute so CSS can style them as a group.
Selector
The part of a CSS rule that says which elements it styles, such as h1 or .note.
Generic font family
A fallback type of font (serif, sans-serif, monospace) used if named fonts are missing.
Cascade
The rules that decide which style wins when two styles set the same property.
Relative path
A file location described from the current file's folder, such as images/logo.png.
Absolute path
A full file location from the very start, such as a full URL or C:/...
Padding
The space inside an element or cell between its border and its content.
Alt text
Alternative text for an image, read by screen readers and shown if the image fails.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

21.3a CSS: rules, classes, backgrounds and fonts

How a CSS rule is written

A CSS rule has a selector (what to style), then curly brackets { } holding one or more declarations.

Each declaration is a property, a colon, a value and a semicolon: `color: #8B4513;`.

Comments go between /* and */. The browser ignores them. Use them to explain your styles.

/* Café Retiro: main headings */
h1 {
    color: #8B4513;
    text-align: center;
}

/* one rule for two selectors */
h2, h3 {
    color: #2E7D32;
}
!Watch out

CSS uses American spelling: color and center. colour and centre are not recognised, and the browser ignores that whole declaration.

Inline styles and external stylesheets

An inline style goes inside one tag, in a style attribute. It affects only that element.

An external stylesheet is a separate .css file attached with `<link>`. It can style every page.

An internal stylesheet sits in a `<style>` element in the head. It affects only that page.

Use external stylesheets for a whole site. Use inline styles only when the question asks, or for one exception.

<!-- inline: only this heading -->
<h1 style="color: #8B4513; text-align: center;">Café Retiro</h1>

<!-- in the head: internal and external -->
<link rel="stylesheet" href="styles/cafe.css">
<style>
    h2 {
        color: #1565C0;
    }
</style>
/* styles/cafe.css: CSS only, no <style> tags */
h2 {
    color: #2E7D32;
}
!Watch out

Never put `<style>` tags or any HTML inside a .css file. The browser stops reading the rule they break, and the styles fail.

Classes in CSS

A class selector starts with a full stop: .note styles every element with class="note".

p.note styles only paragraphs with that class. Other elements with class note are not affected.

A class name is case-sensitive and has no spaces. In the HTML it has no full stop.

e.g.Example
The first paragraph is brown and bold. The second has two classes, so it is brown, bold and italic.
<p class="price">Café con leche €1.80</p>
<p class="price note">Prices include VAT.</p>
.price {
    color: #8B4513;
    font-weight: bold;
}

p.note {
    font-style: italic;
}
!Watch out

Writing class=".price" in the HTML is wrong. The full stop belongs only in the CSS selector.

Backgrounds: colour and image

background-color fills an element with a colour. Colours can be names (white) or hex codes (#FDF6EC).

background-image uses url() with a relative path. The path starts from the CSS file, not from the HTML page.

background-repeat controls tiling: repeat, repeat-x, repeat-y or no-repeat.

body {
    background-color: #FDF6EC;
    background-image: url("../images/beans.jpg");
    background-repeat: repeat-x;
}

.offer {
    background-color: #FFF3E0;
}
?Why?

cafe.css is in the styles folder, so it goes up one folder (../) to reach images. Setting a background colour too means the page still looks right if the image fails to load.

!Watch out

A hex code has # and six hex digits (0–9, A–F). #FDF6E is only five digits and is ignored.

Fonts: family, size, colour, alignment, bold, italic

font-family lists fonts in order of preference, separated by commas. End with a generic family: serif, sans-serif or monospace.

The browser uses the first font it has. The generic family is the last resort, so the text still has the right kind of font.

font-size in px, pt, em or %. color sets the text colour. text-align is left, right, center or justify.

font-weight: bold makes text bold. font-style: italic makes it italic.

h1 {
    font-family: "Trebuchet MS", Helvetica, sans-serif;
    font-size: 32px;
    color: #8B4513;
    text-align: center;
    font-weight: bold;
}

p {
    font-family: Georgia, "Times New Roman", serif;
    font-size: 16px;
    font-style: italic;
}
!Watch out

Put a font name in quotes when it has spaces ("Times New Roman"). Never put quotes round a generic family. "sans-serif" in quotes is treated as a font called sans-serif and does not work as a fallback.

21.3b CSS: page styles, tables and the cascade

A full stylesheet for h1, h2, h3, p and li

Exam tasks often give a list of styles to set for each element. Write one rule per selector, in the same order as the list.

Repeat a property in each rule where the task asks for it, even if it looks the same as another rule.

Try the toy: paste this CSS into the CSS box. In the HTML box, replace everything between `<body>` and `</body>` with this HTML, so the stylesheet link stays. Change the h2 colour or the p font size and watch the preview update.

/* Café Retiro house style */
body {
    background-color: #FDF6EC;
}

h1 {
    font-family: "Trebuchet MS", Helvetica, sans-serif;
    font-size: 32px;
    color: #8B4513;
    text-align: center;
}

h2 {
    font-family: "Trebuchet MS", Helvetica, sans-serif;
    font-size: 24px;
    color: #2E7D32;
}

h3 {
    font-family: "Trebuchet MS", Helvetica, sans-serif;
    font-size: 18px;
    color: #2E7D32;
    font-style: italic;
}

p {
    font-family: Georgia, "Times New Roman", serif;
    font-size: 16px;
    color: #333333;
    text-align: justify;
}

li {
    font-family: Georgia, "Times New Roman", serif;
    font-size: 16px;
    font-weight: bold;
}
<h1>Café Retiro</h1>
<h2>Breakfast</h2>
<h3>Served until 12:00</h3>
<p>Fresh coffee, toast with tomato and olive oil, and homemade cakes.</p>
<ul>
    <li>Café con leche</li>
    <li>Tostada con tomate</li>
    <li>Tarta de Santiago</li>
</ul>
?Why?

The li rule makes the three menu items bold. Their font, size and colour come from the li rule too, so lists match the paragraphs.

There is something to try for this step in the IGCSE library.

Table borders: thickness, colour, collapse, invisible

border sets thickness, style and colour in one line: `border: 2px solid #8B4513;`. The style must be given, such as solid, dashed or dotted.

A border on table styles only the outside edge. Borders between cells need a rule for th and td too.

border-collapse: collapse merges the double lines between cells into single lines.

To make borders invisible, use border: none (or border-style: hidden). To make them visible, give a style such as solid.

table {
    border: 3px solid #8B4513;
    border-collapse: collapse;
}

th, td {
    border: 1px solid #8B4513;
}

/* a layout table with no visible borders */
table.plain, table.plain td {
    border: none;
}
!Watch out

border: 2px #8B4513; with no style shows no border at all. The default style is none.

Cells: padding, spacing, size, colour and alignment

padding is the space inside a cell, between its border and its content.

border-spacing is the gap between cells. It works only when border-collapse is separate, which is the default.

text-align sets horizontal alignment (left, center, right). vertical-align sets vertical alignment (top, middle, bottom).

Cells take width, height and background-color like any other element.

table {
    width: 80%;
    border-collapse: separate;
    border-spacing: 4px;
}

th {
    background-color: #8B4513;
    color: #FFFFFF;
    padding: 8px;
}

td {
    height: 40px;
    padding: 8px;
    text-align: center;
    vertical-align: middle;
    background-color: #FFF3E0;
}
?Why?

Padding stops text touching the border, so the table is easier to read. Spacing separates cells into boxes.

The cascade: which style wins

When two rules set the same property on the same element, the browser uses the cascade to choose.

Highest priority first: inline style, then internal stylesheet, then external stylesheet, then the browser's own defaults.

Internal beats external when the `<style>` element comes after the `<link>`, which is the normal order. In fact, the rule read last wins.

Inside one stylesheet, a later rule beats an earlier rule for the same selector. A class selector beats a plain element selector, whatever the order.

e.g.Example
Breakfast: the external rule says green, the internal rule says blue. Internal wins → blue. Lunch: the inline style says red. Inline wins → red. Both headings are still 24px, because only the external rule sets font-size. The cascade decides each property separately.
<link rel="stylesheet" href="styles/cafe.css">  <!-- h2 green -->
<style>
    h2 { color: #1565C0; }                      /* h2 blue */
</style>
...
<h2>Breakfast</h2>
<h2 style="color: #C62828;">Lunch</h2>        <!-- inline red -->
/* styles/cafe.css */
h2 {
    color: #2E7D32;
    font-size: 24px;
}

How website marks are lost

Most lost marks come from small typing errors that a browser ignores without warning.

Before taking screenshots, open the page in a browser and check every style and link really works.

MistakeHow to avoid it
Absolute path to a local file (C:/... or file:///...)use relative paths such as styles/cafe.css
colour or centre in CSSuse color and center
Missing ; or }, so later styles failend every declaration with ; and close every rule
Quotes round a generic familyfont-family: Arial, sans-serif;
Size with no unit or a space (16 or 16 px)16px
`<style>` tags inside the .css filethe .css file holds CSS only
File saved as cafe.css.txt or with wrong capitalssave as the exact name and type given
Missing alt text, or tags not closedcheck each tag has its partner
URL without https://write the full URL

Giving evidence

Take a screenshot of the HTML source in the text editor, with every line readable. Use more than one screenshot if the code is long.

Take a screenshot of the page in a browser, showing the result of the styles.

Take a screenshot of the CSS file in the editor, showing its file name if possible.

Paste them into the evidence document in the order the question asks, and label each one.

?Why?

The source proves which tags, attributes and paths you used. The browser view proves they work. Examiners often need both to award a mark.

!Watch out

A screenshot of a page that looks right earns nothing if the code that made it is cut off. Zoom so the code is readable but nothing is hidden.

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.