What is CSS Syntax?
CSS syntax defines how styles are written and applied to HTML elements using selectors and declaration blocks.
Basic Structure
CSSRead-only
1
Example
CSSRead-only
1
Parts of CSS Rule
| Part | Description |
|---|---|
| Selector | Targets HTML element (e.g., p, .class, #id) |
| Property | Style attribute (e.g., color, font-size) |
| Value | Defines style (e.g., blue, 16px) |
| Declaration | Property + value pair |
Multiple Declarations
CSSRead-only
1
Grouping Selectors
CSSRead-only
1
Comments in Syntax
CSSRead-only
1
Whitespace & Formatting
CSS ignores extra spaces and line breaks, but proper formatting improves readability.
Best Practices
- Use consistent indentation
- Write one declaration per line
- Use meaningful selectors
- End each declaration with semicolon
Common Mistakes
- Missing semicolons
- Incorrect selector usage
- Typos in property names
- Forgetting curly braces {}
Conclusion
Understanding CSS syntax is the foundation of styling web pages. Proper structure ensures maintainable and error-free code.