CSS Tutorial · CSS Basics · Lesson 2 of 19

How to Add CSS

External stylesheets, style elements and inline styles — and which to use.

Three ways to add CSS

1. External stylesheet (recommended). Put CSS in a .css file and link it from the <head>. Every page shares it and browsers cache it:

<link rel="stylesheet" href="/css/styles.css">

2. Internal <style> element — CSS for one page (used in this tutorial's examples so they're self-contained).

3. Inline style attribute — styles for one element. Hard to maintain and overrides most other rules; avoid it except for dynamic values set by JavaScript.

Example 1Try it yourself
<style>
  /* Internal stylesheet */
  p { color: #0f766e; font-family: system-ui, sans-serif; }
</style>
<p>Styled by the &lt;style&gt; element.</p>
<p style="color: #b91c1c;">Inline style wins over the stylesheet.</p>

Comments

CSS comments use /* ... */.

Test yourself

Which way of adding CSS is best for a multi-page website?

  1. Inline styles
  2. A <style> element on each page
  3. An external stylesheet
  4. JavaScript
Show answer

Answer: An external stylesheet