WDD  Β·  Web Design & Development

CSS: External Stylesheets and Cascade

Lesson 8 of 13 Approx 2 hrs (Friday double)

Learning intentions

  • Understand what an external stylesheet is and how to link one to an HTML page
  • Know the benefit of using external CSS for multi-page websites
  • Understand what the cascade is: how the browser decides which CSS rule wins
  • Know the order of specificity: inline style > ID selector > class selector > element selector
  • Understand what "separation of concerns" means in web development

Success criteria

  • I can write the correct <link> element to connect an HTML page to a .css file
  • I can write the correct relative path to a CSS file from pages in different folders
  • I can state which CSS rule wins when two rules conflict, using specificity order
  • I can explain one advantage of using an external stylesheet over an internal one
Warm up β€” what do you already know?

Answer before the lesson begins.

1. Which of the following is the correct HTML to link an external CSS file called style.css?

2. Which of these CSS selectors has the highest specificity?

3. Which type of CSS has the highest specificity of all?

Key vocabulary

external stylesheet
CSS saved in a separate .css file. Linked to HTML pages using a <link> element. One file can style many pages.
<link> element
An HTML element placed in <head> to connect a page to an external resource such as a CSS file. Self-closing.
rel attribute
Short for "relationship". In a <link> element, rel="stylesheet" tells the browser the linked file is a CSS stylesheet.
href attribute
The path to the linked file β€” same as in an anchor element. For CSS: href="style.css" or href="../style.css".
cascade
The set of rules the browser uses to decide which CSS rule applies when multiple rules target the same element. Specificity is calculated first; order (last rule wins) is used as a tiebreaker.
specificity
A score that determines which CSS rule wins. Higher score = higher priority. Inline (1000) > ID (100) > class (10) > element (1).
override
When a higher-specificity rule takes priority over a lower one for the same property and element.
separation of concerns
The principle of keeping HTML (structure/content) and CSS (presentation/style) in separate files, making each easier to maintain independently.

CSS: External Stylesheets and Cascade

What is an external stylesheet?

An external stylesheet is a separate file β€” saved with the .css extension β€” that contains all the CSS rules for a website. Instead of writing CSS inside every HTML page, you write it once in style.css and link every page to that file. This is the standard approach for real websites.

Benefits of using an external stylesheet:

  • Change the design in one file and it updates every linked page at once
  • Separates structure (HTML) from presentation (CSS) β€” making both easier to read and maintain
  • The browser can cache the stylesheet, making pages load faster on repeat visits

Linking an external stylesheet

Place a <link> element in the <head> of every HTML page that should use the stylesheet:

<head>
  <meta charset="UTF-8">
  <title>My Page</title>
  <link rel="stylesheet" href="style.css">
</head>

The rel="stylesheet" attribute is required β€” it tells the browser what kind of resource the link points to. The href is the relative path to the .css file.

Relative paths to CSS files

The path in href is relative to the HTML file, just like image paths in WDD5. If both index.html and style.css are in the same root folder:

<link rel="stylesheet" href="style.css">   <!-- same folder -->

If the HTML file is inside a pages/ subfolder but style.css is in the root:

<link rel="stylesheet" href="../style.css">  <!-- up one level -->
πŸ“ root/ πŸ“„ index.html HTML page πŸ“„ about.html HTML page πŸ“„ style.css CSS stylesheet β†’ linked one file styles all pages <link rel="stylesheet" href="style.css">

One external CSS file can style every page in the site β€” change it once, update everywhere

The cascade β€” how the browser decides which rule wins

CSS stands for Cascading Style Sheets. The cascade is the algorithm the browser uses when multiple CSS rules target the same element with the same property. The rules are:

1. Specificity wins first. Higher-specificity selectors override lower-specificity ones, regardless of order:

Element
score: 1
p { }
Class
score: 10
.text { }
ID
score: 100
#main { }
Inline
score: 1000
style=""
1 Element p { } 10 Class .intro { } 100 ID #main { } 1000 Inline style style="" p.intro#banner β†’ Element(1) + Class(10) + ID(100) = 111

Higher specificity score always wins, regardless of rule order in the file

2. Order is the tiebreaker. When two rules have the same specificity, the one that appears later in the CSS wins:

p { color: blue; }   /* defined first */
p { color: red; }    /* defined later β€” this wins */

A practical specificity example

/* In style.css */
p { color: blue; }          /* score 1 */
.intro { color: red; }     /* score 10 */
#main-text { color: green; } /* score 100 */

<!-- In HTML -->
<p id="main-text" class="intro" style="color: purple;">
  What colour is this?
</p>

Result: purple β€” the inline style has the highest specificity (1000) and overrides all stylesheet rules.

