05. CSS Syntax Rules, Declaration Blocks & Comments

🟢 Beginner

1. Definition

A CSS Rule Set consists of a selector pointing to HTML elements and a declaration block enclosed in curly braces { ... } containing property-value pairs separated by colons and terminated by semicolons. Comments are formatted using /* comment */.

2. Why It Is Used

Following strict CSS syntax ensures predictable layout rendering across different browser engines. Shorthand properties condense multiple declarations into clean, maintainable single lines.

3. How It Works

The CSS parser reads declaration blocks line by line. When it encounters a property name (e.g. background-color), it maps the assigned value (#2563eb) to the element's CSSOM node.

4. Syntax

/* Rule set syntax */
selector {
  property: value;
}

5. Shorthand vs Longhand Comparison

Shorthand PropertyEquivalent Longhand Declarations
margin: 10px 20px;margin-top: 10px; margin-right: 20px; margin-bottom: 10px; margin-left: 20px;
background: #fff url("bg.jpg") no-repeat center;background-color, image, repeat, position
border: 2px solid blue;border-width, border-style, border-color

6. Beginner Example

/* Single property rule */
h1 {
  color: #1e3a8a;
}

7. Intermediate Example

/* Multi-property declaration block */
.card-title {
  color: #0f172a;
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 12px;
}

8. Advanced Production Rule Example

/* Component banner rule with comments */
.hero-banner {
  /* Box dimensions & padding */
  padding: clamp(2rem, 5vw, 4rem);
  background-color: var(--primary-bg, #0f172a);
  border-radius: 12px;
}

9. Line-by-Line Code Explanation

  • .card-title: Class selector targeting title elements.
  • margin-bottom: 12px;: Property-value declaration adding bottom spacing.
  • /* comment */: Ignored by browser CSS parser; useful for documentation.

10. Expected Terminal / Browser Output

Elements styled cleanly according to declared properties without syntax parsing warnings in DevTools.

11. Common Mistakes & Pitfalls

  • Using HTML comments in CSS: Writing <!-- comment --> in CSS instead of /* comment */ cause parsing errors.

12. Important Rules & Limitations

  • CSS property names are case-insensitive, but class/ID selector names in HTML are case-sensitive.

13. Multilingual Explanation: English

CSS syntax requires wrapping declarations inside curly braces { ... } using colons for assignment and semicolons to separate rules.

14. Multilingual Explanation: Hindi (Roman Script)

CSS mein har rule set mein curly braces { ... }, colon : aur semicolon ; ka sahi istemaal zaroori hota hai.

15. Multilingual Explanation: Marathi (Roman Script)

CSS syntax madhye properties { ... } braces madhye lihilya jaatat ani har declaration ; semicolon ne sampavli jaate.

16. Practice Questions

  1. What is the syntax for multi-line comments in CSS?
  2. What is the difference between shorthand and longhand CSS properties?

17. Practical Coding Challenge

Write a CSS rule for .alert-card using shorthand padding and border properties with clear inline comments.

18. Related Topics