🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

CSS Syntax

AI & DATA SCIENCE // css-syntax

CSS syntax consists of a selector followed by a declaration block in curly braces, containing one or more property-value pairs separated by semicolons.

Syntax

selector {
  property: value;
}

Deep Dive Course

Every CSS rule is built from a selector, which targets specific HTML elements, and a declaration block, delimited by curly braces, containing one or more declarations, each a property name followed by a colon, a value, and a terminating semicolon. Whitespace, indentation, and line breaks inside a rule are entirely optional and purely for human readability, since the CSS parser only cares about the actual tokens, selectors, braces, colons, and semicolons, not how they're formatted, though a missing semicolon between declarations, or a missing closing brace, will break the rule.

1Understanding CSS Syntax

Every CSS rule is built from a selector, which targets specific HTML elements, and a declaration block, delimited by curly braces, containing one or more declarations, each a property name followed by a colon, a value, and a terminating semicolon. Whitespace, indentation, and line breaks inside a rule are entirely optional and purely for human readability, since the CSS parser only cares about the actual tokens, selectors, braces, colons, and semicolons, not how they're formatted, though a missing semicolon between declarations, or a missing closing brace, will break the rule.

💡

Always terminate a declaration with a semicolon, even the last one in a block — while CSS technically allows omitting the semicolon before a closing brace, doing so is a common source of bugs the moment you later add another declaration after it and forget to add the semicolon you skipped.

editor.html
p {
  color: blue;
  font-size: 16px;
}
localhost:3000

2Practical Example

Here is a real-world application of CSS Syntax showing how it is used in production CSS code.

editor.html
.card {
  padding: 20px;
  border: 1px solid gray
}

.title {
  font-weight: bold;
}
localhost:3000

3Best Practices

Follow these guidelines when working with CSS Syntax:

1. Always terminate every declaration with a semicolon, including the last one in a block, to avoid an easy-to-miss bug when adding a new declaration later

2. Use consistent indentation and one declaration per line for readability, even though CSS itself doesn't require any particular formatting

3. Double-check for a missing closing brace when an entire rule, or subsequent rules, appear to not apply at all, since one unclosed rule can silently swallow the rules that follow it

⚠️

Tip: Always terminate a declaration with a semicolon, even the last one in a block — while CSS technically allows omitting the semicolon before a closing brace, doing so is a common source of bugs the moment you later add another declaration after it and forget to add the semicolon you skipped.

editor.html
p {
  color: blue;
  font-size: 16px;
}
localhost:3000

Examples

Example 01Basic Usage
p {
  color: blue;
  font-size: 16px;
}
Example 02Advanced Example
.card {
  padding: 20px;
  border: 1px solid gray
}

.title {
  font-weight: bold;
}

Best Practices

  • Always terminate every declaration with a semicolon, including the last one in a block, to avoid an easy-to-miss bug when adding a new declaration later
  • Use consistent indentation and one declaration per line for readability, even though CSS itself doesn't require any particular formatting
  • Double-check for a missing closing brace when an entire rule, or subsequent rules, appear to not apply at all, since one unclosed rule can silently swallow the rules that follow it

Interview Question

Why does forgetting the semicolon after a declaration's value only sometimes cause a visible bug, rather than always immediately breaking the CSS?

Hint: Think about the specific case of the very last declaration in a block versus one with another declaration following it.

CSS actually permits omitting the semicolon specifically on the very last declaration within a block, right before the closing curly brace, since the brace itself unambiguously signals the end of that declaration — this means a rule with a single declaration, or one where the omitted semicolon happens to be on the final declaration, still parses and works correctly despite the missing punctuation. The bug surfaces later, when a developer adds a new declaration after that previously-last one without first adding back the semicolon it was relying on the closing brace to imply — at that point, the parser reads the new declaration's property name as a continuation of the previous, now-unterminated value, producing an invalid, unparseable declaration that silently fails to apply, often with no obvious error message pointing directly at the real cause. This delayed, easy-to-miss failure mode is exactly why consistently terminating every declaration with a semicolon, including the last one, is considered a much safer default habit.

Exercises

MediumPractice using CSS Syntax in a real scenario.
View Solution
p {
  color: blue;
  font-size: 16px;
}

Frequently Asked Questions

Why does forgetting the semicolon after a declaration's value only sometimes cause a visible bug, rather than always immediately breaking the CSS?

CSS actually permits omitting the semicolon specifically on the very last declaration within a block, right before the closing curly brace, since the brace itself unambiguously signals the end of that declaration — this means a rule with a single declaration, or one where the omitted semicolon happens to be on the final declaration, still parses and works correctly despite the missing punctuation. The bug surfaces later, when a developer adds a new declaration after that previously-last one without first adding back the semicolon it was relying on the closing brace to imply — at that point, the parser reads the new declaration's property name as a continuation of the previous, now-unterminated value, producing an invalid, unparseable declaration that silently fails to apply, often with no obvious error message pointing directly at the real cause. This delayed, easy-to-miss failure mode is exactly why consistently terminating every declaration with a semicolon, including the last one, is considered a much safer default habit.

Related Functions

CSS-CommentsCSS-RulesIncluding-Css-in-Html