CSS syntax defines how styles are written and applied to HTML elements. Understanding
CSS syntax is one of the most important skills for beginners because every CSS rule,
component, layout, animation, and responsive design technique relies on proper syntax.
What Is CSS Syntax?
CSS syntax is the structure used to write CSS rules. A CSS rule tells the browser
which HTML element should be styled and what styles should be applied.
selector {
property: value;
}
Every CSS rule contains a selector and one or more declarations. The browser reads
these declarations and applies the styles to matching HTML elements.
CSS Syntax Cheatsheet
Part
Example
Purpose
Selector
h1
Selects HTML elements.
Declaration Block
{ ... }
Contains CSS declarations.
Property
color
Defines what style changes.
Value
blue
Defines the property setting.
Declaration
color: blue;
Property-value pair.
Rule Set
h1 { color: blue; }
Complete CSS rule.
Comment
/* comment */
Documentation inside CSS.
Class Selector
.card
Targets reusable elements.
ID Selector
#header
Targets unique elements.
Media Query
@media
Creates responsive styles.
Basic CSS Rule Structure
Every CSS rule follows the same basic structure:
selector {
property: value;
}
Example
h1 {
color: blue;
}
This rule changes all h1 elements to blue.
Selectors, Properties, and Values
CSS syntax consists of three primary building blocks.
p {
color: #333333;
}
Part
Example
Description
Selector
p
Selects all paragraph elements.
Property
color
Defines the style category.
Value
#333333
Defines the property setting.
CSS Declarations
A declaration consists of a property and value separated by a colon.
color: blue;
Multiple declarations can exist inside a declaration block.
Grouping selectors reduces duplicate code and improves maintainability.
Common CSS Syntax Errors
Problem
Incorrect
Correct
Missing colon
color blue;
color: blue;
Missing semicolon
color: blue
color: blue;
Missing brace
h1 color: blue;
h1 { color: blue; }
Wrong selector
button
.button or #button
How Browsers Read CSS Syntax
Browser downloads CSS files.
Browser parses selectors.
Browser reads declarations.
Matching HTML elements receive styles.
Final styles are rendered on screen.
If syntax errors exist, browsers may ignore some or all of the affected rules.
CSS Syntax Best Practices
Use meaningful class names.
Keep selectors simple.
Always use semicolons.
Indent consistently.
Group related declarations together.
Use comments for large sections.
Prefer classes over IDs for reusable styling.
Write mobile-first CSS.
Remove unused rules.
Validate CSS before deployment.
Key Takeaways
CSS syntax follows a selector-property-value structure.
Selectors determine which elements receive styles.
Properties define what changes.
Values define how the style changes.
Declarations are grouped inside curly braces.
Clean syntax improves maintainability and debugging.
Understanding CSS syntax is essential before learning selectors, box model, Flexbox, Grid, and responsive design.
Pro Tip
Most CSS bugs for beginners come from missing semicolons, incorrect selectors,
and unmatched braces. Always format your CSS consistently and use browser DevTools
to inspect applied styles.
You now understand CSS syntax, selectors, properties, values, declarations,
rule sets, comments, formatting conventions, and syntax best practices.