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
- Compare CSS specificity using the tuple columns: inline, IDs, classes, and types
- 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.cssfile - 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 importance, specificity tuples, and source order
- I can explain one advantage of using an external stylesheet over an internal one
Answer before the lesson begins.
1. A page is saved as pages/about.html. Which relative path reaches images/logo.png from that page?
2. Which CSS selector targets every element with class="highlight"?
3. Where should an internal <style> block normally be placed?
Key vocabulary
.css file. Linked to HTML pages using a <link> element. One file can style many pages.<link> element<head> to connect a page to an external resource such as a CSS file. Void elements do not have a closing tag.rel attribute<link> element, rel="stylesheet" tells the browser the linked file is a CSS stylesheet.href attributehref="style.css" or href="../style.css".!important status, then specificity, then source order.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 -->
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 several declarations target the same property on the same element. For ordinary author CSS at N5 level, compare them in this sequence:
!importantstatus: an important author declaration beats a normal author declaration.- Specificity: if the declarations have the same importance status, compare their specificity tuples from left to right.
- Source order: if specificity is also equal, the declaration that appears later wins.
The browser's full cascade also considers where styles come from (origin) and cascade layers. Those details are beyond this lesson; the sequence above is the working method for the ordinary author CSS used here.
Specificity uses columns, not a decimal score. Write four separate counts as (inline styles, IDs, classes/attributes/pseudo-classes, types/pseudo-elements):
(0, 0, 0, 1)p { }(0, 0, 1, 0).text { }(0, 1, 0, 0)#main { }(1, 0, 0, 0)style=""Compare the columns from left to right; the first different column decides. For example, p.intro#banner has (0, 1, 1, 1). The counts are never added together. Even 11 classes, (0, 0, 11, 0), do not beat one ID, (0, 1, 0, 0), because the ID column is compared first.
Source order is the final tiebreaker. When two declarations have the same importance status and specificity, the one that appears later wins:
p { color: blue; } /* defined first */ p { color: red; } /* defined later β this wins */
This works across internal and external stylesheets too. Their type does not decide the winner. Here both p selectors have equal specificity, so the internal rule wins only because its <style> block appears after the external <link>:
/* style.css β loaded first */ p { color: blue; } <!-- In the HTML head --> <link rel="stylesheet" href="style.css"> <style> p { color: red; } /* later β this wins */ </style>
If the <link> appeared after the <style> block instead, the equally specific external rule would be later and blue would win.
A practical specificity example
/* In style.css */ p { color: blue; } /* (0, 0, 0, 1) */ .intro { color: red; } /* (0, 0, 1, 0) */ #main-text { color: green; } /* (0, 1, 0, 0) */ <!-- In HTML --> <p id="main-text" class="intro" style="color: purple;"> What colour is this? </p>
All four declarations are normal (none is !important), so compare specificity. Result: purple β the inline style's (1, 0, 0, 0) beats the stylesheet selectors.
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
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.
<link> element in the HTML page's <head>. The href value must exactly match the filename of the CSS file.style.css in the same folder. Write CSS rules in it as normal β no <style> tags needed in the CSS file.<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; }
Given these three CSS rules for a <p> element, and an inline style β which colour wins?
p { color: blue; } /* type selector β (0, 0, 0, 1) */ .intro { color: red; } /* class selector β (0, 0, 1, 0) */
<p class="intro" style="color: green;">Hello</p>
!important status is equal.p is (0, 0, 0, 1), while .intro is (0, 0, 1, 0).(1, 0, 0, 0). Its first column wins, so no source-order tiebreaker is needed.Result: green β the inline style overrides both stylesheet rules.
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.
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.
You have this folder structure:
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. Its specificity (0, 0, 1, 0) beats the type selector p at (0, 0, 0, 1) when the columns are compared from left to right.
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.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.<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..intro) written first still beats a type selector (p) written last because specificity is compared before source order.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 in ordinary author CSS, compare !important status first. If that is equal, write specificity as (inline, IDs, classes/attributes/pseudo-classes, types/pseudo-elements) and compare left to right β never total the columns. If the tuples are equal, the later declaration 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."
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. If the declarations have the same !important status, which has the highest specificity tuple? 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>
Specificity tuples:
Rule A (
p) β type selector, (0, 0, 0, 1)Rule B (
.message) β class selector, (0, 0, 1, 0)Inline style β
(1, 0, 0, 0)All three declarations are normal, so compare their tuples left to right. The inline style wins at the first column, so the element displays in purple.
Marks: correct colour (1), correct specificity tuples 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
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
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>
<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
!important will work β it will override the inline style.The stylesheet declaration is important, while the inline declaration is normal. For ordinary author CSS, importance is compared before specificity, so the important stylesheet declaration wins and the text is red.
This does not mean
!important automatically beats every other declaration. If two important declarations compete, the remaining cascade rules β including origin/layer, specificity, and source order β decide between them.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 explaining that an author
!important declaration beats the normal inline declaration in this example.
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 tuples: Write the four columns on the board as (inline, IDs, classes/attributes/pseudo-classes, types/pseudo-elements). Q5 is an excellent exam-style question β pupils must compare the columns from left to right and identify the winner. Reinforce that the counts are not added.
Q10 (!important): !important is not in the N5 spec as a required topic, but it appears in exam papers. Accept either "the author !important declaration beats the normal inline declaration in this example" OR "remove the inline style from the HTML" as correct approaches.