CSS Syntax

Selectors, properties and values.

✍️ 3 exercises + code challenge in this lesson

A CSS rule has two parts: a selector and a declaration block.

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

  • The selector (h1) chooses the elements to style.
  • The declaration block is wrapped in curly braces.
  • Each declaration is a property: value pair ending with a semicolon.

Comments in CSS are written as /* comment */.

Key Takeaways: CSS Syntax

Mastering CSS Syntax helps you write clean and reliable CSS code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

CSS Syntax Code Example

CSS
<style>
  /* Make all paragraphs teal and centred */
  p {
    color: #14B8A6;
    text-align: center;
    font-size: 20px;
  }
</style>

<p>Every paragraph is styled by one rule.</p>
<p>Me too!</p>
Free sign-in required · Google login available

CSS Syntax Practice Exercises

Check what you learned in “CSS Syntax”.

/ 3 solved
Fill in the blank

Complete the CSS rule.

p 
  color: red
✓ Correct! Not quite. Try again.
Multiple choice

In "p { color: red; }", what is "color"?

✓ Correct! Not quite. Try again.
Code challenge

Write a rule that sets the font size of every paragraph to 20px.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services