How to Add CSS to HTML

Inline styles, style tags and external stylesheets, and which one to use.

Lesson 3 of 49Beginner11 min read

There are three ways to attach CSS to an HTML page. They all work, but one is the clear choice for real websites. Knowing all three helps you read other people's code and fix it, because you'll meet every one of them in the wild.

The method you choose affects more than tidiness. It decides whether your styles can be shared between pages, how fast later pages load, and how easy it is to override a style when the design changes.

Method 1: inline styles

Put CSS directly on one element with the style attribute:

HTML
<p style="color: red; font-weight: bold;">Sold out!</p>

Good for: a quick one-off test. Downside: it only affects that single element, mixes design into the content, can't use hover effects or media queries, and is very hard to override from a stylesheet.

You'll often see inline styles in HTML emails and in code generated by JavaScript, but avoid them in your own pages.

Method 2: internal stylesheet

Put your rules inside a <style> element, usually in the <head>:

HTML
<style>
  p { color: navy; }
</style>

Good for: single-page demos and examples, like many on this site. Downside: the styles can't be shared, so a second page needs its own copy.

Using more than one stylesheet

A page can link several stylesheets. They're applied in the order they appear, so a common pattern is a general file first and more specific files after it:

HTML
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="contact-page.css">

Because the second file comes later, its rules win when the two files disagree. Keep the number of files small, though, because each one is a separate download.

Adding CSS Syntax

The three methods

<!-- external (best) -->
<link rel="stylesheet" href="css/styles.css">

<!-- internal -->
<style> h1 { color: teal; } </style>

<!-- inline -->
<h1 style="color: teal;">Hi</h1>

Adding CSS Summary

  • CSS can be added inline, in a <style> element or in an external .css file.
  • External stylesheets, linked with <link rel="stylesheet">, are best for real sites.
  • One external file can style every page and is cached by the browser.
  • When equally specific rules conflict, the last one wins.
  • Avoid inline styles; they're hard to reuse and hard to override.

Adding CSS Examples

Each example runs in the page. Edit the code and press Run to see what changes.

Example 1

Internal and inline together

The internal rule makes all paragraphs blue. The inline style on the second paragraph overrides it.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Two ways to add CSS</title>
  <style>
    p { color: royalblue; font-family: sans-serif; }
  </style>
</head>
<body>
  <p>I'm blue from the style tag.</p>
  <p style="color: crimson;">I'm red, because my inline style wins.</p>
</body>
</html>
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Lines 6 to 8An internal stylesheet in the head styles every paragraph.
  2. Line 11Uses the internal style.
  3. Line 12The inline style attribute overrides the colour for just this paragraph. The font from line 7 still applies.
Example 2

What an external setup looks like

In a real project you'd have two files. Here's what each contains. (The editor runs them together, just like a browser would.)

/* styles.css */
.brand {
  color: #17a865;
  font-family: Georgia, serif;
  letter-spacing: 2px;
}
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Line 1The CSS file holds nothing but CSS rules, with no <style> tags.
  2. Lines 2 to 6A rule for the brand class. Every page that links styles.css gets it.

Common Adding CSS Mistakes

Style tags inside a .css file

Don't
<style>
  body { background: beige; }
</style>
Do
body { background: beige; }

A .css file contains only CSS. HTML tags in it break the first rule.

Wrong link attributes

Don't
<link href="styles.css">
<style src="styles.css"></style>
Do
<link rel="stylesheet" href="styles.css">

rel="stylesheet" is required, and <style> doesn't load files.

Adding CSS in Real Projects

How sites are organised

Professional sites usually load one or two external stylesheets, sometimes split into files like base.css, layout.css and components.css that a build tool combines. Inline styles are mostly added by JavaScript for things that change on the fly, like the position of a dragged element.

Adding CSS Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Link the external stylesheet.

<link ="stylesheet" ="main.css">
Multiple choiceeasy

Which way of adding CSS is best for a website with many pages?

Multiple choicemedium

A paragraph has an inline style color: red and the stylesheet says p { color: blue; }. What colour is it?

Fix the bugmedium

The internal stylesheet isn't working because it's not valid. Fix it so the button is green with white text.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Shop</title>
  <style src="">
    button { background: green; color: white }
  </stlye>
</head>
<body>
  <button>Buy</button>
</body>
</html>
ResultEdit the code, then press Run or Ctrl+Enter
Checks
  • The button is green
  • The button text is white

Adding CSS Quiz

3 questions to check what you learned.

  1. Which attribute adds inline CSS?

    • css
    • style
    • class
    • design
  2. Where should the <link rel="stylesheet"> usually go?

    • Inside <head>
    • At the end of <body>
    • Inside each element
    • In the CSS file
  3. Why are external stylesheets faster for multi-page sites?

    • They're compressed automatically
    • They load before HTML
    • The browser caches the file and reuses it on every page
    • They skip the cascade

Adding CSS Challenge

Override with care

The page already makes all list items grey. Using only the CSS tab (no inline styles):

  1. Add a rule that makes the li with class today bold and #17a865
li { color: gray; }
ResultEdit the code, then press Run or Ctrl+Enter
Checks
  • Today is green
  • Today is bold
  • Other items stay gray
  • No inline styles were used