Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

3.2 CSS Syntax

Updated Jul 7, 2026

3.2 CSS Syntax

Updated Jul 7, 2026

Before writing CSS, it is important to understand its basic syntax.

Every CSS rule consists of:

  • Selector
  • Declaration
  • Rule Set
  • Comments

What is CSS Syntax?

CSS syntax tells the browser:

Which element to style
        +
What style to apply

Every CSS rule follows this structure:

selector {
    property: value;
}

Example:

h1 {
    color: blue;
}

Breakdown:

h1      → Selector
color   → Property
blue    → Value

Selectors

A selector tells CSS which HTML element should be styled.

HTML:

<h1>Hello World</h1>

CSS:

h1 {
    color: blue;
}

Output:

Hello World
(Blue Color)

Here, h1 is the selector because it selects every <h1> element.

Without selectors, CSS would not know where to apply styles.

For example:

p {
    color: gray;
}

Meaning:

Select every <p> element
and make the text gray.

Multiple selectors can be used.

h1 {
    color: blue;
}

p {
    color: gray;
}

Common Selectors

Element Selector:

h1 {
    color: red;
}

Targets:

<h1>Title</h1>

Class Selector:

.title {
    color: blue;
}

Targets:

<h1 class="title">
    Welcome
</h1>

ID Selector:

#header {
    color: green;
}

Targets:

<h1 id="header">
    Header
</h1>

Declarations

A declaration tells the browser which style to apply.

Structure:

property: value;

Example:

color: blue;

Breakdown:

color → Property
blue  → Value

More examples:

font-size: 20px;

background-color: yellow;

width: 300px;

Multiple Declarations

A CSS rule can contain multiple declarations.

h1 {
    color: blue;
    font-size: 40px;
    text-align: center;
}

Structure:

Selector
│
└── h1
     ├── color: blue;
     ├── font-size: 40px;
     └── text-align: center;

Rule Sets

A complete CSS rule is called a Rule Set.

A rule set contains:

Selector
        +
Declarations

Example:

h1 {
    color: blue;
    font-size: 40px;
}

Structure:

Rule Set
│
├── Selector
│     └── h1
│
└── Declarations
      ├── color: blue;
      └── font-size: 40px;

Another example:

p {
    color: gray;
    line-height: 1.5;
}

Everything inside the braces belongs to one rule.


Braces

CSS rules are enclosed inside braces.

{
}

Example:

h1 {
    color: blue;
}

Components:

h1      → Selector
{       → Opening Brace
}       → Closing Brace

Everything inside the braces is called the declaration block.


Semicolons

Each declaration should end with a semicolon.

Good:

h1 {
    color: blue;
    font-size: 30px;
}

Bad:

h1 {
    color: blue
    font-size: 30px
}

Browsers may still interpret the code, but always end declarations with a semicolon.


Comments

Comments are notes for developers.

Browsers completely ignore comments.

Single comment:

/* This is a comment */

Example:

/* Heading Styles */

h1 {
    color: blue;
}

Multi-line comment:

/*
Main Theme Colors
Used Across Website
*/

Comments are commonly used for:

  • Documentation
  • Organizing code
  • Temporarily disabling styles

Example:

/* Typography */

h1 {

}

p {

}

/* Buttons */

button {

}

To disable code:

/*
h1 {
    color: red;
}
*/

Complete CSS Example

HTML:

<h1 class="title">
    Frontend Development
</h1>

<p>
    Learn HTML and CSS.
</p>

CSS:

/* Main Heading */

.title {

    color: navy;
    font-size: 40px;

}

/* Paragraph */

p {

    color: gray;
    font-size: 18px;

}

Rule Breakdown

Rule 1:

.title {

    color: navy;
    font-size: 40px;

}

Selector:

.title

Declarations:

color: navy;
font-size: 40px;

Rule 2:

p {

    color: gray;
    font-size: 18px;

}

Selector:

p

Declarations:

color: gray;
font-size: 18px;

CSS Rule Structure

Selector
   │
   ▼

h1 {

    color: blue;
    font-size: 30px;

}

   ▲
   │
Declarations

Common Mistakes

Missing semicolon:

Bad:

h1 {
    color: blue
}

Good:

h1 {
    color: blue;
}

Missing braces:

Bad:

h1
    color: blue;

Good:

h1 {
    color: blue;
}

Incorrect comment:

Bad:

// Heading Style

CSS does not support // comments.

Good:

/* Heading Style */

Key Takeaway

Every CSS rule follows the same structure.

selector {

    property: value;

}

Example:

h1 {

    color: blue;
    font-size: 40px;

}

Remember:

Selector
    ↓
Chooses Element

Declaration
    ↓
Defines Style

Rule Set
    ↓
Selector + Declarations

Comment
    ↓
Developer Notes

Understanding CSS syntax is the foundation for everything else in CSS, including selectors, layouts, Flexbox, Grid, animations, and responsive design.

3.1 CSS Introduction3.3 CSS Selectors