Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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. 1

    Move the rule for p from the style element into the file style.css: select it, cut it (Ctrl+X, on a Mac Cmd+X) and paste it into style.css (Ctrl+V, on a Mac Cmd+V). Move only the rule, without the <style> tags.

  2. 2

    In index.html, delete the style element, which is empty now, including its <style> and </style> tags.

  3. 3

    In head, below the title element, add the link element that links the page to style.css. After you click Run, the paragraphs will be green again.

Back

5/6

  1. 1. Introduction to CSS
  2. 2. Inline Styles
  3. 3. Quiz: Inline Styles
  4. 4. Styles in the Document
  5. 5. External Stylesheets
  6. 6. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.