External Stylesheets
The style element has one drawback: its rules apply only to the page they are written in. A website usually has many pages, and they should all look the same. You would have to copy the style element into every page, and every change of the look would mean changing every page.
That’s why the third way is the most common one: an external stylesheet. You write the rules into a separate file with the extension .css, and each page links to it with the link element, just like you did in the first exercise:
<link rel="stylesheet" href="style.css">All the pages then share one stylesheet. When you change a rule in it, the change shows up on all of them at once. In addition, the content stays in the HTML file and the styles in the CSS file, so both are easier to read.
A CSS file contains only rules, without the <style> tags, which belong to HTML:
p {
color: green;
}We’ve added an empty file style.css to the editor. Let’s move our rule into it.
Instructions
- 1
Move the rule for
pfrom thestyleelement into the filestyle.css: select it, cut it (Ctrl+X, on a MacCmd+X) and paste it intostyle.css(Ctrl+V, on a MacCmd+V). Move only the rule, without the<style>tags. - 2
In
index.html, delete thestyleelement, which is empty now, including its<style>and</style>tags. - 3
In
head, below thetitleelement, add thelinkelement that links the page tostyle.css. After you click Run, the paragraphs will be green again.