Estimated time: 15 minutes reading, plus about 25 minutes of practice.
Downloads :
- css_lesson1_cheat_sheet.pdf is a 2–3 page summary of the lesson: rule anatomy, the three methods, common properties and a troubleshooting table.
- css_lesson1_glossary.csv has 19 terms with category, definition and example. You can import it into flashcard apps or a spreadsheet.
- css_lesson1_practice_tracker.csv is a checklist of the lesson's tasks. Open it in Excel or Google Sheets and tick off each one.
Learning goals: By the end, you'll be able to:
- explain what CSS is and why it's separate from HTML
- read and write a CSS rule
- add CSS in three ways and know which one to use
- link an external stylesheet
- use comments and browser DevTools to test and debug styles
Part 1: What is CSS? (3 min)
CSS stands for Cascading Style Sheets. In the HTML course, you built pages with headings, lists, tables and forms. Without CSS, they look plain: black text on white, in Times-style type, stacked from top to bottom. That's the browser's default styling.
CSS lets you change that. It controls:
- colour and backgrounds
- fonts and text
- spacing and borders
- layout (where things sit on the page)
- responsiveness (how pages adapt to phones and tablets)
Why is it separate from HTML?
| HTML | CSS |
|---|---|
| What the content is | How the content looks |
<h1>, <p>, <ul> | color, font-size, margin |
| One structure | Many possible designs |
Separation brings real benefits:
- One change, whole site. Edit one CSS file and every page updates.
- Cleaner HTML. Your markup stays readable.
- Different looks, same content. The same HTML can look completely different on a phone, a desktop or in print.
Think of a house again. HTML is the walls and rooms. CSS is the paint, furniture and curtains. You can redecorate without rebuilding.
What does "cascading" mean?
Several rules can target the same element, and the browser must choose between them. "Cascade" is the system that decides who wins. Lesson 3 covers it in depth. For now, remember that later rules usually override earlier ones when they conflict.
Part 2: Three ways to add CSS (4 min)
Method 1: Inline CSS
Written inside an element's style attribute.
<p style="color: red; font-size: 20px;">This text is red.</p>- It only affects that one element.
- It's quick for tests, but messy and hard to maintain.
- It's also hard to override later.
Method 2: Internal CSS
Written in a <style> element in the <head>.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Internal CSS</title>
<style>
h1 {
color: darkgreen;
}
p {
font-size: 18px;
}
</style>
</head>
<body>
<h1>Hello</h1>
<p>This page is styled with internal CSS.</p>
</body>
</html>- It affects the whole page.
- It's fine for a single-page demo, but it can't be shared between pages.
Method 3: External CSS (best practice)
Written in a separate .css file and linked from the HTML.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello</h1>
<p>This page is styled with an external stylesheet.</p>
</body>
</html>styles.css
h1 {
color: darkgreen;
}
p {
font-size: 18px;
}Notes on the <link> element:
-
rel="stylesheet"says what kind of file it is. -
hrefis the path to the file, using the same path rules as links and images. -
It belongs in
<head>. -
Don't put
<style>tags inside the.cssfile. It holds CSS only.
Which should you use?
| Method | Reusable? | Easy to maintain? | Use it for |
|---|---|---|---|
| Inline | No | No | Quick tests |
| Internal | One page | Medium | Single-page demos |
| External | Yes, many pages | Yes | Real websites |
From here on, we'll use external CSS.
Part 3: Anatomy of a CSS rule (4 min)
p {
color: darkblue;
font-size: 18px;
}| Part | Example | Meaning |
|---|---|---|
| Selector | p | Which elements to style |
| Declaration block | { ... } | Everything inside the braces |
| Declaration | color: darkblue; | One property and value pair |
| Property | color | What to change |
| Value | darkblue | What to change it to |
Syntax rules to remember:
-
A colon
:separates property and value. -
A semicolon
;ends each declaration. -
Curly braces
{ }wrap the declarations. - CSS ignores extra spaces and line breaks, but indent two spaces for readability.
Styling several elements
Separate selectors with commas to apply one rule to all of them.
h1, h2, h3 {
color: navy;
}Your first useful properties
body {
font-family: Arial, sans-serif;
background-color: #f9fafb;
color: #1f2937;
}
h1 {
text-align: center;
color: #1d4ed8;
}
p {
font-size: 18px;
}
img {
border: 2px solid black;
}| Property | What it does | Example values |
|---|---|---|
color | Text colour | red, #ff0000, rgb(255,0,0) |
background-color | Background colour | lightyellow, #eef |
font-family | Typeface | Arial, sans-serif |
font-size | Text size | 16px, 1.2rem |
text-align | Horizontal alignment | left, center, right |
border | Border (width, style, colour) | 2px solid black |
We'll cover colour in Lesson 4, typography in Lesson 5 and sizing units in Lesson 6. For now, just try values and see what happens.
Inheritance: a small preview
Some properties pass from parent to child. If you set color on body, headings and paragraphs inside it inherit that colour unless you override it. That's why the body rule above changes the whole page.
Comments
/* Page-wide styles */
body {
font-family: Arial, sans-serif;
}
/* Heading styles
h1 {
color: navy;
}Comments use /* ... */ and can span several lines. The second comment above is missing its closing */, which would break the rule below it.
Part 4: Common mistakes and how to spot them (2 min)
| Mistake | Example | Effect |
|---|---|---|
| Missing semicolon | color: red then font-size: 20px; | The next line may stop working |
Using = instead of : | color = red; | Declaration ignored |
| Misspelt property | colour: red; | Ignored (CSS uses US spelling, color) |
| Missing closing brace | p { color: red; | Later rules break |
Wrong href path | href="style.css" when the file is styles.css | No styles load |
<link> in <body> | Placed far down the page | Works inconsistently and may flash unstyled content |
Forgetting px | font-size: 18; | Ignored (units are needed except for 0) |
CSS is forgiving. When something is wrong, it ignores the broken line and carries on, so mistakes can be silent.
Part 5: Debugging with browser DevTools (2 min)
Every browser has built-in tools to inspect and edit CSS live.
- Right-click any element and choose Inspect.
- The Elements panel shows the HTML, and the Styles panel shows the CSS rules that apply.
- Untick a declaration to switch it off temporarily.
- Click a value and type a new one to test changes instantly.
- A struck-through declaration means it was overridden or invalid.
Changes in DevTools are temporary. Refresh and they're gone, so copy good changes into your .css file.
Key takeaways
- CSS controls appearance, while HTML controls structure.
-
A rule is a selector plus declarations, each a
property: value;pair. - There are three ways to add CSS. Use external stylesheets for real sites.
-
Link the stylesheet in
<head>with<link rel="stylesheet" href="...">. - CSS silently ignores mistakes, so check spelling, colons, semicolons and braces first.
- DevTools lets you test and debug styles live.
Exercises (25 min)
1. Identify (2 min). In this rule, name the selector, property and value.
h2 {
color: tomato;
}Answer: selector h2, property color, value tomato.
2. Fix the errors (5 min).
p {
color = blue
font-size: 18
}
h1
text-align: center;
}
body {
background-colour: lightgrey;
}Hints: replace = with :, add semicolons, add px to the size, add the missing { after h1, and use background-color.
3. Build it: three methods (8 min).
-
Create a folder called
css-coursewithindex.html(use the standard skeleton from the HTML course). - Add a heading and two paragraphs.
- Style one paragraph with inline CSS.
-
Add an internal
<style>block that styles the<h1>. -
Create
styles.css, link it and move all styles into it. Delete the inline and internal versions.
4. Mini project: a styled profile (8 min). Using your index.html, add an <h1> with your name, a short intro paragraph, a list of three hobbies and an image. In styles.css:
-
set a
font-familyandbackground-coloronbody -
centre the
<h1>and give it a colour -
set a
font-sizefor paragraphs -
add a
borderto the image - add a comment above each rule
5. Challenge (2 min). Open your page, right-click the heading, choose Inspect and change its colour in the Styles panel. Then untick text-align to see it switch off. Refresh the page to see everything reset.
Lesson 1 checkpoint
- I can explain what CSS does and why it's separate from HTML.
- I can name the parts of a CSS rule.
- I know the three ways to add CSS and why external is best.
-
I can link a stylesheet from
<head>. - I can find and fix simple syntax errors.
- I can inspect an element with DevTools.