|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: The CSS Declaration |
| 3 | +description: "Learn how the CSS Declaration is formed by combining a property and a value, and how semicolons are used to separate multiple declarations within a ruleset." |
| 4 | +keywords: [CSS Declaration, Ruleset Structure, CSS Syntax, Semicolon in CSS, CodeHarborHub] |
| 5 | +sidebar_label: Declaration |
| 6 | +--- |
| 7 | + |
| 8 | +We've covered the **Selector** (who to target) and the **Property/Value** pair (what and how to change it). Now we bring them together to form the complete instruction block: the **CSS Declaration**. |
| 9 | + |
| 10 | +A declaration is the final, complete command that tells the browser exactly one thing to do. |
| 11 | + |
| 12 | +<AdsComponent /> |
| 13 | +<br /> |
| 14 | + |
| 15 | +## Anatomy of a Declaration |
| 16 | + |
| 17 | +A single CSS declaration is a complete statement that always consists of three parts, in this exact order: |
| 18 | + |
| 19 | +1. The **Property** |
| 20 | +2. The **Colon** separator (`:`) |
| 21 | +3. The **Value** |
| 22 | +4. The **Semicolon** terminator (`;`) |
| 23 | + |
| 24 | +### The Syntax |
| 25 | + |
| 26 | +```css title="Declaration Syntax" |
| 27 | +property: value; |
| 28 | +``` |
| 29 | + |
| 30 | +### Example |
| 31 | + |
| 32 | +In the ruleset below, everything inside the curly braces is the **Declaration Block**, and each line ending with a semicolon is a separate **Declaration**: |
| 33 | + |
| 34 | +```css title="styles.css" |
| 35 | +p { |
| 36 | + color: midnightblue; /* <-- DECLARATION 1 */ |
| 37 | + font-size: 18px; /* <-- DECLARATION 2 */ |
| 38 | + line-height: 1.5; /* <-- DECLARATION 3 */ |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## The Critical Role of the Semicolon (`;`) |
| 43 | + |
| 44 | +The semicolon is perhaps the most overlooked, yet most essential, part of the declaration syntax. |
| 45 | + |
| 46 | +### Semicolon as a Separator |
| 47 | + |
| 48 | +The semicolon acts as a **separator** between individual declarations within the curly braces (`{...}`). It tells the browser: "The previous instruction is finished; now look for the next instruction." |
| 49 | + |
| 50 | +### Why You Need It |
| 51 | + |
| 52 | +If you forget the semicolon, the browser treats the end of the first value and the start of the next property as a single, invalid declaration, causing all subsequent styles in that ruleset to fail. |
| 53 | + |
| 54 | +<Tabs> |
| 55 | +<TabItem value="broken" label="Broken Code"> |
| 56 | + |
| 57 | +```css |
| 58 | +.card { |
| 59 | + /* No semicolon after 'white'! The browser fails here. */ |
| 60 | + background-color: white |
| 61 | + padding: 20px; |
| 62 | + border: 1px solid gray; /* All these styles are ignored! */ |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +</TabItem> |
| 67 | +<TabItem value="correct" label="Correct Code"> |
| 68 | + |
| 69 | +```css |
| 70 | +.card { |
| 71 | + /* Semicolon separates the declaration! */ |
| 72 | + background-color: white; |
| 73 | + padding: 20px; |
| 74 | + border: 1px solid gray; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +</TabItem> |
| 79 | +</Tabs> |
| 80 | + |
| 81 | +<AdsComponent /> |
| 82 | +<br /> |
| 83 | + |
| 84 | +### The Last Semicolon |
| 85 | + |
| 86 | +The semicolon on the **very last declaration** (the one just before the closing curly brace `}`) is technically optional. However, it is considered **best practice** to always include it. |
| 87 | + |
| 88 | +:::tip Best Practice: Always Include the Semicolon |
| 89 | +Always include the semicolon on the last line. This prevents syntax errors when you later add a new declaration below it, or when your code is automatically processed and minified by tools. |
| 90 | +::: |
| 91 | + |
| 92 | +## Review: The Complete CSS Ruleset |
| 93 | + |
| 94 | +We can now put all three components together to form the fundamental unit of CSS: the **Ruleset**. |
| 95 | + |
| 96 | +| Ruleset Component | Example | Description | |
| 97 | +| :--- | :--- | :--- | |
| 98 | +| **Selector** | `h3` | Who is being styled? | |
| 99 | +| **Declaration Block** | `{...}` | Contains all the styles for the selector. | |
| 100 | +| **Declaration** | `color: red;` | The complete instruction. | |
| 101 | +| **Property** | `color` | The attribute being changed. | |
| 102 | +| **Value** | `red` | The new setting for the attribute. | |
| 103 | +| **Separator** | `:` | Separates Property and Value. | |
| 104 | +| **Terminator** | `;` | Separates Declarations. | |
| 105 | + |
| 106 | +## Interactive Declaration Demo |
| 107 | + |
| 108 | +Use the live editor to break the syntax by removing the semicolon or the colon. See how a tiny syntax mistake causes the entire ruleset to fail! |
| 109 | + |
| 110 | +<CodePenEmbed |
| 111 | + title="Interactive Declaration Demo" |
| 112 | + penId="RNaRVWV" |
| 113 | +/> |
0 commit comments