Sub topic 1
Anatomy of a CSS Rule
Prerequisites
This chapter assumes you know basic HTML elements, attributes, and document structure. You should also know how to link or embed a stylesheet, and you should recognize simple CSS properties and values such as color and margin. Later chapters build on the vocabulary introduced here.
The parts of a rule
A CSS rule is the basic unit of a stylesheet. It tells the browser which elements to style and how to style them. Every rule has two parts: a selector and a declaration block.
p {
color: #333;
margin: 0 0 1rem;
}- The selector (
p) picks the elements the rule applies to. - The declaration block is the text inside the braces
{ }. - Each declaration is a property and a value separated by a colon, and it ends with a semicolon. In
color: #333;,coloris the property and#333is the value.
A block can hold any number of declarations. The final semicolon is optional, but keeping it makes later edits safer, because a new line added below will not break the previous declaration.
What a rule does
The browser reads the selector and tests it against every element in the document. Each element that matches receives the declarations in the block. One element can match many rules at once, and those rules can set the same property to different values. The browser then needs a way to pick one value. That process is the cascade, and most of this tutorial explains it.
Here are two rules that both match the same paragraph:
p {
color: black;
}
.note {
color: green;
}With <p class="note">, both rules apply. Both set color, so only one value can win. Why green wins is a question about specificity, which a later chapter covers.
Properties that are not set
A rule only changes the properties it names. Any property no rule sets keeps a default value. For some properties, such as color and font-family, the default is to take the parent element's value. This is inheritance. For others, such as margin, the default is the browser's built-in style. A later chapter covers inheritance in detail.
Common errors
Most beginner mistakes at this level are syntax errors, and the browser fails quietly when it meets them.
- A missing semicolon merges two declarations into one invalid line. The browser drops it, and often the next declaration too.
- A misspelled property name, such as
colour: red;, is ignored. The browser does not report an error. - An invalid value, such as
margin: 10;without a unit, is dropped. The property keeps its previous value. - A missing closing brace can swallow the rule that follows it.
Selectors versus declarations
Keep the two halves separate in your mind. The selector decides which elements are affected. The declarations decide what changes. Specificity, the main subject of this tutorial, only compares selectors. It never looks at which property or value a rule sets.
Exercise
In the rule .card { padding: 1rem; border: 1px solid #ccc; }, name the selector, the properties, and the values.
Show answer
The selector is .card. The properties are padding and border. The values are 1rem and 1px solid #ccc. The declaration block is everything between the braces and holds two declarations.
Exercise
A stylesheet contains p { colour: red; }. The paragraphs stay their default color. What is wrong, and does the cascade play any part?
Show answer
colour is not a valid property name, so the browser ignores the declaration. The cascade plays no part, because an ignored declaration never competes with other declarations.