What is CSS?
🎨 Chapter 1 — What is CSS?
HTML gives a webpage its structure — headings, paragraphs, lists, links. CSS gives it its appearance — colours, fonts, spacing, layout. This chapter introduces what CSS is, how it relates to HTML, and the three different ways you can attach styles to a page. By the end you will have written working CSS using all three methods and understand when to reach for each one.
1 — What is CSS?
CSS stands for Cascading Style Sheets. It is the language used to describe how HTML elements should be displayed — their colours, sizes, fonts, spacing, and layout. Without CSS, every webpage would look like a plain document: black text on a white background, with browser-default fonts and no visual hierarchy.
Separating structure (HTML) from presentation (CSS) is one of the most important principles of web development. It means you can completely redesign the look of a website by changing only the CSS, without touching the HTML — and you can apply the same stylesheet to thousands of pages at once.
2 — CSS Syntax: Anatomy of a Rule
Everything in CSS is built from rules. A rule targets one or more HTML elements and declares how they should look. Here is the anatomy of a CSS rule:
h1)color, font-size)#4f8ef7, 2rem);- The selector identifies which HTML element(s) the rule applies to.
- Declarations sit inside curly braces
{ }. - Each declaration is a property and a value, separated by a colon
:. - Each declaration ends with a semicolon
;. The last one is technically optional, but always include it — it prevents errors when you add more declarations later. - A rule can contain as many declarations as you like.
/* Style all h1, h2, and h3 headings at once */
h1, h2, h3 {
font-family: Georgia, serif;
color: #ffffff;
line-height: 1.3;
}
/* Style paragraph text separately */
p {
font-size: 1rem;
color: #a0a4b8;
margin-bottom: 1rem;
}
3 — Method 1: Inline CSS
Inline The simplest way to add CSS is directly on an HTML element using the style attribute. The value of the attribute is a list of CSS declarations — exactly what you would put inside the curly braces of a rule, but without the selector (you are already on the element).
<h1 style="color: #4f8ef7; font-size: 2rem;">Hello, CSS!</h1>
<p style="color: #7ecfa0; font-style: italic;">This paragraph is green and italic.</p>
<button style="background-color: #4f8ef7; color: white; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer;">
Click me
</button>
When to use inline CSS
- Quick testing — when you want to see the effect of a style immediately without touching a stylesheet.
- Email HTML — many email clients strip external and internal CSS, so inline is often required for HTML emails.
- Dynamic styles via JavaScript — when JavaScript needs to set a specific style value on an element at runtime.
style attribute 50 times. It is also very difficult to override with a stylesheet. For anything beyond a quick test or a special case, avoid inline CSS.
4 — Method 2: Internal CSS
Internal Internal CSS lives inside a <style> element, placed in the <head> section of your HTML file. You write full CSS rules here — selectors, curly braces, and all — and they apply to every element on that page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Page</title>
<style>
/* These rules apply to the whole page */
body {
background-color: #0d0d0d;
color: #e2e4ec;
font-family: Georgia, serif;
padding: 20px;
}
h1 {
color: #4f8ef7;
border-bottom: 2px solid #4f8ef7;
padding-bottom: 8px;
}
p {
line-height: 1.75;
margin-bottom: 1rem;
}
</style>
</head>
<body>
<h1>Hello, Internal CSS!</h1>
<p>This paragraph is styled by the rules above.</p>
</body>
</html>
<style> tag belongs in <head>. Placing it in <body> works in most browsers but is invalid HTML.When to use internal CSS
- Single-page projects — a page that will never need its styles shared with another page.
- Prototyping and demos — when you want a self-contained HTML file with no external dependencies.
- Email templates — in combination with inline styles, some email clients support
<style>in<head>.
<style> block, with no sharing between them.
5 — Method 3: External CSS
External An external stylesheet is a separate .css file containing nothing but CSS rules. You link it to an HTML page using a <link> element in the <head>. The same stylesheet can be linked from as many HTML pages as you like — change the stylesheet once and every page updates instantly.
/* styles.css — shared styles for the whole website */
body {
background-color: #0d0d0d;
color: #e2e4ec;
font-family: Georgia, serif;
margin: 0;
padding: 20px 40px;
}
h1 {
color: #4f8ef7;
font-size: 2rem;
border-bottom: 2px solid #4f8ef7;
padding-bottom: 8px;
}
p {
line-height: 1.75;
max-width: 700px;
}
a {
color: #7ab8ff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Page</title>
<!-- Link the external stylesheet -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, External CSS!</h1>
<p>All pages that link styles.css will share these styles.</p>
</body>
</html>
href is a relative path from the HTML file to the CSS file. If they are in the same directory, just the filename is enough.Linking multiple stylesheets
You can link more than one stylesheet to a page — the browser applies them all. This is commonly used to separate a site-wide base stylesheet from a page-specific one:
<link rel="stylesheet" href="base.css"> <!-- site-wide styles -->
<link rel="stylesheet" href="blog.css"> <!-- blog-specific styles -->
<link rel="stylesheet" href="dark-theme.css"> <!-- theme override -->
6 — Comparing the Three Methods
| Method | Where it lives | Reusable across pages? | Best for |
|---|---|---|---|
| Inline | style attribute on an element |
No — one element only | Quick tests, email HTML, JS-driven styles |
| Internal | <style> tag in <head> |
No — one page only | Single-page projects, self-contained demos |
| External | Separate .css file |
Yes — link from any page | Almost everything: multi-page sites, real projects |
7 — CSS Comments
CSS comments are written with /* to open and */ to close. Everything between those markers is ignored by the browser. Unlike HTML comments (<!-- -->) or JavaScript comments (//), CSS has only one comment syntax and it works for both single-line and multi-line comments.
/* Single-line comment */
h1 {
color: #4f8ef7; /* inline comment after a declaration */
}
/* ─────────────────────────────────────────────
Section header: Typography
───────────────────────────────────────────── */
p {
font-size: 1rem;
/* font-weight: bold; */ /* commented-out rule — not applied */
}
/* property: value; */) is a handy way to temporarily disable a style during development without deleting it.8 — Your Best Friend: Browser DevTools
Every modern browser includes developer tools that let you inspect and edit CSS live in the browser. This is an essential skill — you will use DevTools constantly while writing CSS.
Right-click any element on the page → "Inspect"
Or use keyboard shortcuts:
F12 — open DevTools
Ctrl + Shift + I — open DevTools (Windows/Linux)
Cmd + Option + I — open DevTools (Mac)
Ctrl + Shift + C — open DevTools and pick an element
In the DevTools Elements panel (Chrome/Edge) or Inspector (Firefox), you can:
- Click any element to see exactly which CSS rules are applied to it.
- See which rules are overriding others (shown with a strikethrough).
- Edit property values live — changes appear instantly on the page (but are lost on refresh).
- Toggle declarations on and off with a checkbox.
- Add new declarations directly in the browser.
✏️ Exercises
Create a simple HTML file for each exercise and open it in your browser to see the results. Try to complete each one before looking at the sample solution.
style attribute directly on each element. Use color for text colour and font-size for size (e.g. 1.2rem or 18px).<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Exercise 1</title></head>
<body>
<h1 style="color: #4f8ef7;">My Styled Heading</h1>
<p style="color: #7ecfa0; font-size: 1.2rem;">
This paragraph is green and a bit larger than normal.
</p>
<p style="color: #c9a8ff; font-size: 0.9rem;">
This paragraph is purple and slightly smaller.
</p>
</body>
</html>
<style> block. Remove all style attributes from the HTML elements. The page should look identical but the styles should now live in the <head>.h1, p) in your rules. To style the two paragraphs differently you will need to give them id or class attributes — try id="intro" and id="detail" and select them with #intro and #detail in CSS.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise 2</title>
<style>
h1 { color: #4f8ef7; }
#intro { color: #7ecfa0; font-size: 1.2rem; }
#detail { color: #c9a8ff; font-size: 0.9rem; }
</style>
</head>
<body>
<h1>My Styled Heading</h1>
<p id="intro">This paragraph is green and a bit larger than normal.</p>
<p id="detail">This paragraph is purple and slightly smaller.</p>
</body>
</html>
The #intro and #detail selectors are ID selectors — they target elements with a matching id attribute. Selectors are covered in full in Chapter 2.
styles.css in the same directory. Update the HTML to link to it using a <link> tag. The page should still look identical.styles.css with just the CSS rules (no HTML). In your HTML file, replace the <style> block with: <link rel="stylesheet" href="styles.css">h1 { color: #4f8ef7; }
#intro { color: #7ecfa0; font-size: 1.2rem; }
#detail { color: #c9a8ff; font-size: 0.9rem; }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise 3</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>My Styled Heading</h1>
<p id="intro">This paragraph is green and a bit larger than normal.</p>
<p id="detail">This paragraph is purple and slightly smaller.</p>
</body>
</html>
border-bottom, line-height, max-width, background-color, color, border-radius, border, and padding.body {
font-family: Georgia, serif;
padding: 40px;
background-color: #0d0d0d;
color: #e2e4ec;
}
h1 {
color: #4f8ef7;
border-bottom: 2px solid #4f8ef7;
padding-bottom: 8px;
}
p {
line-height: 1.75;
max-width: 600px;
color: #a0a4b8;
}
button {
background-color: #4f8ef7;
color: white;
border: none;
border-radius: 6px;
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
}
The cursor: pointer declaration changes the mouse cursor to a pointing hand when hovering over the button — a small but important UX touch that signals the element is clickable.