Aral Code

Getting Started Lesson 2 of 33

CSS Syntax — Selectors, Properties, Values and Comments

Learn how to read and write a CSS rule — selector, curly braces, property, colon, value and semicolon — plus how to write comments.

Every CSS file is made of rules. Once you can read one rule, you can read any CSS file in the world. Let's take one apart.

p {
  color: navy;
  font-size: 18px;
}

The parts of a rule

  • p is the selector. It picks which elements the rule applies to — here, every paragraph.
  • The curly braces { } hold the declarations, the actual style instructions.
  • color and font-size are properties, the thing you want to change.
  • navy and 18px are values, what you want to change it to.
  • A colon : separates each property from its value.
  • A semicolon ; ends each declaration.

You can read the rule out loud as a sentence: "For every p, set the color to navy and the font size to 18 pixels."

Example Reading a rule
<style>
  p {
    color: navy;
    font-size: 18px;
  }
</style>
<h2>Announcement</h2>
<p>Classes are suspended tomorrow because of the typhoon. Stay safe!</p>
<p>Online modules will be posted in the group chat.</p>

One rule, many declarations

A rule can have as many declarations as you need. Each goes on its own line, ending with a semicolon. The order of declarations usually doesn't matter.

Example Several declarations in one rule
<style>
  h1 {
    color: white;
    background-color: #0038A8;
    padding: 12px;
    text-align: center;
    font-family: Georgia, serif;
  }
</style>
<h1>Bayan Kong Mahal</h1>

Many rules

A style sheet is just rules one after another. The browser reads all of them and applies each one to the elements its selector picks.

Example Several rules
<style>
  body {
    font-family: Arial, sans-serif;
    background-color: #f3f6f4;
  }
  h1 {
    color: #0f5c4d;
  }
  p {
    line-height: 1.6;
  }
  strong {
    color: #b4372b;
  }
</style>
<h1>Recess Reminder</h1>
<p>Please throw your trash in the <strong>correct bin</strong>. Plastic bottles go in the yellow bin, food waste in the green bin.</p>

Grouping selectors

When several elements need the same style, list their selectors separated by commas instead of writing the same rule three times:

/* Instead of this: */
h1 { color: #0f5c4d; }
h2 { color: #0f5c4d; }
h3 { color: #0f5c4d; }

/* Write this: */
h1, h2, h3 {
  color: #0f5c4d;
}

Spaces and line breaks

CSS ignores extra spaces and line breaks, so these two rules are identical:

h1{color:red;text-align:center}

h1 {
  color: red;
  text-align: center;
}

The second one is much easier to read. Most developers put each declaration on its own line, indented by 2 spaces. The last semicolon before } is optional, but always adding it prevents a common bug when you add a new line later.

Comments

A CSS comment starts with /* and ends with */. The browser ignores everything between them. Use comments to label sections or explain a tricky value.

/* ===== Header ===== */
header {
  background: #0f5c4d; /* school color */
}

/*
  Comments can also
  span many lines.
*/

Comments are also handy for testing: "comment out" a line to see what the page looks like without it.

Example Comment out a line
<style>
  p {
    color: white;
    background: purple;
    /* padding: 20px; */
  }
</style>
<p>Remove the /* and */ around padding to turn it back on.</p>

The most common syntax mistakes

When a style "doesn't work", check these first:

  1. Missing semicolon — color: red font-size: 20px; breaks both declarations.
  2. Missing closing brace } — can break every rule after it.
  3. Equals sign instead of colon — color = red; is not CSS.
  4. Misspelled property — colour, font-colour, backround. CSS uses American spelling: color, background.
  5. Space between number and unit — 20 px is wrong; write 20px.
Example Find the bugs
<style>
  /* This rule has 3 mistakes. Can you fix them? */
  h1 {
    colour: green;
    font-size: 40 px;
    background-color: yellow
    text-align: center;
  }
</style>
<h1>Fix my CSS!</h1>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. In h2 { color: teal; }, what is h2?
2. What separates a property from its value?
3. How do you write a comment in CSS?
4. How do you give h1, h2 and h3 the same color in one rule?
5. What happens when the browser finds colour: red;?

By Sir Reyel · Last updated 2026-12-04