Lesson 48 of 51 · Module 7, lesson 8 of 11 · 3 min
Connecting CSS to your page
Connect styles to your page, and see how your HTML is already ready for CSS.
What you'll learn
- The three ways to add CSS to a page
- Why a separate stylesheet file is the best choice
- How your class names and structure prepare the page for CSS
Why this matters
HTML says what things are; CSS (Cascading Style Sheets) says how they look: colours, fonts, spacing, layout. You will learn CSS in the next course. This lesson shows only how to connect it, so that your HTML is ready.
Three ways
1. A separate file (the usual way)
Create a folder css in my-website, and in it a file styles.css:
body {
font-family: sans-serif;
max-width: 40rem;
margin: 0 auto;
padding: 1rem;
}
.warning {
color: darkred;
}
Connect it in the <head> of every page:
<link rel="stylesheet" href="css/styles.css">
One file styles your whole website. Change the file, and every page changes.
2. A <style> element in the head
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled page</title>
<style>
body {
font-family: sans-serif;
max-width: 40rem;
margin: 0 auto;
padding: 1rem;
}
.warning {
color: darkred;
}
</style>
</head>
<body>
<h1>Tomato soup</h1>
<p>A warm soup for four people.</p>
<p class="warning">The soup is very hot when it leaves the pot.</p>
</body>
</html>
What you'll see: the same content in a clean, modern font, in a centred column, and the warning in dark red.
This styles only this one page. It is useful for small tests.
3. The style attribute
<p style="color: darkred;">The soup is very hot.</p>
This styles one element only. It is hard to change later, because the style is mixed into the HTML. Avoid it, except for quick tests.
Which one?
| Way | Styles | Use it |
|---|---|---|
<link rel="stylesheet"> | The whole site | Almost always |
<style> in the head | One page | Small tests, and the playground |
style="…" | One element | Rarely |
Your HTML is already ready
Everything you learned makes CSS easier:
- Classes (
class="warning") give CSS names to style. - Landmarks and structure (
<header>,<main>,<nav>) give CSS clear areas to lay out. - Correct elements mean fewer styles to fix: a real
<button>already works with a keyboard, a real list is already a list.
Try it yourself
- Select the
<style>example and choose Try it. Changedarkredtodarkblue. - In your own folder, create
css/styles.csswith the rules above, and add the<link>to all your pages. Open two of them — both change. - Break it on purpose: change the path to
/css/styles.css. The styles disappear (the leading-slash trap). Put it back.
Common mistakes
- Wrong path in the
<link>. The page shows no styles. How to spot it: developer tools → Network tab →styles.cssshows 404 or is red. Fix: check the path. - HTML in the
.cssfile, such as<style>tags. The rules are ignored. Fix: a.cssfile contains only CSS. - Choosing HTML elements for their look —
<h3>because it is small,<blockquote>to indent. Fix: choose by meaning; change the look with CSS.
Check yourself
- Which element connects a stylesheet file?
- Which way of adding CSS styles a whole website at once?
- You want text in red. Should you choose a different HTML element for it?
Answers
<link rel="stylesheet" href="…">.- A separate
.cssfile connected with<link>. - No. Keep the element that fits the meaning, and change the colour with CSS.
Next: CSS changes the look. JavaScript changes the behaviour. The next lesson connects it.

