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.