CSS3 Syntax
CSS syntax is the set of rules for how a stylesheet is written: which element to target, which property to change, and what value to give it. Every style you write follows the same basic pattern, so once the pattern is clear you can read and write any CSS file.
Anatomy of a rule
A CSS rule, also called a ruleset, has two parts. The selector chooses which elements the rule applies to. The declaration block, inside curly braces, lists what to change.
/* selector declaration block */
h1 {
color: #1a1a2e; /* property: value; */
font-size: 2rem;
margin-bottom: 0.5em;
}
.price,
.discount {
font-weight: 700;
}
nav a:hover {
text-decoration: underline;
}
Each declaration is a property name, a colon, a value, and a semicolon. The semicolon separates declarations; it is optional after the last one, but adding it every time saves you from bugs when you add another line later.
Selectors you will meet first
| Selector | Matches |
|---|---|
p | Every <p> element (type selector). |
.price | Elements with class="price". |
#header | The element with id="header". |
nav a | <a> elements anywhere inside <nav>. |
a:hover | Links while the pointer is over them (pseudo-class). |
.price, .discount | Both groups, separated by a comma. |
A comma groups selectors so they share one declaration block. A space means "inside of", which is a completely different thing, and mixing the two up is a classic beginner mistake.
Values, units and comments
Values can be keywords (bold, center), numbers with units (16px, 1.5rem, 50%), colours (#ff6600, rgb(255 102 0)), or functions like calc(100% - 2rem). A zero length needs no unit, so margin: 0 is fine. Any other number needs one: width: 200 is invalid and gets ignored.
Comments start with /* and end with */. CSS has no single-line // comment, and using one can quietly break the rule that follows.
What the browser does with errors
CSS is forgiving. When the browser finds a declaration it does not understand, such as a misspelled property or a value without a unit, it skips just that declaration and carries on with the rest. This is great for compatibility, but it also means mistakes fail silently. Nothing crashes; the style simply does not appear.
When a style "does not work", check these in order:
- Is the property name spelled correctly, with a hyphen where needed (
background-color, notbackgroundcolor)? - Did a missing colon, semicolon or closing brace swallow the next declaration or rule?
- Does the selector actually match the element? A class needs a dot, an id needs a hash.
- Is another rule with higher specificity overriding it? The browser's developer tools show crossed-out declarations.
Where CSS can be written
The same syntax works in an external .css file linked with <link rel="stylesheet">, inside a <style> element in the page head, or in a style attribute. In the attribute version there is no selector and no braces, only declarations. External files are the usual choice, since one file can style a whole site.
Takeaway: selector, braces, then property: value; pairs. Keep that shape consistent and use developer tools to spot the declarations the browser skipped.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.