CSS Syntax and Rules
CSS Syntax and Rules
A CSS rule consists of a selector followed by a declaration block containing property–value pairs.
What you’ll learn
- Label a selector, property and value.
- Write syntactically valid declarations.
- Recognize why a malformed declaration is ignored.
Understand the concept
The form is selector { property: value; }. A colon separates a property from its value, a semicolon ends the declaration, and braces contain the declarations belonging to one selector.
CSS property names are written using hyphens, for example background-color. Whitespace usually helps readability but does not change the meaning of a simple rule.
Visualize the relationship
One property
Target all paragraphs inside the demonstration.
HTML
<p>A short message.</p>CSS
p { color: #1d4ed8; }Rendered output
A short message.
Observe: The paragraph text changes to blue.
Several declarations
Add spacing and a background in the same rule.
HTML
<p>A short message.</p>CSS
p { color: #1d4ed8; background-color: #dbeafe; padding: 14px; border-radius: 8px; }Rendered output
A short message.
Observe: One selector applies four declarations to the same paragraph.
Explain the result
In <code>p { color: navy; }</code>, <code>p</code> is the selector, <code>color</code> is the property, and <code>navy</code> is the value.
Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.
Common mistakes to avoid
- Do not omit the colon between the property and its value.
- A malformed declaration may be ignored even when the rest of the stylesheet continues to work.
Try it yourself
Add font-weight: 700; to the second rule, then remove a colon and inspect the difference.
Before changing the code, predict what will change. Then verify by checking the rendered result.
Check your understanding
What you should remember
A readable CSS rule makes selector, property and value easy to identify.
Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.
Reaching the recap and completing the quizzes are separate milestones.