Separation of concerns

Separation of concerns is the principle that different types of code should be kept separate. In web development, this means:

  • HTML handles structure and content β€” what is on the page
  • CSS handles presentation β€” how it looks
  • JavaScript handles behaviour β€” how it responds to users (covered in WDD10)

Keeping CSS in an external file supports separation of concerns β€” the HTML file focuses purely on content, and the CSS file focuses purely on style. This makes both files shorter, cleaner, and easier to update.

Worked examples

Example 1 β€” The link tag and CSS file

Create a simple two-file setup: an HTML page and a style.css file (both in the same root folder). Show the link tag and how the CSS file connects to it.

1
Write the <link> element in the HTML page's <head>. The href value must exactly match the filename of the CSS file.
2
Create style.css in the same folder. Write CSS rules in it as normal β€” no <style> tags needed in the CSS file.
3
The browser loads the HTML, finds the <link>, fetches style.css, and applies its rules to the page.

index.html (extract from head):

<head>
  <meta charset="UTF-8">
  <title>My Site</title>
  <link rel="stylesheet" href="style.css">
</head>

style.css:

body {
  font-family: sans-serif;
  background-color: #f8f8fc;
}

h1 {
  color: #042C53;
}
Example 2 β€” Specificity in action

Given these three CSS rules for a <p> element, and an inline style β€” which colour wins?

p { color: blue; }        /* element selector β€” score 1 */
.intro { color: red; }   /* class selector β€” score 10 */
<p class="intro" style="color: green;">Hello</p>
1
Element selector (p) has specificity score 1.
2
Class selector (.intro) has specificity score 10 β€” higher than the element rule.
3
Inline style has specificity score 1000 β€” highest of all. It wins.

Result: green β€” the inline style overrides both stylesheet rules.

Example 3 β€” Two pages sharing one stylesheet

Show a two-page site where both pages link to the same style.css. Explain why this is better than two separate internal style blocks.

root/ β”œβ”€β”€ style.css β”œβ”€β”€ index.html └── pages/ └── about.html

index.html head:

<link rel="stylesheet" href="style.css">     <!-- same folder -->

pages/about.html head:

<link rel="stylesheet" href="../style.css">   <!-- up one level -->

Advantage: If you change a colour in style.css, both pages update automatically. With separate internal stylesheets, you would need to make the same change in every single HTML file β€” one change could easily be missed.

Now you try

You have this folder structure:

root/ β”œβ”€β”€ style.css β”œβ”€β”€ index.html └── pages/ └── contact.html

Task A: Write the <link> tag for index.html to use style.css.

Task B: Write the <link> tag for pages/contact.html to use the same style.css (correct relative path required).

Task C: State which rule wins for a <p class="intro"> if: p { color: navy; } and .intro { color: teal; } are both in the stylesheet.

Task A:

<link rel="stylesheet" href="style.css">

Task B:

<link rel="stylesheet" href="../style.css">

Task C: .intro { color: teal; } wins. The class selector (score 10) has higher specificity than the element selector p (score 1).

Common mistakes
βœ•
Missing rel="stylesheet" in the link element. Writing <link href="style.css"> without rel="stylesheet" means the browser does not know the file is a stylesheet and will not apply it. Both attributes are required.
βœ•
Wrong relative path to the CSS file from a subfolder. If your HTML page is in pages/ and you write href="style.css", the browser looks for pages/style.css β€” which does not exist. You need href="../style.css" to go up one level.
βœ•
Writing CSS rules inside <style> tags in a .css file. An external .css file contains only CSS rules β€” no HTML tags whatsoever. <style> tags are only used in internal stylesheets inside an HTML file.
βœ•
Assuming the last rule always wins. Order only matters as a tiebreaker when specificity is equal. A class selector (.intro) written first will still beat an element selector (p) written last, because specificity takes priority over order.
Exam tip

The exam commonly asks: "Write the HTML to link an external stylesheet". The expected answer is: <link rel="stylesheet" href="style.css"> β€” placed in the <head>. Both the rel and href attributes must be present to get full marks.

For "which rule wins" questions, score each selector: element = 1, class = 10, ID = 100, inline = 1000. The rule with the highest total score wins. If two rules have the same score, the later one in the CSS wins.

For "give one advantage of external stylesheets", a strong answer is: "The CSS is written once but can style multiple pages β€” if you update the stylesheet, all linked pages update automatically."

Task Set

Questions 1–4 are auto-checked. Questions 5–10 are self-marked β€” write your answer, then reveal the model answer to check your work.

1. Which of the following is the correct way to link an external CSS file? TYPE 1

2. In CSS specificity, which selector type has the highest score? TYPE 1

