On this page11 sections
Lecture 001: What Is CSS & How to Add It
| Category | 1: CSS Basics |
| Lecture | 001 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
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.cssfile - Explain the "cascade": which rule wins when two disagree
1. A CSS Rule
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
<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
<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
<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:
p { color: crimson; }Then link it from the <head> of every page:
<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
/* 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:
- More specific selectors win. An
idbeats aclass, and aclassbeats a tag name (Lecture 002). - If they're equally specific, the later rule wins.
- Inline
stylebeats both.
Example
<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:
<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
- In
p { color: red; }, which part is the selector, the property and the value? - What are the three ways to add CSS to a page?
- Which way is best for a website with many pages, and why?
- How do you write a comment in CSS?
- Two rules have the same selector and set different colours. Which one applies?
- Write a rule that makes all
<h2>headings navy and 24px.
Practice Answers
- Selector
p, propertycolor, valuered. - Inline (
styleattribute), internal (<style>block) and external (a linked.cssfile). - External: one file styles every page, so a change is made once.
/* comment */- The one written later.
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.