CSS Syntax
In this page:
selector {
property: value;
property: value;
}
Anatomy of a CSS Rule
A CSS rule always has two parts: a selector that says what to style, and a declaration block in curly braces that says how to style it. Together they form one complete rule. If either part is missing or the braces are mismatched, the browser will typically ignore the entire malformed rule.
Example: Anatomy of a CSS Rule
<style>
p {
color: blue;
}
</style>
<p>Selector "p" plus a declaration block "{ color: blue; }"</p>
Selectors and Declaration Blocks
The selector comes before the opening curly brace and identifies which elements the rule applies to. Everything between the curly braces is the declaration block, holding the actual styling instructions.
Example: Selectors and Declaration Blocks
<style>
.highlight {
background: yellow;
}
</style>
<p class="highlight">Selector before the brace, declarations inside</p>
Properties and Values
Inside a declaration block, each line is a property (what aspect you're changing, like color or padding) paired with a value (the setting you want, like red or 16px), separated by a colon.
Example: Properties and Values
<style>
p {
color: red;
padding: 16px;
}
</style>
<p>color: red and padding: 16px are property:value pairs</p>
Multiple Declarations in One Rule
A single selector can control many properties at once, simply by listing several declarations, each ended with a semicolon, inside the same declaration block.
Forgetting a semicolon between declarations is a common source of bugs, since the browser may misread where one declaration ends and the next begins.
Example: Multiple Declarations in One Rule
<style>
p {
color: green;
font-size: 18px;
padding: 10px;
}
</style>
<p>Three declarations, each ended with a semicolon</p>
Grouping Selectors
When several different selectors need the exact same styling, you can group them together with commas instead of repeating the same declaration block over and over. This avoids the repetition and maintenance burden of copy-pasting the same block under several separate selectors.
Example: Grouping Selectors
<style>
h1, h2, h3 {
color: navy;
}
</style>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
- Forgetting the semicolon at the end of a declaration, which can cause the next declaration to be ignored.
- Mixing up the colon (between property and value) with the semicolon (between declarations).
- Leaving off one of the curly braces, which breaks the entire rule and often the rules after it too.
- A CSS rule is made of a selector and a declaration block wrapped in curly braces.
- Each declaration inside the block is a property and a value, separated by a colon and ended with a semicolon.
- Getting the punctuation right (colons, semicolons, braces) is essential, since CSS is strict about its syntax.
CSS syntax rules are part of the language standard and are interpreted identically by every modern browser.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: