Skip to content

Commit b7545e2

Browse files
authored
Merge pull request #84 from codeharborhub/dev-1
Done Syntax Basics
2 parents c50fd3d + 8f8b815 commit b7545e2

File tree

9 files changed

+459
-8
lines changed

9 files changed

+459
-8
lines changed

docs/css/basics/cascading-order.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ The browser doesn't guess; it uses a strict set of rules known as the **Cascade*
1313
2. **Specificity:** How detailed is the selector?
1414
3. **Inheritance:** Does the element automatically get its parent's style?
1515

16+
<AdsComponent />
17+
<br />
18+
1619
## 1. The Cascade: Order and Source
1720

1821
The **Cascade** is the primary system for layering styles. It decides the initial winner based on the origin and order of the stylesheet.
@@ -70,6 +73,9 @@ Let's compare two conflicting selectors:
7073
Never use Inline Styles and try to reserve **ID selectors** (`#`) for JavaScript targeting rather than styling, as their high specificity makes CSS harder to override and maintain.
7174
:::
7275

76+
<AdsComponent />
77+
<br />
78+
7379
## The Power of `!important`
7480

7581
The `!important` keyword is the nuclear option of CSS. When added to a declaration, it instantly makes that style rule **override all other rules**, regardless of specificity.
@@ -130,6 +136,9 @@ Properties related to the **Box Model** and **Layout** do **not** inherit:
130136

131137
The browser applies the `font-family` to the `<p>` elements, but since `border` is not inherited, the `<p>` tags will not have a border unless one is specifically declared for them.
132138

139+
<AdsComponent />
140+
<br />
141+
133142
## Interactive Cascade Order Demo
134143

135144
This interactive example shows how **Specificity** (ID vs. Class) and **Source Order** (last rule wins) determine the final style.

docs/css/basics/external.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ You've explored **Inline** (least scalable) and **Internal** (single-page only)
99

1010
External stylesheets are CSS rules saved in a **separate file** (usually named `style.css` or similar) and then connected to the HTML document using the `<link>` tag.
1111

12+
<AdsComponent />
13+
<br />
14+
1215
## How to Implement External Styles
1316

1417
This method achieves true **separation of concerns** by keeping your presentation code entirely separate from your structural HTML code.
@@ -107,14 +110,19 @@ Now that you know the three ways to add styles, let's briefly look at the order
107110

