Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
CSS Basics  ›  Lesson

External CSS

External CSS 12 minAn external .css file linked from each page lets one edit restyle a whole site, and the browser caches it for later pages.

External CSS

Harbour Café's site now has a home page, a menu page, a contact page and the festival page, and the owner wants the same fonts and colours everywhere. The answer is one stylesheet file that every page links to.

The rule

One file, linked from every page, changed once.

An external stylesheet is a plain text file ending in .css that holds only CSS rules, with no <style> tags and no HTML:

/* styles.css */
body {
  font-family: Georgia, serif;
}
.price {
  color: green;
}

Each page connects to it with a link element in its head:

<head>
  <title>Harbour Café</title>
  <link rel="stylesheet" href="styles.css">
</head>
  • rel="stylesheet" tells the browser what the linked file is for.
  • href is the file's address. A relative path such as styles.css is looked up relative to the page's own location.

Why this is the default choice:

  • One edit restyles the whole site.
  • Caching: the browser downloads styles.css once and reuses it on every page, so later pages load faster.
  • Clean HTML: pages hold content and meaning; the look lives in its own file.

A page can link several stylesheets, for example a shared base.css and a page-specific festival.css. They are read in the order the link elements appear.

The shortcut

Treat external CSS as the default, then check the path from the page's point of view: start in the folder that contains the HTML file and follow the href from there.

Linking the menu pageStep 1 of 4
  1. Locate the page

    The menu page lives at menu/index.html. The stylesheet lives at the site root: styles.css.

The classic failure is not an error message at all: a page that suddenly looks like plain 1995 text almost always means the stylesheet link is broken.

Drills

Answer fast — trust the rule.

quick drill

Which tag correctly links an external stylesheet?

quick drill

What should a .css file contain?

quick drill

The page menu/index.html links href="styles.css", but the file is at the site root. What happens?

quick drill

Why do later pages on the site often load their styles faster?

Remember

  • One .css file, linked from every page with <link rel="stylesheet" href="...">.
  • Follow the href from the page's own folder.
  • An unstyled page usually means a broken link path.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy the link broke

The café's contact page at contact/index.html shows up completely unstyled, while the home page looks fine. Both use <link rel="stylesheet" href="styles.css">, and the file is at the site root. Explain why, and give a fix.