CSS: CSS Syntax

🗓 Last updated: 2026-11-30👤 Written by: Casey✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

Properties in this section: color border font-size background

1. Basic Structure of CSS Rules

CSS styles consist of a series of rulesets, each containing a Selector and a Declaration block. The browser parses these rules and applies them to corresponding elements in the HTML document.

The basic syntax structure is:

CSS
selector {
  property1: value1;
  property2: value2;
  /* More declarations... */
}

(1) Component Breakdown

Selector: The selector is the core of CSS, used to "select" the HTML elements you want to style. It can be an HTML tag name, class name, id name, or more complex selector combinations.

Declaration block: Wrapped in curly braces {}, containing one or more declarations.

Declaration: Consists of a property and value separated by an English colon :, with each declaration ending with an English semicolon ;.

📌 Syntax rules: CSS property names and values are separated by colons :, and each declaration ends with a semicolon ;. Semicolons are easy to forget, but missing one can cause subsequent styles to fail — make it a habit to add semicolons to every line.

Properties: Properties are specific features in CSS used to define element styles. Each property has a name describing the type of style to modify, such as color, border, font-size, etc.

Values: Define the specific visual effect of a property, composed of numbers with units or keywords.

(2) A Complete Example

▶ Example

▶ Try it Yourself

This rule means: select all <h1> elements on the page, change their text color to blue, set font size to 24 pixels, and center-align them.


2. CSS Comments

💡 Tip: CSS comments use /* */, different from HTML's <!-- -->. Comments help you mark code sections, especially useful for team collaboration.

CSS comments use the /* comment content */ syntax. The browser won't parse comment content. CSS has only this one commenting style — whether single-line or multi-line, everything goes between /* */.

▶ Example

▶ Try it Yourself

Comments are very useful in development — you can use them to explain what certain styles do, or temporarily "disable" some styles for debugging.


3. Syntax Notes

(1) Semicolons are separators

Every declaration must end with an English semicolon ;, telling the browser the declaration is complete. The semicolon after the last declaration can be omitted, but it's recommended to add it for consistency and to make it easier to add new styles later.

▶ Example

▶ Try it Yourself

(2) Colons between property names and values

The colon must be an English colon :. There can be spaces between the property name and colon, but it's recommended to maintain a consistent style.

(3) Using quotes

Some property values (like font names) that contain spaces need to be wrapped in quotes:

▶ Example

▶ Try it Yourself

(4) Case sensitivity

CSS property names and values are not case-sensitive in most cases, but lowercase is conventionally used. The only exception is that some values (like font names, URL paths) may need to preserve their original case.

▶ Example

▶ Try it Yourself

4. Common Syntax Errors

Beginners often make the following mistakes when writing CSS — be careful to avoid them:

Error Correct
Missing semicolon in color: red color: red;
Using Chinese colon color:red Use English colon color: red
Mismatched curly braces Ensure { and } appear in pairs
Misspelled property names Check property names are correct (e.g., background not backgound)

Properties in this section: color border font-size background

❓ FAQ

Q What is CSS and what is it used for?
A CSS (Cascading Style Sheets) controls how a page looks — colors, fonts, spacing, and layout. HTML provides the structure; CSS paints it. When the browser reads a rule like h1 { color: red; }, it applies that style to every matching element. Without CSS, every page is just black text on white.
Q How do I style a div with CSS?
A Select it, then declare properties. To style every div: div { width: 300px; padding: 20px; background: #f0f0f0; }. To style one specific div, give it a class: <div class="box"> in HTML, then .box { border: 1px solid #ccc; } in CSS.
Q Why is my CSS not working?
A Check in this order: a broken selector (misspelled class name or missing .), a misspelled property or value (colour doesn't exist), a missing colon or semicolon that kills the declaration, a stylesheet that never loaded (check the Network tab in DevTools), or a higher-specificity rule overriding yours (crossed-out properties in the Styles panel). Open DevTools first — most issues are visible right there.
Q What are the two parts of CSS syntax?
A A selector and a declaration block. The selector decides which elements to target; the { } block decides what to change, with each declaration written as property: value; — for example p { color: blue; font-size: 16px; }. A missing colon invalidates the declaration and a missing semicolon swallows the next one, so end every declaration with a semicolon, including the last.
Q How do I apply CSS to an HTML page? How many ways are there?
A Three: inline styles written on the tag with style="color: red", embedded rules inside a <style> tag, or an external file linked with <link rel="stylesheet" href="style.css">. Real projects use the external file almost exclusively — it's easier to maintain and gets cached. Inline styles are for quick debugging. See the "CSS Introduction Methods" lesson for a full comparison.
Q How do I add a border in CSS?
A The border shorthand takes three values: width, style, color — e.g. border: 1px solid #333;. For a single side, write border-bottom: 2px dashed red;; to remove a border, border: none;. Pair it with border-radius: 8px; for rounded corners.
Q Is CSS worth learning? Why do people say it's hard?
A The syntax itself is easy — you can get through it in a day. What's hard is predicting the result once cascading, inheritance, and specificity stack up. Still, HTML + CSS is the minimum entry ticket for building anything on the web. Practice in the order "selectors → box model → layout" and you'll be building static pages within a few weeks.
Q How do comments work in CSS? Same as HTML comments?
A No. CSS comments are /* comment */; HTML uses <!-- comment -->. CSS has no // single-line comment — writing one doesn't comment anything out and can break the next rule.
Q Can I put multiple selectors in one CSS rule?
A Yes, separate them with commas: h1, h2, h3 { color: red; } targets all three at once without repeating the rule. Just don't mix up the separators — commas separate selectors, semicolons separate declarations.

📖 Summary

📝 Exercises

  1. Create an HTML file using a <style> tag to set red text and 28px font size for h1 elements, and blue text and 16px font size for p elements.
  2. Add comments in the style code explaining what each style does.
  3. Intentionally write one declaration incorrectly (e.g., write colour instead of color), open the page in a browser, and see what the Styles panel in Developer Tools shows.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