CSS Syntax
You already know CSS styles HTML. This session zooms into the grammar of a rule so you can read and write styles without guessing where braces, colons, and semicolons belong.
What is CSS syntax?
CSS syntax is the fixed pattern used to write a style rule. Almost every everyday rule follows the same shape: choose what to style (the selector), open a declaration block with {, list one or more declarations, then close with }. Each declaration pairs a property with a value.
Anatomy of a CSS rule
Break a rule into named parts. Once you can label each piece, debugging typos becomes much easier.
📊 Parts of a CSS rule
| Part | Example | Role |
|---|---|---|
| Selector | p |
Chooses which elements the rule applies to |
| Declaration block | { … } |
Holds one or more declarations between curly braces |
| Declaration | color: teal; |
One property–value instruction, usually ending with ; |
| Property | color |
The aspect you want to change |
| Value | teal |
The setting you assign to that property |
| Colon / semicolon | : / ; |
Separates property from value; ends most declarations |
A labeled rule
Here is a minimal rule with every major piece visible. Later sessions deepen selectors; here the goal is correct structure.
Labeled CSS rule
cssp {
color: teal;
}
Read it left to right
p— selector (every paragraph){…}— declaration blockcolor— property:— separates property from valueteal— value;— ends the declaration
Whitespace and line breaks are flexible. You could writep{color:teal;}on one line; browsers treat it the same. Readable multi-line form is preferred for learning and teamwork.
Several declarations in one block
One rule can set many properties for the same selector. Put each declaration on its own line and end it with a semicolon.
Multiple declarations
css.note {
color: #0f172a;
background-color: #e0f2fe;
padding: 1rem;
border-radius: 0.5rem;
}
Why this works
.noteis a class selector (you will practice classes more later)- Four declarations share one declaration block
- The browser applies all of them to elements with
class="note" - A missing semicolon between two declarations can make the next property fail to apply
Grouping selectors
When several selectors need the same styles, list them separated by commas. One declaration block serves all of them.
Grouped selectors
cssh1, h2, h3 {
font-family: Georgia, serif;
color: #1e293b;
}
When to group
Grouping avoids copy-paste when headings (or other elements) should share look-and-feel. If one selector needs a unique style later, give it its own rule instead of overloading the group.
Keep this short mental list while you write:
- Curly braces wrap the declaration block
- Colon sits between property and value
- Semicolon ends each declaration (required before the next one; good habit even on the last)
- Commas separate grouped selectors — not properties
- Property names and most keyword values are case-insensitive, but write them in lowercase by convention
Try it yourself
- Create
syntax.htmlwith a paragraph:<p class="note">Syntax practice</p>and a<style>block in the head. - Add the
.notemulti-declaration rule from this session. - Change
background-colorto#fef3c7, save, and refresh. - Add a fifth declaration:
font-size: 1.125rem;inside the same block, save, and refresh again. - Confirm both the background and the font size update. If not, check braces and semicolons.
Common mistakes
- Writing
color tealwithout a colon — the browser cannot parse the declaration - Forgetting
{or}so declarations sit outside a block and are ignored - Omitting the semicolon between two declarations so the second property never applies
- Using a comma between property and value (
color, teal) instead of a colon - Putting a property on its own line with no selector or braces — that is not a complete rule
Summary
- CSS syntax = selector + declaration block
{ }filled with declarations - A declaration is
property: value; - One block can hold many declarations for the same selector
- Commas group selectors that share styles
- Careful braces, colons, and semicolons prevent silent failures
- Practice by editing values and adding declarations, then refreshing
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top