CSS Basics

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).

/* CSS Rule Anatomy */
selector
property: value;
property: value;
}

Selector

Identifies which HTML elements to style

h1
.class-name
#element-id

Property

The style aspect to change

color
font-size
margin

Value

How to change the property

blue
16px
10px

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

1

Element Selectors

Basic

Element 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.

h1 { color: blue; } p { font-size: 16px; } div { margin: 10px; }
Targets all elements of that type
2

Class Selectors

Common

Class 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.

.highlight { background: yellow; } .button { padding: 10px; } .container { max-width: 800px; }
Prefix with dot (.)Reusable
3

ID Selectors

Unique

ID 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.

#header { position: fixed; } #main-content { margin-top: 60px; } #footer { background: #333; }
Prefix with hash (#)One 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

1 / 4
1

Rules, Selectors, and Declarations

A CSS rule consists of a selector targeting elements, and a declaration block with property: value pairs inside curly braces.

1 of 4
🧠CSS Has No Single-Line Comments
Unlike JavaScript, CSS only supports block comments with /* */. If you try // for a comment, the browser will treat it as invalid syntax and ignore the declaration — a common copy-paste mistake.

🎯 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.