On this page11 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. A CSS Rule
  4. 2. Three Ways to Add CSS
  5. 3. Comments in CSS
  6. 4. The Cascade: Which Rule Wins?
  7. 5. When CSS Has an Error
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 001: What Is CSS & How to Add It

Category1: CSS Basics
Lecture001 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelBeginner

HTML builds the page; CSS decides how it looks. One CSS rule can colour every heading on a whole website at once. This lecture shows what a rule looks like and the three places you can put it.


Prerequisites

  • HTML elements, attributes and classes (HTML course, Lectures 003–004)

Learning Objectives

  • Read and write a CSS rule: selector, property and value
  • Add CSS inline, in a <style> block, and in a separate .css file
  • Explain the "cascade": which rule wins when two disagree

1. A CSS Rule

output
h1 { color: teal; font-size: 32px; }
│     │      │
│     │      └── value
│     └───────── property
└─────────────── selector (which elements to style)

Each property: value; pair is a declaration. A rule can hold as many as you like, each ending with ;.

Example

html
<style>
  h1 { color: teal; font-size: 32px; }
  p  { color: #555; }
</style>

<h1>Welcome to CSS</h1>
<p>Every paragraph on this page is grey.</p>
<p>Including this one. One rule styled them all.</p>

2. Three Ways to Add CSS

Inline: on one element

html
<p style="color: crimson;">Only this paragraph is red.</p>

Quick for a test, but you have to repeat it on every element.

Internal: a <style> block in the page

html
<style>
  p { color: crimson; }
</style>
<p>Every paragraph on this page is red.</p>
<p>This one too.</p>

External: a separate file (the best choice)

Put the rules in a file such as styles.css:

css
p { color: crimson; }

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

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

Now a single file styles the whole site. Change it once and every page updates.

In this course the examples use a <style> block, so they run right here in the browser. On a real site, move those rules into your .css file.


3. Comments in CSS

css
/* Brand colour for all headings */
h1, h2 { color: #0468b5; }

CSS comments use /* … */. The <!-- --> style is for HTML only.


4. The Cascade: Which Rule Wins?

When two rules set the same property on the same element:

  1. More specific selectors win. An id beats a class, and a class beats a tag name (Lecture 002).
  2. If they're equally specific, the later rule wins.
  3. Inline style beats both.

Example

html
<style>
  p { color: blue; }
  p { color: green; }      /* later, so it wins over blue */
  .note { color: purple; } /* a class is more specific, so it wins over both */
</style>

<p>I am green.</p>
<p class="note">I am purple.</p>
<p class="note" style="color: orange;">Inline style wins: I am orange.</p>

That's where the name comes from: rules flow down, or "cascade", and the strongest one applies.


5. When CSS Has an Error

CSS doesn't crash. If a declaration is invalid, the browser skips just that declaration and carries on with the rest. That's forgiving, but it means mistakes fail silently.

Example: two typos; spot which styles still work:

html
<style>
  p {
    color: green;
    font-size: 20px
    background: yellow;  /* lost: the line above has no semicolon */
    colr: red;           /* ignored: misspelled property */
    font-weight: bold;
  }
</style>

<p>Green and bold, but not yellow and not 20px.</p>

When a style "doesn't work", check for a missing ;, a missing }, or a misspelled property. In DevTools (right-click → Inspect), invalid declarations appear crossed out with a warning icon.


Common Beginner Mistakes

1. Missing semicolons. color: red font-size: 20px; breaks both declarations. End every declaration with ;.

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

3. Wrong file path. If href="style.css" but the file is called styles.css, nothing is styled. Check the spelling.

4. Styling everything inline. It works, but a site with hundreds of inline styles is painful to change.


Practice Questions

  1. In p { color: red; }, which part is the selector, the property and the value?
  2. What are the three ways to add CSS to a page?
  3. Which way is best for a website with many pages, and why?
  4. How do you write a comment in CSS?
  5. Two rules have the same selector and set different colours. Which one applies?
  6. Write a rule that makes all <h2> headings navy and 24px.

Practice Answers

  1. Selector p, property color, value red.
  2. Inline (style attribute), internal (<style> block) and external (a linked .css file).
  3. External: one file styles every page, so a change is made once.
  4. /* comment */
  5. The one written later.
  6. h2 { color: navy; font-size: 24px; }

Key Takeaways

  • A rule = selector + { property: value; } declarations.
  • External stylesheets are the best home for CSS; <style> and inline are for small cases.
  • CSS comments use /* … */.
  • When rules clash: inline > more specific > written later.