Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 2 of 11

CSS Syntax & How CSS Works

Understanding CSS syntax is essential before writing any styles. Every CSS rule follows the same structure: selector, property, and value. This topic explains how CSS rules are written, how the browser reads them, and the building blocks of every stylesheet.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

CSS Rule Structure

A CSS rule has two parts: the selector (which elements to style) and the declaration block (how to style them). Inside the declaration block, each declaration has a property and a value separated by a colon, ending with a semicolon. Multiple declarations go inside curly braces { }.

Diagram
Loading diagramโ€ฆ

A CSS rule consists of a selector and a declaration block containing property-value pairs

Key Concepts

  • Selector - Targets the HTML elements: h1, .class, #id
  • Property - What to change: color, font-size, margin, background
  • Value - The setting: red, 16px, 20px, #229c6b
  • Declaration - A property:value pair: color: red;
  • Declaration block - All declarations inside { } for one selector
  • Rule - Selector + declaration block together form a complete CSS rule
  • Semicolons - Every declaration must end with ; or the next property breaks

CSS Syntax Example

CSS Syntax Example
/* CSS Rule Structure */
/* selector { property: value; } */

h1 {
  color: #E44D26;          /* text color */
  font-size: 2rem;         /* font size */
  font-weight: bold;       /* font weight */
  margin-bottom: 16px;     /* space below */
}

/* Multiple selectors, same styles */
h2, h3 {
  color: #333;
  font-family: Arial, sans-serif;
}

/* Class selector */
.highlight {
  background-color: yellow;
  padding: 4px 8px;
  border-radius: 4px;
}

Tip

Use CSS comments (/* */) to label sections of your code. Group related styles together - all heading styles, then paragraph styles, then layout styles. This habit saves hours of debugging time as your stylesheet grows.

Common Mistake

Forgetting the colon between property and value (color red instead of color: red) or missing the semicolon at the end of a declaration are the two most common syntax errors. Both cause the rule and potentially the next rule to break silently.

Practice Task

Write a CSS rule from scratch that: (1) Targets all <h2> elements, (2) Sets their color to #667eea, (3) Sets font-size to 1.5rem, and (4) Adds margin-bottom: 12px. Make sure every declaration ends with a semicolon!

Quick Quiz

Key Takeaways

  • Understanding CSS syntax is essential before writing any styles.
  • Selector - Targets the HTML elements: h1, .class, #id
  • Property - What to change: color, font-size, margin, background
  • Value - The setting: red, 16px, 20px, #229c6b

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/11 done