108111
The Cascade is more complex, involving **Specificity** (which we'll cover in a future lesson), but generally, a style defined closer to the HTML element (inline) overrides a style defined further away (external).
109112

110-
## Summary of CSS Methods
113+
<AdsComponent />
114+
<br />
115+
116+
:::note Summary of CSS Methods
111117

112118
| Method | Where is the Style? | Multi-Page Reuse? | Best for? |
113119
| :--- | :--- | :--- | :--- |
114120
| **Inline** | Inside the element's `style` attribute. | No | Single element, quick debugs, HTML emails. |
115121
| **Internal** | Inside the `<style>` tag in the `<head>`. | No (Requires Copy/Paste) | Single, standalone demos or test pages. |
116122
| **External** | In a separate linked `.css` file. | Yes | **All modern, multi-page websites.** |
117123

124+
:::
125+
118126
### Interactive External Styles Demo
119127

120128
Use the live editor to change the styles and watch the change below.

docs/css/basics/inline.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ You've learned that CSS is how we add style. The next question is: **where do we
99

1010
The easiest, fastest, and most immediate way to apply a style is by using **Inline Styles**. This method involves writing CSS rules directly inside the HTML element you want to style using the **`style` attribute**.
1111

12+
<AdsComponent />
13+
<br />
14+
15+
1216
## How to Write Inline Styles
1317

1418
Inline styles are written directly within the starting tag of any HTML element.
@@ -74,6 +78,9 @@ Inline styles completely violate the golden rule of web development: **Separatio
7478
* **Performance:** The browser has to read and render the style for every single tag, which can slow down page rendering compared to using an external stylesheet.
7579
* **No Advanced CSS:** You cannot use powerful CSS features like **Media Queries** (for responsiveness), **Animations**, or complex selectors with inline styles.
7680

81+
<AdsComponent />
82+
<br />
83+
7784
## When Should I Use Inline Styles?
7885

7986
:::tip Best Practice

docs/css/basics/internal.mdx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ In the previous lesson, we learned about **Inline Styles**, which put CSS direct
99

1010
The next step up is using an **Internal Stylesheet**, also known as **Embedded CSS**. This method keeps the style code in one central location within the same HTML document, offering better organization than inline styles.
1111

12+
<AdsComponent />
13+
<br />
14+
1215
## How to Implement Internal Styles
1316

1417
Internal Stylesheets are created by placing the CSS rules inside a special HTML element: the **`<style>` tag**.
@@ -66,9 +69,12 @@ The biggest drawback is **lack of reusability across multiple documents**. If yo
6669

6770
:::
6871

69-
* **No Caching:** Because the CSS is embedded within the HTML file, the browser cannot cache the stylesheet separately. This means the styles have to be re-downloaded every time a user visits a new page.
70-
* **Bloated HTML:** Adding a lot of CSS code directly into the `<head>` significantly increases the size of the HTML file, which can slightly slow down the initial load time.
71-
* **Maintenance Overhead:** Updating a global style requires editing every single HTML file on your site.
72+
* **No Caching:** Because the CSS is embedded within the HTML file, the browser cannot cache the stylesheet separately. This means the styles have to be re-downloaded every time a user visits a new page.
73+
* **Bloated HTML:** Adding a lot of CSS code directly into the `<head>` significantly increases the size of the HTML file, which can slightly slow down the initial load time.
74+
* **Maintenance Overhead:** Updating a global style requires editing every single HTML file on your site.
75+
76+
<AdsComponent />
77+
<br />
7278

7379
## When is Internal Styling Appropriate?
7480

docs/css/introduction.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import thumbnail from '/img/tutorials/css/frontend-trinity-analogy.png';
1010

1111
CSS (**Cascading Style Sheets**) is the language that brings **style**, **color**, and **life** to web pages. If you can think of **HTML as the basic structure** (like the walls and beams of a house), then **CSS is the interior designer** it defines **how everything looks and feels**.
1212

13+
<AdsComponent />
14+
<br />
15+
1316
## What Exactly *Is* CSS?
1417

1518
CSS is a **stylesheet language** used to describe the **presentation** of HTML documents. It's the magic ingredient that turns a boring text document into a beautiful, engaging user interface.
@@ -77,6 +80,9 @@ This paragraph text is styled using the ruleset above!
7780
</div>
7881
</BrowserWindow>
7982

83+
<AdsComponent />
84+
<br />
85+
8086
## What You’ll Master in this Tutorial
8187

8288
This CodeHarborHub CSS series is designed to take you from knowing nothing about styling to building professional, responsive layouts.
Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,113 @@
1-
<ComingSoon />
1+
---
2+
title: Comments in CSS
3+
description: "Learn how to use single-line and multi-line comments in CSS to annotate your code, improve readability, and temporarily disable styles."
4+
keywords: [CSS Comments, CSS Syntax, Code Annotation, CSS Best Practices, CodeHarborHub]
5+
sidebar_label: Comments
6+
---
7+
8+
You've learned the structure of a CSS ruleset. Now, let's talk about the essential tool for any good developer: **comments**.
9+
10+
Comments are lines of text that the browser completely ignores. They serve a crucial purpose: to make your code understandable to yourself (when you return to it months later) and to other developers.
11+
12+
<AdsComponent />
13+
<br />
14+
15+
## The CSS Comment Syntax
16+
17+
Unlike HTML (which uses `<!-- ... -->`) or JavaScript (which uses `//` or `/**/`), CSS only uses one syntax for commenting.
18+
19+
### Single Syntax: Slash-Asterisk (`/* ... */`)
20+
21+
The syntax for comments in CSS starts with a forward slash followed by an asterisk (`/*`) and ends with an asterisk followed by a forward slash (`*/`).
22+
23+
<Tabs>
24+
<TabItem value="single" label="Single Line Comment">
25+
26+
```css
27+
/* This is a simple comment that explains the rule below. */
28+
body {
29+
font-family: 'Helvetica Neue', sans-serif;
30+
}
31+
```
32+
33+
</TabItem>
34+
<TabItem value="multi" label="Multi-Line Comment">
35+
36+
```css
37+
/*
38+
=====================================
39+
This is a large, multi-line comment.
40+
We use this for section headers
41+
or complex explanations.
42+
=====================================
43+
*/
44+
.main-layout {
45+
display: flex;
46+
}
47+
```
48+
49+
</TabItem>
50+
</Tabs>
51+
52+
### The Key Rule
53+
54+
Regardless of whether your comment spans one line or ten, you **must** use the `/*` and `*/` delimiters. You cannot use the double-slash (`//`) syntax in standard CSS.
55+
56+
<AdsComponent />
57+
<br />
58+
59+
## Why Comments are Crucial
60+
61+
Good comments are a sign of professional, maintainable code. They help in two primary ways: **Documentation** and **Debugging**.
62+
63+
### 1. Code Documentation
64+
65+
Use comments to explain the *why* behind your code, not just the *what*.
66+
67+
| Example of Good Commenting | Purpose |
68+
| :--- | :--- |
69+
| `/* FIX: Added !important here to override third-party library 'reset.css' */` | Explains *why* you used an unconventional/dangerous feature. |
70+
| `/* --- 2.0 Typography Styles --- */` | Provides a clear structure and navigation guide for your large stylesheet. |
71+
| `/* Targets the first button in the navigation bar only */` | Clarifies the intent of a complex or specific selector. |
72+
73+
### 2. Debugging and Temporarily Disabling Code
74+
75+
Comments are a perfect tool for quickly testing and fixing issues. If you suspect a specific block of CSS is causing a layout error, you can instantly disable it by surrounding it with comment tags, without deleting the code.
76+
77+
<Tabs>
78+
<TabItem value="broken" label="Before Commenting (Bug)">
79+
80+
```css
81+
.sidebar {
82+
width: 250px;
83+
/* This line below is causing the layout to break! */
84+
float: left;
85+
background-color: lightgrey;
86+
}
87+
```
88+
89+
</TabItem>
90+
<TabItem value="fixed" label="After Commenting (Testing)">
91+
92+
```css
93+
.sidebar {
94+
width: 250px;
95+
/* float: left; <-- Disabled for testing */
96+
background-color: lightgrey;
97+
}
98+
```
99+
100+
</TabItem>
101+
</Tabs>
102+
103+
<AdsComponent />
104+
<br />
105+
106+
### Interactive Commenting Demo
107+
108+
Use the live editor to comment out the `background-color` and `padding` rules. See how the browser stops applying those styles instantly!
109+
110+
<CodePenEmbed
111+
title="Interactive Declaration Demo"
112+
penId="wBGWeBb"
113+
/>
Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,113 @@
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

Comments
 (0)