To command the web browser and design precise visual layouts, you must understand the exact grammar of CSS. Unlike procedural languages like C, C++, or JavaScript that execute sequential statements, CSS is a declarative styling language. You do not tell the browser how to draw pixels step-by-step; instead, you declare the desired visual state of HTML elements through Rulesets.
A standard CSS ruleset consists of two primary components: a Selector and a Declaration Block.
/* Complete CSS Ruleset Anatomy */
selector {
property: value; /* Declaration 1 */
property: value; /* Declaration 2 */
}
Let us examine a real-world example styling an introductory paragraph:
p.intro {
color: #d22b2b;
font-size: 16px;
line-height: 1.6;
}
| Component | Example from Above | Definition & Grammatical Purpose |
|---|---|---|
| Selector | p.intro |
Points to the HTML element(s) you wish to style. Here, it targets all <p> elements with class intro. |
| Declaration Block | { color: ... line-height: 1.6; } |
Enclosed within curly braces { }, containing one or more declarations that define styling attributes. |
| Property | color, font-size |
The specific stylistic feature of the element you want to modify (e.g., text color, margins, background). |
| Value | #d22b2b, 16px |
The setting applied to the property. It must conform to valid units (px, rem, %, keywords). |
| Colon (:) | : |
Separates the property name from its value. |
| Semicolon (;) | ; |
Terminates a declaration. Omitting a semicolon causes the browser to discard subsequent declarations! |
Whitespace Insensitive: CSS treats spaces, tabs, and newlines interchangeably outside of string literals. Both single-line and formatted multi-line styles parse identically, though multi-line is preferred for readability.
Property & Keyword Case Insensitivity: CSS property names and standard keyword values are case-insensitive (COLOR: RED; works the same as color: red;). However, standard industry practice enforces strict lowercase.
HTML Class & ID Case Sensitivity: In HTML documents, class names and ID identifiers are strictly case-sensitive! A class named .btnPrimary will NOT match class="btnprimary".
Question: What happens if a web designer forgets to add a semicolon at the end of a CSS declaration?
Answer: The browser's CSS parser will fail to delimit the declaration. It will treat the next property name as an invalid continuation of the current value and discard both declarations completely due to CSS Error Handling rules.
Consider this malformed CSS snippet. What color will the heading text render?
h1 {
color: blue
font-weight: bold;
}
h1 {
color: green;
}
Explanation: The first color: blue lacks a semicolon, causing the parser to treat color: blue font-weight: bold; as an invalid value and discard it. However, the second ruleset color: green; is valid, so the text renders in green!
Boost your CSS and front-end development proficiency by exploring these closely connected topics:
Understand margins, borders, padding, content and the universal box-sizing fix.
Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.
Build powerful two-dimensional grid layouts with template areas and fr units.
Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.