Edit the CSS variables below to customize this component live!
+ +`} + defaultCss={`:root { + --primary-color: #2563eb; + --accent-color: #3b82f6; + --text-color: #ffffff; + --radius: 12px; +} + +.hero-card { + background: linear-gradient(135deg, var(--primary-color), var(--accent-color)); + color: var(--text-color); + padding: 2rem; + border-radius: var(--radius); + font-family: system-ui, sans-serif; + box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); +} + +.hero-card h2 { + margin-bottom: 0.5rem; + font-size: 1.5rem; +} + +.hero-card p { + opacity: 0.9; + margin-bottom: 1.25rem; +} + +.btn { + background-color: #ffffff; + color: var(--primary-color); + border: none; + padding: 0.6rem 1.2rem; + border-radius: 6px; + font-weight: 600; + cursor: pointer; + transition: transform 0.2s ease; +} + +.btn:hover { + transform: translateY(-2px); +}`} + height="340px" +/> + +--- + +## What You Will Learn + +* **Rendering Engine Mechanics:** Understand DOM and CSSOM parsing, paint cycles, GPU layer creation, and browser rendering pipelines. +* **Layout Systems:** Build robust, flex-based, and multi-dimensional grid structures using CSS Grid and Flexbox without utility clutter. +* **Architecture & Specificity:** Master the cascade algorithm, specificity calculation rules, inheritance propagation, and native CSS layers (`@layer`). +* **Modern Responsive Design:** Transition from static media queries to modern content-aware container queries and dynamic fluid typography. + +--- + +## Course Curriculum Breakdown + +| Module | Lecture Title | Core Learning Objective | +| :--- | :--- | :--- | +| **Module 1: Foundations** | Lecture 1: What is CSS & How it Works | DOM tree generation, CSSOM, and rendering engine flow | +| | [Lecture 2: Parts of a CSS Rule](#) | Anatomical breakdown of selectors, declarations, properties, and values | +| | [Lecture 3: Types of CSS Rules](#) | Standard style rules vs. metadata directives and `@`-rules | +| | Lecture 4: The Cascade, Specificity & Inheritance | Specificity scoring math, cascade layers, and value inheritance | +| **Module 2: Selectors & Typography** | Lecture 5: CSS Selectors (Basic to Advanced) | Class, ID, attribute selectors, and relational combinators | +| | Lecture 6: Pseudo-Classes & Pseudo-Elements | Dynamic user state pseudo-classes and DOM insertion pseudo-elements | +| | Lecture 7: Typography & Text Styling | Web font integration via `@font-face`, line heights, and fluid sizing | +| **Module 3: Box Model & Layouts** | Lecture 8: The CSS Box Model | Content-box vs. border-box sizing calculations and margin collapsing | +| | Lecture 9: Display & Positioning | Document flow context, absolute/relative offsets, and sticky placement | +| | Lecture 10: Flexbox Masterclass | Axis distributions, alignment properties, and flex item shrink/grow logic | +| | Lecture 11: CSS Grid Architecture | Two-dimensional template areas, track sizing, and auto-fit placement | +| **Module 4: Modern CSS** | Lecture 12: CSS Custom Properties | Scoped variables, dynamic runtime overrides, and dark mode themes | +| | Lecture 13: Responsive Design & Media Queries | Mobile-first breakpoints, viewport units, and container queries | +| | Lecture 14: Transitions, Transforms & Animations | Keyframe definitions, GPU hardware acceleration, and timing functions | + +--- + +## Prerequisites + +Before taking this course, you should have a basic understanding of: +1. **HTML Fundamentals:** Document structure, tags, attributes, and basic semantic elements. +2. **Text Editor Familiarity:** Basic experience writing code in VS Code or similar development environments. +3. **Browser DevTools:** Knowing how to right-click an element and select **Inspect** to view styles live. + +:::info Recommended Learning Path +Work through the lectures sequentially. Each lesson builds upon the rendering concepts established in prior modules. +::: + + + \ No newline at end of file diff --git a/courses/css/module-1-foundations/Quiz/index.jsx b/courses/css/module-1-foundations/Quiz/index.jsx new file mode 100644 index 000000000..824b9630d --- /dev/null +++ b/courses/css/module-1-foundations/Quiz/index.jsx @@ -0,0 +1,234 @@ +import React, { useState } from 'react'; +import BrowserOnly from '@docusaurus/BrowserOnly'; +import styles from './styles.module.css'; + +const quizData = [ + { + id: 1, + question: "During browser rendering, which tree is constructed by combining DOM elements with visual styles?", + options: [ + "CSSOM Tree", + "DOM Tree", + "Render Tree", + "Layout Tree" + ], + correctAnswer: 2, + explanation: "The Render Tree combines the visible DOM nodes with their associated CSSOM styles to determine what actually gets painted to the screen." + }, + { + id: 2, + question: "In the CSS rule `h1 { color: #2563eb; }`, what term describes the `color: #2563eb;` segment?", + options: [ + "Property", + "Declaration", + "Selector", + "Declaration Block" + ], + correctAnswer: 1, + explanation: "A single property-value pair (terminated by a semicolon) is called a Declaration. The entire set inside curly braces is the Declaration Block." + }, + { + id: 3, + question: "Which of the following `@`-rules MUST be placed at the very top of a stylesheet before standard style rules?", + options: [ + "@media", + "@keyframes", + "@import", + "@supports" + ], + correctAnswer: 2, + explanation: "`@import` and `@charset` directives must appear before any standard style rules; otherwise, browsers will invalidate them." + }, + { + id: 4, + question: "What is the calculated Specificity Vector (ID, Class, Type) for the selector `div#main .card p`?", + options: [ + "(1, 1, 2)", + "(0, 2, 2)", + "(1, 2, 1)", + "(0, 1, 3)" + ], + correctAnswer: 0, + explanation: "`#main` = 1 ID, `.card` = 1 Class, `div` and `p` = 2 Types. Thus, the specificity vector is (1, 1, 2)." + }, + { + id: 5, + question: "Which of these CSS properties is inherited by child DOM nodes by default?", + options: [ + "margin", + "background-color", + "color", + "padding" + ], + correctAnswer: 2, + explanation: "Typography-related properties like `color`, `font-family`, and `line-height` inherit naturally. Box-model properties like `margin` and `padding` do not." + } +]; + +function QuizContent() { + const [currentQuestion, setCurrentQuestion] = useState(0); + const [selectedAnswers, setSelectedAnswers] = useState({}); + const [showResults, setShowResults] = useState(false); + + const handleSelectOption = (optionIndex) => { + setSelectedAnswers({ + ...selectedAnswers, + [currentQuestion]: optionIndex + }); + }; + + const handleNext = () => { + if (currentQuestion < quizData.length - 1) { + setCurrentQuestion(currentQuestion + 1); + } else { + setShowResults(true); + } + }; + + const handlePrev = () => { + if (currentQuestion > 0) { + setCurrentQuestion(currentQuestion - 1); + } + }; + + const handleRestart = () => { + setSelectedAnswers({}); + setCurrentQuestion(0); + setShowResults(false); + }; + + const calculateScore = () => { + let score = 0; + quizData.forEach((q, index) => { + if (selectedAnswers[index] === q.correctAnswer) { + score += 1; + } + }); + return score; + }; + + const question = quizData[currentQuestion]; + const isAnswered = selectedAnswers[currentQuestion] !== undefined; + + return ( +{question.question}
+ ++ Q{idx + 1}: {q.question} +
++ Your Answer: {q.options[userAns]} {isCorrect ? 'β ' : 'β'} +
+ {!isCorrect && ( ++ Correct Answer: {q.options[q.correctAnswer]} +
+ )} +`) inside `.card` must inherit the parent container's font settings automatically. +* The `.card-footer a` link must explicitly set `color: inherit` to match paragraph text rather than the browser's default user-agent blue (`#0000ee`). +* The `.reset-badge` span must set `color: initial` to revert directly to standard User-Agent rendering. + +```html title="Card Component Structure" +
Your progress has been logged to the CodeHarborHub ecosystem.
+ +Your progress has been logged to the CodeHarborHub ecosystem.
+ +`} +defaultCss={`/* Challenge 2: Refactor the CSS to use modern styling and correct inheritance */ +.card { + color: #475569; + font-family: system-ui, -apple-system, sans-serif; + background: #f8fafc; + padding: 1.5rem; + border-radius: 8px; + border: 1px solid #e2e8f0; +} +.card p { + color: inherit; + margin: 0.5rem 0 1rem 0; +} +.card-footer { + display: flex; + justify-content: space-between; + align-items: center; + font-size: 0.875rem; +} +.card-footer a { + color: #2563eb; + text-decoration: none; +} +.card-footer a:hover { + text-decoration: underline; +} +.reset-badge { + color: #059669; + font-weight: 500; +}`} + +height="480px" /> + +## Task Self-Check Scorecard + +In this challenge, you should have successfully applied the following CSS concepts: + +* **Specificity Math:** Calculated specificity vectors to resolve style conflicts. +* **Explicit Inheritance Control:** Used `inherit` and `initial` to manage property inheritance in nested elements. +* **Source Order Resolution:** Ensured that the order of CSS rules does not override specificity unless intended. +* **Modern CSS Practices:** Applied semantic class names, responsive design principles, and clean code formatting. \ No newline at end of file diff --git a/courses/css/module-1-foundations/lecture-1-what-is-css.md b/courses/css/module-1-foundations/lecture-1-what-is-css.md new file mode 100644 index 000000000..bfe573bac --- /dev/null +++ b/courses/css/module-1-foundations/lecture-1-what-is-css.md @@ -0,0 +1,175 @@ +--- +id: what-is-css +title: "What is CSS & How it Works" +sidebar_label: "Lecture 1" +sidebar_position: 1 +description: Understand what CSS is, how browsers process stylesheets, and the step-by-step rendering pipeline from DOM to pixels. +tags: ["css", "lecture", "foundations", "rendering", "browser"] +--- + +**CSS (Cascading Style Sheets)** is the language used to specify the presentation, layout, and visual formatting of web pages written in HTML. + +While **HTML** defines the structural raw content (headings, paragraphs, buttons) and **JavaScript** adds dynamic behavior, **CSS** controls visual layout, typography, colors, animations, and responsive screen adaptation. + +:::info Why "Cascading"? +The term "cascading" refers to the way CSS rules are applied in a hierarchical manner, where multiple rules can target the same element. The browser determines which rule takes precedence based on specificity, source order, and importance. +::: + +## How the Browser Engine Renders CSS + +To understand CSS deeply, you must understand how a browser rendering engine (like Blink, Gecko, or WebKit) converts raw code into actual pixels on a screen. + + +```mermaid +flowchart LR + A[HTML Document] -->|Parse| B(DOM Tree) + C[CSS Stylesheets] -->|Parse| D(CSSOM Tree) + B --> E(Render Tree) + D --> E + E --> F(Layout / Reflow) + F --> G(Painting) + G --> H(Compositing) + H --> I[Pixels on Screen] + +``` + +### 1. Constructing the DOM and CSSOM + +* **DOM (Document Object Model):** The browser parses HTML markup into a tree structure of nodes representing page elements. +* **CSSOM (CSS Object Model):** Simultaneously, the browser parses external stylesheets, style tags, and inline styles into a tree structure representing style rules. + +### 2. The Render Tree + +The browser combines the DOM and CSSOM trees into a **Render Tree**. Unlike the DOM, the Render Tree only includes nodes required for visual rendering. Elements styled with `display: none` are excluded entirely from the Render Tree (though elements with `visibility: hidden` remain included). + +### 3. Layout (Reflow) + +The rendering engine calculates the exact geometryβwidth, height, and spatial coordinates (`x, y`)βfor every node in the Render Tree relative to the viewport. + +### 4. Painting + +The engine converts calculated geometry into visual pixels, drawing text, borders, colors, shadows, and backgrounds across software paint layers. + +### 5. Compositing + +The browser merges separate paint layers into a single image displayed on the screen, offloading transformed or animated elements to the GPU (Graphics Processing Unit) when GPU acceleration is triggered. + +--- + +## Interactive Playground: The Impact of CSS + +Explore how plain HTML elements transform when styling rules are applied. Edit the CSS below to observe live rendering changes: + +HTML provides structure. CSS provides typography, color, spacing, and layout context.
+ +`} + defaultCss={`.card-container { + background-color: #0f172a; + color: #f8fafc; + padding: 1.5rem; + border-radius: 12px; + border: 1px solid #1e293b; + font-family: system-ui, sans-serif; +} + +.badge { + background-color: #38bdf8; + color: #0f172a; + font-size: 0.75rem; + font-weight: 700; + padding: 0.25rem 0.5rem; + border-radius: 4px; + text-transform: uppercase; +} + +.card-container h2 { + margin: 0.75rem 0 0.5rem 0; + font-size: 1.25rem; +} + +.card-container p { + color: #94a3b8; + font-size: 0.9rem; + line-height: 1.5; + margin-bottom: 1rem; +} + +.action-btn { + background-color: #2563eb; + color: #ffffff; + border: none; + padding: 0.5rem 1rem; + border-radius: 6px; + font-weight: 600; + cursor: pointer; + transition: background-color 0.2s; +} + +.action-btn:hover { + background-color: #1d4ed8; +}`} + height="360px" +/> + +--- + +## Three Methods to Link CSS to HTML + +There are three ways to apply CSS to HTML documents: + +### 1. External Stylesheet (Recommended) +Links an independent `.css` file via the `` tag placed inside the `` of your HTML document. + +```html title="index.html" + + + +``` + +:::tip Why External? +External stylesheets encourage clean separation of concerns, allow rule caching across pages, and promote scalable styling patterns across large web platforms like CodeHarborHub. +::: + +### 2. Internal Style Tag + +Embeds CSS directly within a ` + + +``` + +### 3. Inline Styles + +Applies styling rules directly to individual HTML elements using the `style` attribute. + +```html title="index.html" +Learn CSS with interactive examples!
+ +`} +defaultCss={`.card { + background-color: #0f172a; + color: #f8fafc; + padding: 1.5rem; + border-radius: 12px; + border: 1px solid #1e293b; + font-family: system-ui, sans-serif; +} +.action-btn { + background-color: #2563eb; + color: #ffffff; + border: none; + padding: 0.5rem 1rem; + border-radius: 6px; + font-weight: 600; + cursor: pointer; + transition: background-color 0.2s; + margin-top: 1rem; +} +.action-btn:hover { + background-color: #1d4ed8; +}`} +/> + +## Common Formatting Practices + +While whitespace (spaces, tabs, line breaks) is ignored by the CSS parser, clean formatting improves readability and team collaboration on platform projects: + +### Single-line vs. Multi-line Rules + +```css title="Single-line vs Multi-line CSS Rules" +/* Multi-line Format (Recommended for readability) */ +.user-avatar { + width: 48px; + height: 48px; + border-radius: 50%; + object-fit: cover; +} + +/* Single-line Format (Used occasionally for small utility classes) */ +.text-center { text-align: center; } +.hidden { display: none; } +``` + +:::tip CodeHarborHub Style Guide +Stick to multi-line rules with 2-space indentation for regular components. Keep property names in lowercase and always include a space after the colon separating properties from values (color: #2563eb;). +::: + +## Summary Checklist + +| Concept | Structure | Quick Example | +|----------|--------------|----------------| +|Rule Set |Selector + Declaration Block |`h1 { color: red; }`| +|Selector |Targets DOM node(s) |`.button`, `#app`, `div`| +|Declaration |Property + Value pair |font-size: 16px;| +|Delimiter |Separates property & value |`:` (Colon)| +|Terminator |Concludes a declaration |`;` (Semicolon)| \ No newline at end of file diff --git a/courses/css/module-1-foundations/lecture-3-types-of-css-rules.md b/courses/css/module-1-foundations/lecture-3-types-of-css-rules.md new file mode 100644 index 000000000..cbe287b5b --- /dev/null +++ b/courses/css/module-1-foundations/lecture-3-types-of-css-rules.md @@ -0,0 +1,176 @@ +--- +id: types-of-css-rules +title: "Types of CSS Rules" +sidebar_label: "Lecture 3" +sidebar_position: 3 +description: "Learn the two primary types of CSS rules: Style Rules and At-Rules. Understand how they differ in syntax, purpose, and behavior within a stylesheet." +tags: ["css", "lecture", "foundations", "syntax", "style rules", "at-rules"] +keywords: ["css", "lecture", "foundations", "syntax", "style rules", "at-rules"] +--- + +Not all CSS rules behave the same way. While most rules target HTML elements to apply styles directly, CSS also provides specialized directives that control how stylesheets parse, handle responsive viewports, import assets, or define complex animation logic. + +In CSS, rules are categorized into two primary types: **Style Rules** and **At-Rules (`@`)**. + +## 1. Style Rules + +**Style Rules** are the backbone of CSS. A style rule selects elements in the DOM tree and applies visual declarations inside a standard declaration block. + +```css title="Example Style Rule" +.hero-title { + font-size: 2.5rem; + color: #0f172a; + line-height: 1.2; +} +``` + +Every standard style rule follows the pattern learned in Lecture 2: a selector followed by a declaration block `{ ... }`. + +## 2. At-Rules (`@`) + +At-Rules are special directives that start with an `@` symbol (e.g., `@import`, `@media`). They instruct the CSS engine on metadata parsing, browser environments, custom typography loading, or conditional rendering logic. + +At-rules fall into two sub-categories based on their syntax structure: + +### A. Statement At-Rules + +These directives end with a single semicolon ; and do not contain nested CSS blocks. + +* `@charset`: Specifies the character encoding used by the stylesheet (must be placed at line 1). +* `@import`: Loads external CSS files into the current stylesheet. + +```css title="Example Statement At-Rules" +@charset "UTF-8"; +@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap'); +``` + +:::warning Parsing Order Rule +`@charset` and `@import` directives must appear before any standard style rules in your document. Any standard style rule placed above an `@import` will cause the browser to invalidate that import. +::: + +### B. Nested Block At-Rules + +These directives contain nested declaration blocks enclosed in `{ ... }`. They apply styles conditionally or create reusable browser instructions. + +|Directive | Purpose | Example Use Case | +|---|---|---| +|`@media` | Applies styles conditionally based on media queries (screen width, dark mode, resolution). | Responsive mobile-first layouts | +|`@keyframes`| Defines animation frames and property values across a timeline. |Keyframe animations | +|`@font-face` | Registers external font files for use across the document. |Custom brand typography | +|`@supports` | Applies styles only if the browser supports a specific CSS feature (Feature Query). | Progressive enhancement | +|`@layer` | Assigns styles to explicit cascade layers to solve specificity conflicts cleanly. | Modern architecture & resets | + +## Code Example: At-Rules in Action + +### 1. The @media Rule (Responsive Conditional Logic) + +```css title="Example Media Query At-Rule" +/* Base style for mobile devices */ +.nav-menu { + display: flex; + flex-direction: column; +} + +/* At-Rule overriding styles for tablet screens and above */ +@media (min-width: 768px) { + .nav-menu { + flex-direction: row; + justify-content: space-between; + } +} +``` + +### 2. The @keyframes Rule (Animation Keyframes) + +```css title="Example Keyframe Animation At-Rule" +@keyframes pulse { + 0% { transform: scale(1); opacity: 1; } + 50% { transform: scale(1.05); opacity: 0.8; } + 100% { transform: scale(1); opacity: 1; } +} + +.badge-live { + animation: pulse 2s infinite ease-in-out; +} +``` + +## Interactive Playground: Style Rules vs. At-Rules + +Experiment with the interactive editor below. Try adjusting the `@keyframes `animation parameters or changing the conditional properties inside the `@media `rule to see how the live preview reacts: + +This is a sample card description.
+ +This is the lead paragraph acting as the main summary for the internal card component.
+This is a secondary paragraph containing an internal dashboard link.
+This is the lead paragraph acting as the main summary for the external resource component.
+Discover advanced layouts by reading the documentation on the MDN Web Docs website.
+This card has an external link. The entire container gets a unique box shadow.
+ Visit External Site +This link ends with "/course", which automatically appends an arrow icon.
+ View our comprehensive ++ Modern CSS techniques allow layout and typography to scale dynamically relative to the viewport size. This eliminates the need for rigid media queries. +
+ + +
+ By using the modern clamp() function, we establish clear minimum and maximum size boundaries. This ensures that text remains highly readable on small mobile screens as well as massive ultra-wide desktop monitors.
+
Edit the CSS on the left to update this UI in real-time!
\n