Selectors and the cascade: styles in a separate file
Why:You’ll keep the look of a page apart from its content, and find out why “my rule doesn’t work”. Afterwards you’ll be able to link a stylesheet, pick the elements you want and predict which of two rules wins.
30 minutes
Read by speech synthesis (Qwen3-TTS); the voice is not a person’s.
CSS is the language that styles a page from a separate file. A selector picks elements, and the cascade decides which rule wins.
Looks apart from meaning
In the first part you built a page from meaningful elements, and in the browser it looks like a page from a textbook: black on white, blue links, Times New Roman. That is a “page without styling”, and it is how search engines and screen readers see your page today. Now let’s give it a look. There is a separate language for that: CSS, Cascading Style Sheets.
The idea of separating looks from meaning came in October 1994, before Netscape 2 existed. It was proposed by Håkon Wium Lie, an engineer at CERN and a colleague of Tim Berners-Lee. He called the styles “cascading” because several sources style a page at once (the browser, the reader, the site’s author), and someone has to decide whose word counts most. You’ll work through that “ladder of priorities” in this lesson. CSS 1 became a W3C Recommendation in December 1996, but for years browsers understood it in different ways, and it only became truly usable in the mid-2000s. In 2003 the CSS Zen Garden appeared: one HTML page, hundreds of unlike designs.
Three ways to add styles
- A separate file. In the head, a line:
<link rel="stylesheet" href="styles.css">. Every page of the site links to the one file, so the look changes in one place, and the browser saves the file instead of downloading it again. This is what nearly everyone does. - A
<style>element in the head. The styles sit in the page itself. It suits a small example, or styles for a single page. - A
style="…"attribute on an element. The styling sits right on the element. This is the worst way: the rule can’t be reused, it beats almost everything else and it upsets the order. Use it rarely (for example, when a script works out the value).
Remember the lesson on rendering: CSS blocks painting until the browser has it. So the stylesheet is linked right at the start and kept small.
A rule and a selector
A CSS rule has a selector (who) and, in curly braces, pairs of “property: value;” (what to do):
.card {
padding: 1rem;
border: 1px solid #c9c4b6;
} The main selectors:
- by tag:
p,h2andamean every element of that kind; - by class:
.cardmeans every element withclass="card". This is the main tool: you can give any element a class and use it as many times as you like; - by id:
#topmeans the one element withid="top"; - descendant:
nav ameans links insidenav, at any depth; direct child:ul > li; - list:
h2, h3means both; - by state:
a:hover(under the pointer),a:focus-visible(in focus, when you work from the keyboard),li:first-child; - by attribute:
a[href^="tel:"]means links whose address starts with “tel:”.
Check yourself: what does the selector .card pick?
Every element on the page with the class card. Unlike an id, a class can be used many times.
Inheritance
Some properties pass from a parent to its descendants on their own: text color, typeface, font size, line height. That is why you set the typeface and text color of a page once, on body, rather than on every paragraph. Other properties aren’t inherited: spacing and borders belong to the block itself, and its children don’t get them. It would be odd if a border around a paragraph appeared around every word as well.
The cascade: who wins
When several rules act on one element, the browser decides by a ladder:
- Importance and origin. A rule with
!importantbeats ordinary ones. Better not to use it: it breaks the ladder, and the only way to fight it is with another one. - Specificity. The more precise the selector, the stronger it is. It is counted with three numbers: (ids, classes, tags).
#ctais (1, 0, 0),.buttonis (0, 1, 0) andnav ais (0, 0, 2). They are compared from left to right: one id beats any number of classes, and one class beats any number of tags. - Order. If the strength is equal, the rule lower in the file wins.
This is how a rule usually “doesn’t work”: you wrote a { color: red } for a link, but somewhere above it nav a { color: green }, at (0, 0, 2), turned out stronger. Or you wrote the rule higher up than it needed to be. Try it on the diagram: the rules color the same link, and the diagram counts who wins. Change the selectors and check the !important boxes.
The element: <a class="button" id="cta"> inside nav inside header. Each rule sets one property: color.
| No. | Selector | color | !important | Count (id, class, tag) | Result |
|---|---|---|---|---|---|
| 1 | — | not a selector | |||
| 2 | — | not a selector | |||
| 3 | — | not a selector |
No rule matches, so the link keeps its default color. The order is: !important first, then specificity, then whatever comes later in the file.
Practical rules that people follow today:
- style with classes and keep specificity low, so rules are easy to override;
- don’t style by
id: it is too strong, and only another id can override it; - don’t nest selectors deeper than two or three levels;
- keep
!importantfor extreme cases. In the lesson on modern CSS you’ll meet a better tool: layers, which replace most of these fights.
Check yourself: which is stronger, one id or ten classes?
One id. Specificity is compared from left to right, and one id (1, 0, 0) beats any number of classes (0, 10, 0).
A palette by tone
Your brief has a “tone” field. It will tell you what to build the palette from: two or three colors and a neutral. Here are some starting points. You can change the numbers, but check the contrast (you’ll learn how in the lesson on color):
| Tone | What it feels like | A palette idea |
|---|---|---|
| Businesslike | composure, trust | navy, gray, white; a serif typeface in the headings |
| Warm | care, comfort | terracotta and a cream background, dark brown text |
| Bold | energy | an almost black background or text, and one bright accent |
| Calm | clarity, air | a muted green or teal on a light warm background |
Practice
You’re in 2005. The page is built from meaning, and now it needs dressing. The stylesheet styles.css is already waiting for you in the second tab. Link it in the head, then style the body, the header, the button and the cards in turn. Resist the temptation to write style="…" right on an element: the check will notice, and you’ll lose the point of the exercise.
In the header, the text has to be readable on its background. Choose colors to suit your tone: a program will check the contrast.
What was wrong with 2005
The main trouble wasn’t CSS, but browsers. Internet Explorer 6, by far the most widely used, read CSS in its own way, and authors wrote “hacks” (clever lines that one browser understood and the others ignored) and conditional comments for different versions. The fashionable “web 2.0” style, with its glossy gradients, shadows and rounded buttons, has gone too: it was tied to the way images were drawn then. What remains is what still works: HTML for meaning, CSS for looks, and classes as the language between them.
Practice
Preparing your page…
Test
Question 1 of 6
Cards
These cards go into your review once you finish the lesson.
What is the idea behind CSS?
Answer
Keep looks apart from meaning: HTML says what is what, and CSS, in a separate file, says how it looks. Håkon Lie proposed it in 1994.
How do you link a stylesheet?
Answer
In the head, with a link element: rel is stylesheet and href is styles.css. Don’t put styles in the style attribute: they can’t be reused.
What is a CSS rule made of?
Answer
A selector (who), curly braces, and inside them pairs of “property: value;”. For example: .card { padding: 1rem; }.
How does the cascade decide which rule wins?
Answer
!important first, then specificity (an id beats a class, a class beats a tag), and when that is equal, the rule lower in the file.
What is inheritance?
Answer
Some properties (color, font-family) pass down to descendants. Spacing and borders don’t. That is why the typeface and text color are set on body.
Sources
- Getting started with CSS (MDN) CC BY-SA, in English
- Handling conflicts (MDN) CC BY-SA, in English
- Specificity (web.dev) CC BY, in English
- Cascading HTML Style Sheets: A Proposal (Håkon Wium Lie, 1994) link only, in English
- CSS Cascading and Inheritance Level 5 (W3C) link only, in English
Videos
CSS Specificity explained (Kevin Powell)
The video loads from youtube-nocookie.com only when you press play.
Next
You now know: selectors, the cascade and inheritance, specificity, A separate stylesheet.