Explain it simply
Explain it simply
Think of a CSS rule as a label on a shop shelf. At the top you write what it applies to (the selector), and inside the curly braces { } you list the changes (the declarations). Each line has the form property: value;, and the semicolon is the full stop that ends it.
CSS is strict but quiet. A typo or a missing unit produces no error message; the browser simply skips that part. That is why writing the syntax precisely matters so much.
Problem → Why → Solution
The problem: one missing semicolon makes two lines disappear
On a product page you write a rule to make the price green, bold and 20px. After a refresh the text is bigger, but it is still black and normal weight. There is nothing in the console and nothing in the editor, so you start wondering whether you misspelled color or the colour name.
.price {
color: green
font-weight: bold;
font-size: 20px;
}Why does this happen?
Line 2 has no semicolon, so the browser does not know where that declaration ends and keeps reading until the next semicolon. The value of color becomes "green font-weight: bold", which is not a valid colour, so the whole declaration is thrown away. font-weight never existed as a separate declaration, so the text is not bold either. font-size starts cleanly after a semicolon, so it works.
Core concept: the anatomy of a rule and how errors are handled
| Part | Example | Job |
|---|---|---|
| Selector | h1 | Chooses which elements to style |
| Declaration block | { ... } | Groups the declarations |
| Declaration | color: blue; | One setting |
| Property | color | What to change |
| Value | blue, 24px | How to change it |
A colon (:) separates the property from its value, and a semicolon (;) separates declarations. The last declaration in a block may omit its semicolon, but always adding it is wiser: a line you add later will not recreate this lesson's problem. The browser ignores spaces and line breaks, so a rule on one line or on many lines is the same rule. Property names are case-insensitive (COLOR equals color), but class names are case-sensitive, so .Note and .note are different.
When parsing, the browser skips errors in the smallest piece it can. An unknown property (font-szie) or an invalid value (font-size: 18, with no unit) drops just that one declaration, and the others in the block still apply. Structural mistakes reach further: a missing semicolon also breaks the next declaration, and a missing closing brace } makes the browser read every following rule as part of the unclosed block, so they no longer style the elements you meant.
Values come in several forms: keywords such as bold or center, numbers with units such as 24px or 1.5rem, hex colours such as #1d4ed8, and functions such as rgb(...) or url(...). Some properties take several space-separated values (border: 1px solid gray), while font-family takes a comma-separated list of fonts in order of preference. Put a font name that contains spaces in quotes, like "Times New Roman".
A comment starts with /* and ends with */, and it may span several lines. JavaScript's // comment does not exist in CSS; the browser reads it as part of the next selector and drops that whole rule.
- Read the selector up to {
- Split declarations at each ;
- Drop any invalid declaration
- End the rule at }
Punctuation tells the browser where each piece ends; lose one and the damage spills forward.
General pattern: rules and a selector list
/* a comment can span
several lines */
selector {
property: value;
property: number-with-unit;
}
selector-one, selector-two {
property: value;
}A comma-separated selector list is a single rule that selects every listed selector. Lengths (font-size, margin...) always need a unit such as px or rem, unless the value is 0.
Example: declarations that apply and declarations that are skipped
This stylesheet contains two deliberate mistakes. Open it in a browser and inspect the h1 and the p: the Styles pane shows the skipped declarations crossed out with a warning icon.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS syntax</title>
<style>
/* selector + declaration block = one rule */
h1 {
color: blue;
font-size: 24px;
font-szie: 40px; /* typo: unknown property, skipped */
}
p {
font-size: 18; /* no unit: invalid value, skipped */
color: #374151;
}
</style>
</head>
<body>
<h1>Syntax check</h1>
<p>Inspect me in DevTools.</p>
</body>
</html>Code breakdown
- Selector and opening brace
h1 {h1 is the selector and { opens the declaration block. Until the browser meets }, it reads every following line as a setting for h1.
- A valid declaration
font-size: 24px;Property (font-size), colon, a value with a unit (24px) and a semicolon: all four parts are right, so the browser accepts it.
- Unknown property
font-szie: 40px; /* typo: unknown property, skipped */There is no property called font-szie, so only this line is skipped. Its semicolon is in place, so the color and font-size above are unaffected.
- Missing unit
font-size: 18; /* no unit: invalid value, skipped */A bare number like 18 is not a valid font-size (only 0 may omit its unit), so the declaration is dropped and the default 16px stays. Without <!DOCTYPE html>, quirks mode may guess 18px for you, which is one more reason to always include the doctype.
- Still applied
color: #374151;The line above failed, but its semicolon still marked where it ended, so this declaration starts cleanly and works.
The h1 is blue and 24px (the font-szie line is skipped). The p is dark grey (#374151), but font-size: 18 is skipped, so it keeps the browser's default 16px.When one selector in a list is invalid
A selector list such as h1, h2, h3 { ... } saves writing three rules. But if one selector in the list is invalid (for example a misspelled pseudo-element), the browser drops the entire rule, and even the valid h1 loses its style. A list written inside :is() is "forgiving" and skips only the invalid selector.
/* one bad selector: the whole rule is dropped, h1 stays black */
h1, h2::nonsense {
color: red;
}
/* forgiving list: h1 turns red, the bad selector is ignored */
:is(h1, h2::nonsense) {
color: red;
}In real projects
Where this shows up in real projects
Teams do not rely on human eyes to find syntax errors. They run a linter such as Stylelint in the editor and in the CI pipeline to catch unknown properties, missing units and unbalanced braces before anything is committed. A formatter such as Prettier also rewrites every rule to one declaration per line with semicolons, so a problem like this lesson's becomes easy to see.
In production, some "works locally, broken on the live site" bugs come from a missing brace in a bundled stylesheet. When files are concatenated, one } missing at the end of the first file can break every rule in the second. If a rule does not appear in DevTools at all, check the braces above it before you check its selector.
Common mistakes
Common mistakes
1. Forgetting the unit on a length
Writing width: 300; or margin: 16; makes the browser drop the declaration. Every non-zero length needs a unit such as px, rem or %. The confusion comes from properties like line-height: 1.6, which accepts a bare number because it is a multiplier, not a length.
2. Writing JavaScript-style // comments
A line like // header styles is not a comment in CSS. The browser glues it onto the next selector and drops that rule. Write every comment as /* */.
3. Deleting part of a rule but leaving or losing a brace
When you delete a rule, remove its opening { and closing } as a pair. One brace too many or too few makes every rule below it stop working at once. Use your editor's matching-brace highlight to check.
Best practices
Best practices
- Write one declaration per line and end even the last one with a semicolon.
- Indent the lines inside a block by two or four spaces so a missing brace stands out.
- When a style does not show, look in DevTools for a crossed-out declaration with a warning icon: the browser considered it invalid.
Mini challenge
Mini challenge: a stylesheet for a sale badge
Write syntactically correct rules for the HTML in the starter. Add a comment above each rule, then confirm in DevTools that no declaration is crossed out.
- Give h2 and h3 the same font-family with one selector list
- Give .badge a background colour, a text colour, and padding and font-size with units
- Give .price a font-size, a font-weight and a colour, with every semicolon in place
- Put a /* */ comment above every rule
<!-- TODO: wrap this in <!DOCTYPE html>, <head> and <body> -->
<style>
/* TODO: one rule for h2 AND h3 (selector list) - font-family */
/* TODO: .badge - background-color, color, padding (with units), font-size */
/* TODO: .price - font-size, font-weight, color */
</style>
<h2>Rainy season sale</h2>
<h3>Waterproof jacket <span class="badge">-30%</span></h3>
<p class="price">$49</p>Expected: Both headings share one font. The "-30%" badge shows white text on dark red with padding and a smaller font. The price is 24px, bold and dark green.
Show hint
In padding: 2px 8px; the first value is top and bottom and the second is left and right. Both values need a unit.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sale badge</title>
<style>
/* headings share one font */
h2, h3 {
font-family: Georgia, serif;
}
/* the discount label */
.badge {
background-color: #b91c1c;
color: white;
padding: 2px 8px;
font-size: 0.875rem;
}
/* the price line */
.price {
font-size: 24px;
font-weight: bold;
color: #166534;
}
</style>
</head>
<body>
<h2>Rainy season sale</h2>
<h3>Waterproof jacket <span class="badge">-30%</span></h3>
<p class="price">$49</p>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A rule is a selector plus { declarations }, and each declaration is property: value;.
- An invalid declaration (unknown property, unitless length) is skipped on its own, with no error.
- A missing semicolon also breaks the next declaration; a missing brace breaks the rules after it.
- Comments are /* */ only, and one invalid selector in a list drops the whole rule.
CSS reference — MDN Web Docs