Sub topic 1
Rules, selectors and declarations: the inputs to the cascade
Prerequisites
This chapter assumes you already know:
- Basic HTML structure: elements, classes, ids and nesting.
- How to write a simple CSS rule with a selector and declarations.
- How to open your browser's developer tools and inspect an element.
Why start with the inputs
The cascade is the process that picks one value for each property of each element. Before it can pick, it needs a list of candidates. Every candidate comes from a declaration inside a rule whose selector matches the element. This chapter covers how those candidates are built, so the later chapters on ordering make sense.
Anatomy of a rule
A rule has two parts: a selector list and a declaration block.
h1,
.title,
#logo {
color: navy;
margin-bottom: 0.5rem;
}- The selector list is
h1, .title, #logo. It says which elements the rule targets. - The declaration block is everything inside the braces.
- A declaration is one property and value pair, such as
color: navy. Declarations end with a semicolon.
A comma in a selector list means "or". The browser treats h1, .title and #logo as three separate selectors that share one block. This matters later. Each selector has its own specificity, and the browser uses the one that matched the element.
From rules to candidates
For each element, the browser runs through every rule in every stylesheet. If a selector matches, each declaration in the block becomes a candidate for that element. An element can end up with many candidates for the same property.
p { color: black; }
.note { color: blue; }
#intro { color: green; }For <p id="intro" class="note">Hello</p>, all three rules match. The element gets three candidates for color.
| Candidate | Selector | Value |
|---|---|---|
| 1 | `p` | black |
| 2 | `.note` | blue |
| 3 | `#intro` | green |
Choosing among them is the cascade's job. The next chapters explain how.
Invalid input is dropped early
The browser discards invalid declarations while parsing. They never reach the cascade. Developers use this on purpose to write fallbacks.
.box {
width: 50%;
width: calc(100% - 2rem);
}A browser that understands calc() keeps both declarations, and the second one wins by source order. A browser that does not understand it drops the second declaration and uses 50%.
Selectors are stricter. If one selector in a list is invalid, the browser drops the whole rule.
.card, ::not-a-real-pseudo {
padding: 1rem;
}Here .card gets no padding, because the unknown pseudo-element invalidates the entire rule. The :is() and :where() functions are the exception, because they ignore invalid selectors inside them.
Shorthands expand into longhands
A shorthand property sets several longhand properties at once. The cascade works on longhands, so a shorthand can compete with a longhand written elsewhere.
.spaced { margin-top: 10px; }
.reset { margin: 0; }An element with both classes receives two candidates for margin-top: 10px and 0. The margin: 0 declaration also adds candidates for the other three sides. A shorthand sets every longhand it covers. Omitted values reset to their initial value.
Common errors at this stage
These mistakes stop a declaration from becoming a candidate. They happen before any cascade logic runs.
| Mistake | Effect |
|---|---|
| Misspelled property, such as `colour: red` | Declaration is dropped |
| Invalid value, such as `margin: 1x` | Declaration is dropped |
| Missing semicolon between declarations | The two declarations merge and both can fail |
| `.card .featured` when you meant `.card.featured` | Selector targets a different element, so no match |
Check these first when a style seems to do nothing. In developer tools, a dropped declaration appears struck through with a warning icon, or does not appear at all.
Exercise
Given .a, .b { color: red; } and <p class="a">, how many candidates for color does the paragraph get from this rule, and what happens if you change the selector list to .a, ::bogus?
Show answer
One candidate, red, because the .a selector matches. With ::bogus in the list, the whole rule is invalid and dropped, so the paragraph gets no candidate from it.
Exercise
An element matches .x { margin-left: 4px; } and .y { margin: 0; }. How many candidates does it have for margin-left?
Show answer
Two: 4px from .x and 0 from the margin shorthand in .y. The cascade decides between them in later chapters.