HTML gives a page structure; CSS — Cascading Style Sheets — gives it a look. Colours, spacing, fonts, layout, animation: all CSS. The word cascading is the key idea — many rules can target the same element, and CSS has a precise system for deciding which wins. Get selectors and the cascade right and everything else falls into place.
What is CSS?
CSS is a list of rules. Each rule says "for elements matching this selector, apply these styles". The browser gathers every rule that matches an element and resolves conflicts using the cascade. You never change the HTML's meaning — only its presentation.
Three ways to attach CSS
You can hook styles onto HTML three ways — but in real projects you'll use the third almost exclusively:
<!-- 1. Inline (avoid — hard to reuse and maintain) --><p style="color: navy;">Hello</p><!-- 2. Internal (fine for a one-off page) --><head><style>p { color: navy; }</style></head><!-- 3. External (do this) --><head><link rel="stylesheet" href="styles.css"></head>
Rule syntax
Every rule is a selector plus a block of declarations. Each declaration is a property: value; pair:
selector {property: value; /* one declaration */color: #2563eb; /* another */}/* Real example */button {background: #2563eb;color: white;padding: 12px 24px;border-radius: 8px;}
Selectors
Selectors decide what a rule targets. The five you'll use constantly:
| Selector | Matches | Example |
|---|---|---|
tag | Every element of that type | p { … } |
.class | Elements with class="…" | .btn { … } |
#id | The one element with that id | #hero { … } |
a, b | Group — match a OR b | h1, h2 { … } |
a b | Descendant — b inside a | nav a { … } |
Classes are the workhorse. They're reusable and low-specificity, which keeps your CSS predictable. You'll also lean on pseudo-classes for state and attribute selectors for targeting by attribute:
a:hover { text-decoration: underline; } /* on hover */input:focus { outline: 2px solid #2563eb; } /* when focused */li:first-child { font-weight: 600; } /* the first item */tr:nth-child(2n) { background: #f6f6f3; } /* zebra stripes */a[target="_blank"]::after { content: " ↗"; } /* by attribute */
Cascade & specificity
When several rules set the same property on one element, three things decide the winner, in order:
- Importance & origin — an author rule beats a browser default;
!importantoverrides normal rules (use it rarely). - Specificity —
#id(100) beats.class(10) beatstag(1). More specific wins. - Source order — if specificity ties, the rule written later wins.
p { color: black; } /* specificity 1 */.intro { color: gray; } /* specificity 10 — wins over the tag */#lead { color: navy; } /* specificity 100 — wins over both */
Practice
Take the "About me" page from the HTML course and link an external stylesheet. Give the page a background colour, style your headings, and make links change colour on :hover — using classes, not ids.
Key takeaways
- 01CSS is rules: a selector plus property: value declarations. Prefer one external stylesheet.
- 02Class selectors are the workhorse — reusable and low-specificity.
- 03Pseudo-classes (:hover, :focus, :nth-child) target state and position.
- 04Conflicts resolve by importance, then specificity (#id > .class > tag), then source order.
- 05Avoid ids and !important for styling — they create specificity you can't override cleanly.
