On this page10 sections
  1. Learning Objectives
  2. 1. Inline CSS
  3. 2. Internal CSS: a <style> Block
  4. 3. External CSS: a Separate File
  5. 4. Common CSS Properties
  6. 5. Which Should I Use?
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 009: Adding CSS to HTML

Category2: Text & Styles
Lecture009 of 048
Estimated video length10 min
TargetHTML5 · any modern browser
LevelBeginner

CSS (Cascading Style Sheets) controls how HTML looks. You have already used it inside the style attribute. There are two better places to put it: a <style> block in the page, or a separate .css file shared by every page.


Learning Objectives

  • Add CSS inline, internally with <style>, and externally with <link>
  • Explain why an external stylesheet is the best choice for a website
  • Style colours, fonts, sizes, borders, padding and margin with CSS rules

1. Inline CSS

The style attribute from Lecture 006 styles one element.

html
<h1 style="color: navy;">Inline style</h1>

2. Internal CSS: a <style> Block

A <style> block, usually in the <head>, holds rules that style the whole page.

Example

html
<!DOCTYPE html>
<html>
<head>
  <style>
    body { background-color: #f8f9fa; }
    h1   { color: navy; }
    p    { color: #495057; }
  </style>
</head>
<body>
  <h1>Internal CSS</h1>
  <p>Every paragraph on this page is grey.</p>
  <p>Without repeating the style on each one.</p>
</body>
</html>

A rule is a selector (which elements) followed by { property: value; }.


3. External CSS: a Separate File

Write the rules in a file, for example styles.css:

css
body { background-color: #f8f9fa; }
h1   { color: navy; }
p    { color: #495057; }

Then link it from the <head> of every page:

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

One file now styles the whole site. Change a colour once and every page updates.


4. Common CSS Properties

Example

html
<style>
  h2 { color: #c2255c; font-family: Verdana, sans-serif; font-size: 28px; }
  p.box {
    border: 2px solid #c2255c;   /* a line around the element */
    padding: 16px;               /* space inside the border */
    margin: 20px;                /* space outside the border */
  }
</style>

<h2>Styled with CSS</h2>
<p class="box">Border, padding and margin give this paragraph room to breathe.</p>
PropertyControls
colorText colour
font-familyTypeface
font-sizeText size
borderA line around the element
paddingSpace between the content and the border
marginSpace outside the border

5. Which Should I Use?

MethodUse it for
Inline styleA one-off quick test
<style> blockA single page, or examples like the ones in this course
External .css fileReal websites: one file for every page

The CSS course goes much further: layouts, animation, responsive design and more.


Common Beginner Mistakes

1. <link> in the body. It belongs in the <head>.

2. Wrong path. href="style.css" won't find a file called styles.css, or one in a css/ folder.

3. HTML inside a CSS file. A .css file contains only rules, with no <style> tags.

4. Missing braces. Every rule needs { and }.


Practice Questions

  1. Name the three ways of adding CSS to HTML.
  2. Which element holds internal CSS, and where does it usually go?
  3. Write the tag that links a stylesheet called main.css.
  4. Which is best for a website with 20 pages, and why?
  5. Which property adds space inside the border?
  6. Write a rule that makes every <h1> dark green.

Practice Answers

  1. Inline (style attribute), internal (<style> block), external (linked .css file).
  2. <style>, in the <head>.
  3. <link rel="stylesheet" href="main.css">
  4. An external stylesheet, because one file styles all 20 pages.
  5. padding
  6. h1 { color: darkgreen; }

Key Takeaways

  • CSS can be inline, internal (<style>) or external (<link rel="stylesheet">).
  • External files are the right choice for real sites.
  • A rule = selector + { property: value; }.
  • color, font-*, border, padding and margin are the first properties to learn.