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:

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:

html
<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

html
<!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.

The page "Tomato soup" in a sans-serif font inside a centred column, with the warning sentence "The soup is very hot when it leaves the pot." in dark red.

This styles only this one page. It is useful for small tests.

3. The style attribute

html
<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?

WayStylesUse it
<link rel="stylesheet">The whole siteAlmost always
<style> in the headOne pageSmall tests, and the playground
style="…"One elementRarely

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

  1. Select the <style> example and choose Try it. Change darkred to darkblue.
  2. In your own folder, create css/styles.css with the rules above, and add the <link> to all your pages. Open two of them — both change.
  3. 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.css shows 404 or is red. Fix: check the path.
  • HTML in the .css file, such as <style> tags. The rules are ignored. Fix: a .css file 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

  1. Which element connects a stylesheet file?
  2. Which way of adding CSS styles a whole website at once?
  3. You want text in red. Should you choose a different HTML element for it?

Answers

  1. <link rel="stylesheet" href="…">.
  2. A separate .css file connected with <link>.
  3. 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.

Chat with us on WhatsApp