Cascading Style Sheets (CSS) separate presentation from content. The cascade decides which rule wins when several rules apply to the same element.
<head>)<style> block)style="…")!important declarations – override the normal cascade, but a later rule with a higher specificity and !important will still win.Specificity is expressed as a three‑part number (a‑b‑c):
a – number of inline style attributes (0 or 1)b – number of ID selectorsc – number of class, attribute or pseudo‑class selectorsExample:
/* selector */ /* specificity */
p { … } /* 0‑0‑1 */
.menu { … } /* 0‑0‑1 (class) → 0‑0‑1 */
#header { … } /* 0‑1‑0 */
#main .nav li.active { … } /* 0‑1‑2 (1 ID + 2 classes) */
style="color:#c00;" /* 1‑0‑0 (inline)
<p style="color:#c00; font-weight:bold;">Important notice</p>
<head>
<style>
.highlight { color:#c00; font-weight:bold; }
</style>
</head>
.css file)Linking the stylesheet (the same file must be linked from every HTML page you use in the exam):
<head>
<link rel="stylesheet" href="css/styles.css">
</head>
Relative vs absolute paths
href="css/styles.css"href="https://www.example.com/css/styles.css"Exam tip: For Paper 3 you must use **relative paths** for locally stored files; absolute URLs are only allowed for external resources.
| Selector type | Syntax | Typical use | Specificity (a‑b‑c) |
|---|---|---|---|
| Element selector | p { … } |
Apply a rule to every occurrence of an element. | 0‑0‑1 |
| Class selector | .menu { … } |
Reusable styling for a group of elements. | 0‑0‑1 (or 0‑0‑n for n classes) |
| ID selector | #header { … } |
Unique styling for a single element (must be unique on a page). | 0‑1‑0 |
| Inline style | style="…" |
Quick, one‑off changes (e.g., email HTML). | 1‑0‑0 (highest without !important) |
<!-- CSS (external or internal) -->
<style>
.button { background:#006; color:#fff; padding:8px 12px; }
#submitBtn { border:2px solid #000; }
</style>
<!-- HTML -->
<button class="button">Cancel</button>
<button id="submitBtn" class="button">Submit</button>
These are the elements you must be able to create in the exam. The right CSS properties are shown so you can link content and presentation quickly.
| HTML element | Key CSS properties | Typical class / ID |
|---|---|---|
<h1>…</h1> |
font-family, font-size, color, margin‑bottom | .page-title |
<p>…</p> |
line-height, text-align, color | .intro |
<a href="">…</a> |
color, text-decoration, :hover colour | #mainNav a |
<img src="" alt=""> |
border, max-width, display, float | .align-right |
<table>…</table> |
border-collapse, width, margin, background‑color for th |
.data-tbl |
<form>…</form> |
margin, padding, display (grid/flex) for layout of controls | .login-form |
<video>…</video> |
width, height, border | .media |
| Property | Purpose | Example value |
|---|---|---|
color | Text colour | #333 or rgb(0,0,255) |
background | Background colour / image | #f0f0f0 or url('img/bg.jpg') no-repeat center/cover |
font-family | Font face | Arial, sans-serif |
font-size | Size of text | 16px or 1.2em |
font-weight | Boldness | bold or 700 |
line-height | Space between lines | 1.5 |
text-align | Horizontal alignment | center |
margin | Space outside the element | 10px 0 |
padding | Space inside the element | 5px 10px |
border | Border style | 1px solid #999 |
width / height | Dimensions | 200px or 100% |
display | Block, inline, inline‑block, flex, grid | flex |
float | Float element left/right | right |
clear | Control float behaviour | both |
list-style | Bullets / numbers for lists | disc inside |
position | static, relative, absolute, fixed, sticky | relative |
z-index | Stacking order (used with positioned elements) | 10 |
Media queries let you apply different styles depending on the device’s width, orientation, resolution, etc. They are often used to meet e‑safety requirements such as ensuring readable text on small screens.
@media screen and (max-width: 600px) {
/* e‑safety: larger, high‑contrast text on mobiles */
body { font-size:1.2rem; line-height:1.6; }
.nav { flex-direction:column; }
.hero { background-image:url('img/hero-mobile.jpg'); }
}
.btn-primary, .card-header).block__element--modifier. This makes it obvious which elements a rule targets and reduces accidental overrides.#id if the element may be referenced later.!important.@media queries in the external stylesheet.In the exam you will usually create at least two HTML files (e.g., index.html and about.html). Both must contain the same <link rel="stylesheet" href="css/styles.css"> line in the <head>. This demonstrates that a single stylesheet can control the appearance of an entire website.
| Aspect | Inline style | Class selector | ID selector | External stylesheet |
|---|---|---|---|---|
| Definition location | Inside the HTML tag | In a CSS rule set (.class) |
In a CSS rule set (#id) |
Separate .css file (or <style> block) |
| Re‑usability | Single element only | Multiple elements | One element per page | All pages that link the file |
| Specificity | Highest (1‑0‑0) | Low (0‑0‑1 per class) | Medium (0‑1‑0) | Depends on selector type (usually low) |
| Maintainability | Poor – edit each element | Good – edit class once | Good – edit ID once | Best – one file controls many pages |
| Typical exam use | Quick test, email HTML | Standard page styling (tables, navigation, forms) | Unique sections (header, footer) or when higher specificity is required | Final projects; mandatory for Paper 3 (multiple pages) |
Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.