Sub topic 5
External Styles: Linking a Stylesheet
This chapter assumes you know basic HTML structure (html, head, body), can save text files and open them in a browser, and understand relative file paths. You have already seen inline styles and the style element. Here you will move CSS into its own file and link it from your pages.
Why a separate file
An external stylesheet is a plain text file that contains only CSS rules. Many HTML pages can point to the same file. When you change a rule in that file, every page that links to it changes too. The browser can also store the file and reuse it on later page loads, which you will see in a later chapter.
Linking the file
You attach an external stylesheet with a link element inside the head.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>External styles</title>
<link rel="stylesheet" href="css/site.css">
</head>
<body>
<p>This page gets its styles from css/site.css.</p>
</body>
</html>The link element has two attributes that matter here:
rel="stylesheet"tells the browser what kind of resource this is. Without it, the browser does not treat the file as CSS.hrefgives the path to the file. It works like the path in a normal link.
link is a void element, so it has no closing tag and no content.
The stylesheet file
The file contains only CSS. Do not put a style element or any other HTML tag in it.
/* css/site.css */
body {
font-family: system-ui, sans-serif;
margin: 2rem;
}
p {
color: darkgreen;
}Comments in CSS use /* ... */. Lines starting with // are not CSS comments.
Getting the path right
A relative href is resolved from the location of the HTML file that contains the link. With this layout:
project/
index.html
css/
site.css
blog/
post.htmlthe paths are different for each page:
| Page | Correct href |
|---|---|
| `index.html` | `css/site.css` |
| `blog/post.html` | `../css/site.css` |
The .. means "go up one folder". A path starting with /, such as /css/site.css, is resolved from the root of the site. That form works from any page, but only when the site is served from a web server's root, not when you open files directly from disk.
flowchart TD
A[Browser loads page.html] --> B[Parser reaches link element in head]
B --> C[Browser requests the href URL]
C --> D{File found?}
D -- Yes --> E[Rules are applied to the page]
D -- No --> F[Request fails and no styles are applied from it]Restricting a stylesheet by media
The media attribute limits a stylesheet to certain conditions. This file applies only when printing:
<link rel="stylesheet" href="print.css" media="print">You can use several link elements on one page, for example one for general styles and one for print.
Combining external styles with other methods
External styles take part in the same cascade as inline and internal styles. If a link and a style element set the same property with equal specificity, the one that appears later in the HTML wins. A style attribute on an element beats those selector rules.
<head>
<link rel="stylesheet" href="site.css">
<style>
p { color: blue; }
</style>
</head>
<body>
<p>Blue, if site.css sets p to another color, because the style element comes later and has equal specificity.</p>
<p style="color: red;">Red, because the inline declaration beats the selector rules above.</p>
</body>If you put the link after the style element, the file's rule would win instead. The order of link and style elements in the head therefore matters.
Common errors
- Wrong path. The page shows no styling. Open the browser's developer tools, check the Network tab, and look for a 404 response on the CSS file.
- Missing
rel="stylesheet". The file may download, but its rules are not applied. - HTML inside the CSS file. A
<style>tag insite.cssmakes the first rule invalid, so that rule is skipped. - Stale cache. You edited the file but see the old styles. Reload the page without the cache (usually Ctrl+Shift+R or Cmd+Shift+R).
- Wrong case on a server. Many servers treat
Site.cssandsite.cssas different files, even if your own computer does not.
Exercises
Exercise
Your file blog/post.html needs the stylesheet at css/site.css, and both blog and css sit directly inside the project folder. What should the href be?
Show answer
../css/site.css. The .. moves up from the blog folder to the project folder, and then the path goes into css.
Exercise
A page has <link rel="stylesheet" href="site.css"> followed by <style> p { color: blue; } </style>. The file site.css has p { color: green; }. What color are paragraphs without a style attribute, and what changes if you swap the order of the two elements?
Show answer
They are blue, because the later rule wins when specificity is equal. After swapping the order, they are green.