Sub topic 2
Anatomy of a CSS rule
This chapter assumes you know basic HTML structure (html, head, body, common elements). It also assumes you can edit and save text files and open them in a browser. Every method for adding CSS to a page, which later chapters cover, uses the same building block: the rule. Learn its parts once and you can read all three methods.
A rule has two halves
A CSS rule says which elements to style and how to style them. It has a selector and a declaration block.
p {
color: darkblue;
line-height: 1.5;
}pis the selector. It chooses the elements the rule applies to. Here it matches every<p>element.- The braces
{ }enclose the declaration block. - Each line inside the braces is a declaration. A declaration is a property, a colon, a value, and a semicolon.
colorandline-heightare properties. A property names one aspect of appearance.darkblueand1.5are values. A value is the setting chosen for that property.
Declarations in detail
A declaration always has the shape property: value;. Three details cause most beginner errors.
- The separator is a colon, not an equals sign.
color = redis invalid. - Semicolons separate declarations. The last one may omit the semicolon, but you should always write it. If you add a line later and the previous line has no semicolon, the two merge into one broken declaration.
- Whitespace and line breaks do not matter.
p{color:red;margin:0}is the same rule as the formatted version.
Many values need a unit or a keyword. margin: 2rem is valid. margin: 2 is not, because only zero may be written without a unit. Colors can be keywords such as crimson, hex codes such as #dc143c, or functions such as rgb(220 20 60).
Comments
CSS comments start with /* and end with */. They can span several lines. CSS has no // comment, so a line beginning with // becomes part of the next rule and breaks it.
/* Body text */
p {
color: darkblue; /* a dark shade, easier to read than pure blue */
}Selecting more than one element
A selector can be a single element name, or a comma-separated list. The comma means "or", so the same declarations apply to each selector.
h1, h2, h3 {
font-family: Georgia, serif;
margin-bottom: 0.5rem;
}You can also write several rules in a row. Each rule is independent, and a stylesheet is simply a list of rules.
body {
font-family: system-ui, sans-serif;
margin: 2rem;
}
p {
color: darkgreen;
}What the browser does with mistakes
CSS does not stop at an error. The browser discards the part it cannot parse and keeps going, and it shows no message on the page.
- An unknown property, such as
colour: red;, is ignored. - An invalid value, such as
margin: 2;, makes that one declaration ignored. Other declarations in the block still apply. - A missing closing brace is worse. The browser may swallow the next rule into the broken one, so styles further down the file silently stop working.
p {
colour: red; /* unknown property: ignored */
margin: 2; /* missing unit: ignored */
color: darkblue; /* still applies */
}When a style does not appear, check the spelling, colon, semicolon, and braces before you suspect anything more complicated.
Where the rule appears in the three methods
The same declarations show up in all three methods, but the selector appears in only two of them.
| Method | Selector written? | What you write |
|---|---|---|
| Style attribute | No, the element carrying the attribute is the target | Declarations only |
| Style element | Yes | Full rules |
| Linked stylesheet | Yes | Full rules in a `.css` file |
Compare an inline version with its rule form:
<p style="color: crimson; font-weight: bold;">Inline: declarations only.</p>p {
color: crimson;
font-weight: bold;
}The declarations are identical. The inline version has no braces and no selector, because the element that holds the attribute is the target. The next chapters show each method in turn.
Exercise
This rule has three mistakes. Find them. p { color = red; margin: 2; font-weight: bold }
Show answer
First, color = red uses an equals sign, and the separator must be a colon, so write color: red;. Second, margin: 2 has no unit (only 0 may omit one), so write margin: 2rem; or similar. Third, the final declaration lacks a semicolon. This is legal for the last declaration but fragile, so add it. The browser ignores the first two invalid declarations and applies only font-weight: bold.
Exercise
Rewrite the inline style style="color: darkblue; line-height: 1.5;" as a rule that styles every paragraph.
Show answer
p { color: darkblue; line-height: 1.5; }