On this page10 sections
Lecture 009: Adding CSS to HTML
| Category | 2: Text & Styles |
| Lecture | 009 of 048 |
| Estimated video length | 10 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
CSS (Cascading Style Sheets) controls how HTML looks. You have already used it inside the
styleattribute. There are two better places to put it: a<style>block in the page, or a separate.cssfile 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.
<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
<!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:
body { background-color: #f8f9fa; }
h1 { color: navy; }
p { color: #495057; }Then link it from the <head> of every page:
<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
<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>| Property | Controls |
|---|---|
color | Text colour |
font-family | Typeface |
font-size | Text size |
border | A line around the element |
padding | Space between the content and the border |
margin | Space outside the border |
5. Which Should I Use?
| Method | Use it for |
|---|---|
Inline style | A one-off quick test |
<style> block | A single page, or examples like the ones in this course |
External .css file | Real 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
- Name the three ways of adding CSS to HTML.
- Which element holds internal CSS, and where does it usually go?
- Write the tag that links a stylesheet called
main.css. - Which is best for a website with 20 pages, and why?
- Which property adds space inside the border?
- Write a rule that makes every
<h1>dark green.
Practice Answers
- Inline (
styleattribute), internal (<style>block), external (linked.cssfile). <style>, in the<head>.<link rel="stylesheet" href="main.css">- An external stylesheet, because one file styles all 20 pages.
paddingh1 { 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,paddingandmarginare the first properties to learn.