DevAcademy
LearnCSSCSS Syntax
BeginnerCSS

CSS Syntax

Learn the anatomy of a CSS rule: selectors, declaration blocks, properties, values, and comments.

Reading Time

10 min

Lesson

Lesson 3 of 30

Anatomy of a Rule

A CSS rule pairs a selector with a declaration block. The declaration block is wrapped in curly braces and contains one or more declarations, each made of a property and a value separated by a colon, ending with a semicolon.

Rule Structure

<style>
  /* selector */
  h1 {
    /* declaration: property: value; */
    color: darkslateblue;
    font-size: 32px;
  }
</style>

<h1>Styled Heading</h1>
Output

Terminology

TermMeaning
SelectorWhat element(s) the rule targets, e.g. h1
Declaration BlockThe { ... } containing all declarations
DeclarationA single property: value; pair
PropertyThe style aspect being changed, e.g. color
ValueThe setting applied to the property, e.g. darkslateblue

Multiple Declarations

A single rule can contain as many declarations as needed. Each one ends with a semicolon — while the last one technically doesn’t require it, it’s best practice to always include it.

Multiple Declarations Example

<style>
  .card {
    background-color: #f4f4f4;
    padding: 16px;
    border-radius: 8px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  }
</style>

<div class="card">A styled card</div>
Output

Comments

CSS comments are written between /* and */. They can span multiple lines and, like HTML comments, are ignored entirely by the browser.

CSS Comments

/* This is a single-line comment */

/*
  This is a
  multi-line comment
*/
p {
  color: black; /* inline comment */
}

Missing Semicolons Break the Next Declaration

Forgetting a semicolon merges the next declaration into the value of the previous one, silently breaking both. Always terminate every declaration with a semicolon.

Best Practice

Format one declaration per line and always end with a semicolon — it makes diffs cleaner and prevents subtle bugs when a new declaration is added later.

Interview Questions

Quick Quiz

1. What separates a property from its value in a declaration?

2. What character ends a CSS declaration?

3. How are CSS comments written?