html
/

CSS Syntax

Last Sync: Today

On this page

11
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

html

CSS Syntax

What is CSS Syntax?

CSS syntax defines how styles are written and applied to HTML elements using selectors and declaration blocks.

Basic Structure

CSSRead-only
1
selector {
  property: value;
}

Example

CSSRead-only
1
p {
  color: blue;
  font-size: 16px;
}

Parts of CSS Rule

PartDescription
SelectorTargets HTML element (e.g., p, .class, #id)
PropertyStyle attribute (e.g., color, font-size)
ValueDefines style (e.g., blue, 16px)
DeclarationProperty + value pair

Multiple Declarations

CSSRead-only
1
h1 {
  color: red;
  text-align: center;
  margin: 10px;
}

Grouping Selectors

CSSRead-only
1
h1, h2, p {
  color: green;
}

Comments in Syntax

CSSRead-only
1
/* This is a comment */
p { color: black; }

Whitespace & Formatting

CSS ignores extra spaces and line breaks, but proper formatting improves readability.

Best Practices

  • Use consistent indentation
  • Write one declaration per line
  • Use meaningful selectors
  • End each declaration with semicolon

Common Mistakes

  • Missing semicolons
  • Incorrect selector usage
  • Typos in property names
  • Forgetting curly braces {}

Conclusion

Understanding CSS syntax is the foundation of styling web pages. Proper structure ensures maintainable and error-free code.

Try it yourself

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

Test Your Knowledge

Q1
of 3

Which is a CSS selector?

A
color
B
p
C
blue
D
16px
Q2
of 3

What defines styling?

A
Selector
B
Property
C
Value
D
Declaration
Q3
of 3

Which symbol ends declaration?

A
:
B
;
C
{}
D
,

Frequently Asked Questions

What is a selector?

It targets HTML elements to apply styles.

What is a declaration?

A combination of property and value.

Is semicolon mandatory?

Yes, especially for multiple declarations.

Previous

css introduction

Next

css selectors

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.