Introduction to CSS
Style your first web page
Skills taught: css.basics, css.linking, css.syntax
CSS stands for Cascading Style Sheets. It is the language browsers use to decide how HTML elements look — colours, fonts, spacing, and layout.
Linking CSS to HTML
The most common way to use CSS is an external file linked from your HTML <head>:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My Page</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Hello, CSS!</h1>
<p>This paragraph will be styled.</p>
</body>
</html>
CSS rule anatomy
A CSS rule has two parts: a selector (what to target) and a declaration block (what to change):
/* selector { property: value; } */
h1 {
color: #4fd1c5; /* text colour */
font-size: 2rem; /* text size */
}
p {
color: #94a3b8;
line-height: 1.6;
}
- selector — which HTML element(s) to target (
h1, p, .card) - property — what to change (
color, font-size, margin) - value — the new value (
#4fd1c5, 2rem, 0 auto) - declaration — one property–value pair, ending with a semicolon
- declaration block — all declarations inside the curly braces
Tip: CSS comments use /* ... */ syntax. Use them to explain non-obvious values like magic numbers or colour choices.
Three ways to write CSS
<!-- 1. External stylesheet (BEST — reusable across pages) -->
<link rel="stylesheet" href="style.css" />
<!-- 2. Internal style block (useful for single pages) -->
<style>
h1 { color: teal; }
</style>
<!-- 3. Inline style (AVOID — hard to maintain) -->
<h1 style="color: teal;">Title</h1>
Warning: Avoid inline styles. They mix structure and presentation, they cannot be reused, and they have very high specificity — making them hard to override later.
Frequently Asked Questions
Do I need to learn HTML before CSS?
Yes. CSS targets HTML elements, so you need to understand tags, classes, and IDs before CSS selectors make sense.
What is the difference between CSS and CSS3?
CSS3 is the current version of CSS, introduced around 2011. It added modules for flexbox, grid, animations, custom properties, and media queries. All modern browsers support CSS3 features.
Can I use multiple CSS files?
Yes. Large projects often split CSS across multiple files — one for layout, one for typography, one per component. Each file needs its own <link> tag in HTML.