3. What does rel="stylesheet" mean in the <link> element? TYPE 1

4. In CSS, what is the cascade? TYPE 1

5. A <p> element has these three CSS rules applied. State which colour it will be and explain which specificity rule decides this. (3 marks) TYPE 2

p { color: blue; }           /* rule A */
.message { color: orange; }  /* rule B */
<p class="message" style="color: purple;">Hello</p>
Colour: purple

Specificity scores:
Rule A (p) β€” element selector, score = 1
Rule B (.message) β€” class selector, score = 10
Inline style β€” score = 1000

The inline style has the highest specificity (1000) and overrides both stylesheet rules. The element displays in purple.

Marks: correct colour (1), correct specificity scores for at least two rules (1), correct conclusion with reasoning (1).

6. State one advantage of using an external stylesheet instead of an internal stylesheet. Explain your answer. (2 marks) TYPE 2

Any one of the following (1 mark for advantage + 1 mark for explanation):

Advantage 1: One file can style multiple pages. If you update style.css, all linked pages update automatically β€” with an internal stylesheet, you would need to make the same change in every HTML file separately.

Advantage 2: It supports separation of concerns β€” the HTML file contains only structure and content, the CSS file contains only styling. Both files are shorter and easier to read.

Advantage 3: The browser can cache the external CSS file, so it only downloads it once β€” this makes page loads faster for returning visitors.

7. Write the correct <link> tag for a page at pages/contact.html that needs to link to style.css in the root folder. (2 marks) TYPE 2

<link rel="stylesheet" href="../style.css">
The ../ is essential β€” it navigates up one level from the pages/ folder to the root, where style.css is located.

Marks: correct <link> element with rel="stylesheet" (1), correct path with ../ (1).

8. Explain what "separation of concerns" means in web development. (2 marks) TYPE 2

Separation of concerns is the principle of keeping different types of code in separate files, each with a single responsibility.

In web development: HTML handles the structure and content of the page (what is there), CSS handles the presentation (how it looks), and JavaScript handles behaviour (how it responds to the user).

Keeping them separate makes each file easier to read, maintain, and update independently.

1 mark for the general principle (keep different types of code separate), 1 mark for applying it to HTML/CSS in web development.

9. A web developer has an HTML page with an internal <style> block. Rewrite it to use an external stylesheet: write (a) the modified HTML page head with the <link> tag; and (b) the contents of style.css. Both files are in the same folder. (3 marks) TYPE 2

<head>
  <meta charset="UTF-8">
  <title>Book Club</title>
  <style>
    body { background-color: #f8f8fc; }
    h1 { color: #185FA5; }
  </style>
</head>
(a) Modified HTML head:
<head>
  <meta charset="UTF-8">
  <title>Book Club</title>
  <link rel="stylesheet" href="style.css">
</head>
(b) style.css:
body { background-color: #f8f8fc; }
h1 { color: #185FA5; }
Marks: <style> block removed and replaced with <link> (1), correct rel and href in <link> (1), style.css contains only CSS rules (no HTML tags) (1).

10. A developer adds .intro { color: red !important; } to override an inline style on a <p class="intro"> element that has style="color: blue;". Explain whether this will work and why. (2 marks) TYPE 2

Yes, !important will work β€” it will override the inline style.

Normally, inline styles have the highest specificity (1000) and override all stylesheet rules. However, !important is a special CSS declaration that forces a rule to override all other rules regardless of specificity β€” including inline styles. With !important, the text colour will be red.

Note: !important should be used sparingly as it makes CSS harder to maintain. A better solution is usually to remove the inline style from the HTML entirely and let the stylesheet rule apply normally.

1 mark for stating it works; 1 mark for correctly explaining why (!important overrides specificity including inline styles).
Teacher notes β€” Shift+T to hide

Suggested timing: 2-hour Friday double. Hour 1: warm-up, notes on external stylesheets + the link element, relative paths to CSS (draw folder structure on board). Hour 2: specificity notes + worked examples, now you try, task set. Q9 is the practical consolidation question β€” ideal for the second hour.

Relative paths to CSS β€” the biggest confusion point: Draw the folder tree from Example 3 on the board before the task set. Most errors in Q7 come from pupils writing href="style.css" when the HTML is in a subfolder. Reinforce: the path must be relative to where the HTML file is, not the root.

Specificity scoring: Write the four selector types on the board with their scores (1, 10, 100, 1000). Q5 is an excellent exam-style question β€” pupils must calculate scores and identify the winner. Drill this pattern before the task set.

Q10 (!important): !important is not in the N5 spec as a required topic, but it appears in exam papers. Accept either "it works because !important overrides inline style" OR "remove the inline style from the HTML" as correct approaches.