Lesson 1 of 7 · 5 min
How CSS works
Rules, selectors and declarations, three ways to add CSS to a page, and how the cascade and inheritance decide what you see.
CSS stands for Cascading Style Sheets. A stylesheet is a list of rules. Each rule picks some elements with a selector and sets declarations on them.
h1 {
color: #0a0a0a;
font-size: 2rem;
}
Here h1 is the selector, and color: #0a0a0a; is a declaration made of a property (color) and a value (#0a0a0a). Every declaration ends with a semicolon.
Three ways to add CSS
1. An external stylesheet (use this). One file, shared by every page, cached by the browser.
<head>
<link rel="stylesheet" href="styles.css">
</head>
2. A <style> element inside the page. Handy for a quick experiment or a single-page demo.
<style>
body { font-family: system-ui, sans-serif; }
</style>
3. An inline style attribute. It only affects one element and is hard to override, so keep it for values set by JavaScript.
<p style="margin-top: 0">Inline styles win over most rules.</p>
The cascade
When two rules set the same property on the same element, the browser has to pick one. It compares, in order:
- Importance: a declaration marked
!importantbeats a normal one. Avoid it; it makes later changes painful. - Specificity: a more specific selector wins (covered in the next lesson).
- Source order: if everything else is equal, the rule that comes later wins.
p { color: black; }
p { color: gray; } /* later, same specificity: paragraphs are gray */
Inheritance
Some properties pass down from a parent to its children. Text properties such as color, font-family, font-size and line-height inherit. Box properties such as margin, padding, border and width do not.
That is why you set the font once on body:
body {
font-family: "Geist", system-ui, sans-serif;
line-height: 1.6;
color: #0a0a0a;
}
Every paragraph, list and link inside now uses that font, unless a more specific rule says otherwise.
Units you will use most
| Unit | Relative to | Good for |
|---|---|---|
px |
Nothing, a fixed size | Borders, small details |
rem |
The root font size (usually 16px) | Font sizes, spacing |
em |
The element's own font size | Padding that grows with text |
% |
The parent's size | Widths |
vw / vh |
The viewport width / height | Full-screen sections |
Prefer rem for type and spacing. If a reader raises their browser's default font size, a rem-based page scales with it.
Try it
Make a page with an h1 and two paragraphs. Set the font on body, then give the second paragraph a different colour with a class:
body { font-family: system-ui, sans-serif; }
.note { color: #666; }
<p>Normal text.</p>
<p class="note">Muted text.</p>
Open the browser's developer tools (right-click, Inspect) and select the second paragraph. The Styles panel shows every rule that matches it, with overridden declarations crossed out. That panel is the best way to answer "why does this look like that?".
More from CSS Crash Course
- Lesson 3 · 5 minThe box modelEvery element is a box of content, padding, border and margin. Learn how they add up, how display changes the box, and the one reset every project needs.
- Lesson 4 · 5 minTypography and colourFonts, sizes, line height and measure, colour formats, and design tokens with CSS custom properties, including a dark theme in a few lines.
- Lesson 5 · 5 minFlexboxLay out items in a row or a column, align them on both axes, share space between them, and build the navigation bars and card rows you see everywhere.