CSS Syntax is the grammar of writing valid CSS rules. Once you understand the syntax, every stylesheet becomes easier to read. CSS may look simple because a rule can be only a few lines long, but each symbol has a job. Selectors choose elements. Curly braces hold declarations. Properties describe what should change. Values describe how it should change. Colons, semicolons, commas, and comments all affect how the browser reads the stylesheet.
CSS is forgiving in some situations, but that does not mean syntax is unimportant. A missing brace can break many rules below it. A missing semicolon can stop the next declaration from working. A wrong selector can silently target nothing. A wrong value can be ignored by the browser. Clean syntax prevents many beginner bugs before they happen.
Basic CSS Syntax
The most common CSS rule has a selector followed by a declaration block. The declaration block is wrapped in curly braces. Inside the block, each declaration contains a property, a colon, a value, and usually a semicolon.
selector {
property: value;
}
Here is a real example:
h1 {
color: navy;
font-size: 36px;
margin-bottom: 16px;
}
This rule targets all h1 elements. It sets the text color, font size, and bottom margin. The browser reads the selector first, then applies each declaration to the matching elements.
| Syntax Part | Example | Purpose |
|---|---|---|
| Selector | h1 | Selects the target element |
| Opening brace | { | Starts the declaration block |
| Property | color | Names the style feature |
| Colon | : | Separates property from value |
| Value | navy | Sets the property value |
| Semicolon | ; | Ends the declaration |
| Closing brace | } | Ends the rule |
Selectors in CSS Syntax
A selector tells the browser which elements should receive the style. CSS has many selector types, but beginners should first understand element selectors, class selectors, ID selectors, grouping selectors, and descendant selectors.
p {
color: #333333;
}
.card {
padding: 24px;
}
#main-title {
font-size: 42px;
}
The selector p targets paragraph elements. The selector .card targets elements with the class card. The selector #main-title targets the element with the ID main-title. Class selectors are heavily used in real CSS because they are reusable and usually easier to manage than IDs.
Declaration Blocks
A declaration block contains one or more declarations. It starts with { and ends with }. Everything inside belongs to that selector until the closing brace appears. If the closing brace is missing, the browser may treat later rules incorrectly.
.button {
background-color: blue;
color: white;
padding: 12px 20px;
border-radius: 6px;
}
This declaration block defines a reusable button style. Each declaration sits on its own line for readability. CSS does allow one-line rules, but multi-line formatting is easier to scan and debug.
Properties and Values
A property is the CSS feature you want to change. A value is the setting assigned to that property. Some properties accept numbers, some accept keywords, some accept colors, some accept lengths, and some accept multiple values in a specific order.
.box {
width: 300px;
background-color: lightgray;
border: 2px solid black;
display: flex;
}
In this example, width uses a length value, background-color uses a color keyword, border uses multiple values, and display uses a keyword. The browser validates values based on the property. If a value is invalid for that property, the declaration is ignored.
Semicolons in CSS
A semicolon ends a declaration. The final declaration before a closing brace may work without a semicolon in many browsers, but it is still best practice to include it. This prevents errors when adding new declarations later.
/* Better */
p {
color: black;
line-height: 1.6;
}
/* Risky when edited later */
p {
color: black
line-height: 1.6;
}
In the risky example, the missing semicolon after color: black can cause the next line to be interpreted incorrectly. This kind of bug is common because CSS does not always show a loud error. The rule may simply not work.
Whitespace and Formatting
CSS ignores most extra whitespace. Spaces, line breaks, and indentation are mostly for humans. The browser can read compact CSS, but developers should write readable CSS while editing.
h1{color:navy;font-size:36px;}
The compact version works, but it is harder to maintain. A readable version is better in source files:
h1 {
color: navy;
font-size: 36px;
}
Minified CSS is common in production because it reduces file size. But source CSS should be readable. Build tools can minify CSS automatically when needed.
CSS Comments
CSS comments start with /* and end with */. Comments are ignored by the browser. They are useful for explaining sections, temporarily disabling rules, or leaving notes for other developers.
/* Main page heading */
h1 {
color: navy;
}
CSS does not use // for normal comments. That style belongs to languages like JavaScript, C, and C++. If you write // in a regular CSS file, it is not a valid CSS comment and may cause unexpected parsing behavior.
Grouping Selectors
If multiple selectors need the same declarations, you can group them using commas. This reduces repetition.
h1,
h2,
h3 {
color: #111160;
line-height: 1.2;
}
The comma is important. Without commas, the selector means something else. For example, h1 h2 h3 means an h3 inside an h2 inside an h1, which is not the same as styling all three heading levels.
Descendant and Combined Selectors
Selectors can describe relationships. A space between selectors means descendant. No space means the same element must match multiple selector parts.
.article p {
line-height: 1.7;
}
button.primary {
background-color: blue;
}
The selector .article p targets paragraphs inside an element with class article. The selector button.primary targets button elements that also have the class primary. Small spacing differences can change the meaning of a selector completely.
At-Rules in CSS Syntax
CSS also has at-rules. These start with the @ symbol. Common examples include @media, @import, @font-face, and @keyframes. Some at-rules contain blocks, while others are single statements.
@media (max-width: 600px) {
.container {
padding: 16px;
}
}
This media query applies the nested rule only when the viewport width is 600px or less. Notice that the inner CSS rule still follows normal selector and declaration syntax.
Common CSS Syntax Mistakes
- Missing the closing curly brace after a rule.
- Using an equals sign instead of a colon.
- Forgetting the semicolon between declarations.
- Writing invalid comments using double slashes.
- Using a class selector without the dot.
- Using an ID selector without the hash symbol.
- Forgetting commas when grouping selectors.
- Writing values that the selected property does not accept.
/* Wrong */
.title {
color = red;
}
/* Correct */
.title {
color: red;
}
CSS uses a colon between property and value, not an equals sign. This mistake is common for beginners coming from programming languages where assignment uses =.
How the Browser Handles Invalid CSS
Browsers are designed to keep rendering even when CSS contains mistakes. If one declaration is invalid, the browser usually ignores that declaration and continues with the next one. If the structure of the CSS is broken, such as a missing brace, larger parts of the stylesheet can fail.
This forgiving behavior is useful for compatibility, but it can hide mistakes. A stylesheet may load successfully while some rules silently do nothing. Browser developer tools help reveal this. Invalid or overridden declarations often appear crossed out or marked with warnings.
Readable CSS Syntax Style
Readable CSS is easier to debug and easier to share. Use consistent indentation, one declaration per line, meaningful selector names, and clear grouping. Avoid extremely long selectors unless necessary.
.product-card {
display: grid;
gap: 16px;
padding: 24px;
border: 1px solid #dddddd;
border-radius: 8px;
}
This rule is simple, readable, and reusable. The class name explains the component. The declarations are grouped clearly. A future developer can scan it quickly and understand what it does.
Valid CSS vs Working CSS
A CSS file can be syntactically valid and still not produce the expected design. Valid syntax only means the browser can read the rule. The selector may still target the wrong element, the declaration may be overridden, or the property may not affect that type of element. For example, setting width on an inline element may not behave the way a beginner expects because inline layout has different rules.
This is why CSS debugging needs two checks. First, check whether the syntax is valid. Second, check whether the rule actually applies to the intended element and wins in the cascade. Browser developer tools show matched selectors, inherited values, overridden declarations, and computed styles. The computed tab is especially useful because it shows the final value after all CSS rules have been processed.
Syntax Habits That Scale
Good syntax habits matter more as a stylesheet grows. Use predictable class names, avoid unnecessary IDs for styling, keep related declarations together, and do not create selectors that depend on too much page structure. A selector like .card-title is usually easier to reuse than a long selector such as main section div article h2.
- Use classes for reusable styling patterns.
- Keep selectors as simple as the design allows.
- Group related declarations in a consistent order.
- Use comments for sections, not for explaining every obvious line.
- Check the browser console or developer tools when CSS behaves unexpectedly.
CSS Syntax FAQ
Does CSS require semicolons?
Each declaration should end with a semicolon. The last declaration may sometimes work without one, but always using semicolons is safer and cleaner.
Are spaces important in CSS?
Most extra spaces are not important, but spaces inside selectors can change meaning. For example, .card p and .cardp are completely different selectors.
Can CSS have nested rules?
Regular CSS historically did not support Sass-style nesting in all environments. Modern CSS nesting exists in current browsers, but beginners should first learn normal flat CSS syntax because it is universal and easier to debug.
Why is my CSS rule not applying?
The selector may not match, the property value may be invalid, another rule may override it, the stylesheet may not be loaded, or the syntax above it may be broken.
Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.