CSS Syntax
Learn the fundamental syntax and structure of CSS rules, selectors, properties, and values that form the foundation of web styling.
What is the syntax of a CSS rule?
A CSS rule is a selector followed by a declaration block in curly braces. Each declaration is a property and a value separated by a colon and ended with a semicolon: p { color: red; font-size: 16px; }.
selector {
property: value;
property: value;
}
/* Group selectors with commas */
h1, h2, h3 { font-family: Georgia, serif; }CSS Rule Structure
CSS Rule Structure is the fundamental syntax pattern consisting of a selector (identifies which HTML elements to target) followed by a declaration block (contains property-value pairs enclosed in curly braces that define the styling instructions).
Selector
Identifies which HTML elements to style
Property
The style aspect to change
Value
How to change the property
CSS Syntax in Action
CSS Syntax in Action: A practical demonstration of CSS syntax rules applied to create a complete styled webpage, showcasing how selectors, properties, values, and comments work together to control element presentation and layout.
Code Editor
<h1>Selector</h1>
<p class="note">A rule is a selector plus declarations.</p>Preview
Syntax Rules Demonstrated
- •Proper selector syntax (element, class, ID)
- •Declaration blocks with curly braces
- •Property-value pairs with colons
- •Semicolons ending each declaration
Key Concepts
- •Comments for code documentation
- •Grouping selectors with commas
- •Pseudo-classes (:hover, :active)
- •Multiple properties in one rule
CSS Selector Types
Element Selectors
BasicElement Selectors: CSS selectors that target all HTML elements of a specific tag type (e.g., h1, p, div) by specifying the tag name without any prefixes, applying styles to every instance of that element on the page.
Class Selectors
CommonClass Selectors: CSS selectors that target HTML elements with a specific class attribute value, identified by a period (.) followed by the class name, allowing multiple elements to share the same styling rules.
ID Selectors
UniqueID Selectors: CSS selectors that target a single HTML element with a specific ID attribute value, identified by a hash symbol (#) followed by the ID name, designed for unique elements that appear only once per page.
CSS Syntax Rules
Correct Syntax:
- End each declaration with a semicolon (;)
- Use curly braces to wrap declarations
- Separate properties and values with colons (:)
- Use lowercase for property names
- Add spaces after colons for readability
Common Mistakes:
- Forgetting semicolons at the end of declarations
- Missing opening or closing curly braces
- Using equals (=) instead of colons (:)
- Incorrect selector syntax (.class vs class)
- Missing quotes around font names with spaces
Your Learning Path
Master CSS syntax and selectors systematically
What You've Learned
- ▶CSS rule structure and syntax
- ▶Selectors: element, class, and ID
- ▶Properties and values syntax
- ▶Declaration blocks and comments
- ▶Common syntax rules and mistakes
What's Next
- ▶CSS Selectors - Advanced targeting techniques
- ▶CSS Colors - Create stunning color schemes
- ▶CSS Backgrounds - Add visual depth
- ▶CSS Typography - Control fonts and text
- ▶CSS Box Model - Master spacing and layout
Pro Learning Tip
Practice writing CSS rules by hand to build muscle memory. Start with simple properties and gradually add complexity as you master the syntax.
CSS Syntax — step by step
Rules, Selectors, and Declarations
A CSS rule consists of a selector targeting elements, and a declaration block with property: value pairs inside curly braces.
🎯 Quick Check
What is the correct syntax for a CSS comment?
CSS Syntax: summary
Key points
- Comments are written
/* like this */. There's no//single-line comment in CSS. - The last semicolon in a block is optional, but always adding it prevents bugs when you add lines.
- Property names are case-insensitive but written in lowercase by convention.
- An invalid declaration is simply ignored; the rest of the rule still works.
Common mistakes
- Forgetting a semicolon, which silently breaks the next declaration.
- Writing a space between a number and its unit (
10 px), which makes the value invalid.
Frequently asked questions
›What is a CSS declaration?
A declaration is one property: value pair, like color: blue. A declaration block is a group of them inside { }.
›How do I write comments in CSS?
Wrap the comment in /* and */. Comments can span several lines but can't be nested.