NexusBerry Training & Solutions
    NexusBerry Training & Solutions

    NexusBerry provides cutting-edge training and solutions for modern technology needs.

    About Us

    • Privacy Policy
    • Blog
    • Ruls and Regulations
    • Forms

    NexusBerry

    • Contact Us
    • About Us
    • Job Openings
    • Payment Methods

    Resources

    • Internship
    • Tutorials
    • Fee Structure
    • Admission Form

    AI & Data Science

    • Diploma in AI Engineering
    • AI Automation Engineer — AI Agents & Workflow Automation
    • AI for Everyone: Prompt Engineering & AI Tools Bootcamp
    • Data Science Professional with Python & Applied Analytics
    • Full Stack Python Developer
    • Data Science & Machine Learning Bootcamp
    • Python & Django Full Stack Web Developer Bootcamp
    • Data Analytics Bootcamp: Python, SQL & Power BI
    • Agentic AI Engineer: Build, Deploy & Scale Production-Ready Multi-Agent Systems
    • Applied AI & Machine Learning Engineer

    Software Engineering

    • Modern Frontend Developer with React & Next.js
    • MEAN Stack Mastery: Empower Your Web Development Skills for Modern Applications
    • Advanced Web Development with ASP.NET Core and Angular.js
    • Unlocking the Potential of PHP Laravel with Vue.js: A Comprehensive Training Course for Web Development Success
    • Django & React: Build Full-Stack Modern, Scalable Web Applications
    • Full Stack JavaScript Developer Specialization: Building Web and Mobile Apps with MERN Stack and React Native
    • Next.js: The Modern Path to Full Stack Web Development
    • MERN Full Stack Web Development With Nextjs & GenAI

    Mobile App Development

    • Flutter App Development Course: Masterclass with Real-World Projects
    • React Native for Android and iOS App Development

    Digital Marketing

    • Social Media Marketing Course
    • SEO (Search Engine Optimization) Course
    • Full Stack Digital Marketing Course

    Creative Design

    • Master Video Editing: From Beginner to Pro Storyteller
    • Full Stack Designing & Video Editing Course
    • UI/UX Designing
    • Professional Graphic Design Masterclass: Master Visual Branding & AI Tools

    IT Infrastructure & Security

    • The Ultimate Cybersecurity Course: Web App Protection from Scratch
    • DevOps and Cloud Computing
    • CCNA Course

    © Copyright 2025, All Rights Reserved By NexusBerry Training & Solutions

    1. Home
    2. Tutorials
    3. Web Development Fundamentals-V2
    4. Lecture 4: CSS Fundamentals and The Box Model
    css 1/3

    Lecture 4: CSS Fundamentals and The Box Model

    Subscribe to our channel

    Every visual decision on the web comes down to CSS. This is where you learn to control it — not guess at it.


    What You'll Learn

    The building blocks every front-end developer uses every single day:

    • 📝 CSS Rule Anatomy — how selectors, properties, and values fit together into rules that style your page
    • 🔗 3 Ways to Add CSS — inline, internal, and external stylesheets, and when to use each
    • 🎯 Selectors & Specificity — element, class, and ID selectors, plus how browsers decide which rule wins when styles conflict
    • 📦 The Box Model — the single most important concept in CSS; understanding how margin, border, padding, and content interact
    • 🎨 Color Formats — named colors, hex codes, RGB, and RGBA for transparency
    • 🛠️ The Professional Reset — the one box-sizing: border-box rule every real project starts with, and why it saves you from layout headaches

    Why This Matters

    Most CSS frustration — elements the wrong size, layouts breaking unexpectedly, styles not applying — comes from not understanding the Box Model and specificity. Once these two concepts click, CSS stops feeling like guesswork and starts feeling like a tool you control.


    Before You Watch

    Make sure you've completed Lecture 2: HTML Foundations. You should be comfortable with:

    • Writing HTML tags and nesting elements
    • Understanding parent/child relationships in HTML
    • The basic structure of a webpage (head, body, etc.)

    📎 The cheatsheet for this lecture is attached below the video. The Box Model diagram and the Common Properties Quick Reference table are worth keeping open as you code along!

    Lecture 4 Cheatsheet — CSS Fundamentals & the Box Model: Style HTML, Master Selectors, and Own Every Pixel of Spacing

    Use this for: quick syntax lookup · copy-paste code · revision before the next class

    How to use: scan section 1 for the big ideas. Use section 2 as a syntax reference. Grab code from section 3 when practicing. Check section 4 before submitting anything that feels off. If a term in the lecture confused you, section 5 has a plain-English translation. Section 6 points you to the assignment, quiz, and next lecture. Section 7 has interview questions with brief answers for self-testing.

    Scope: this cheatsheet covers exactly what was taught in Lecture 4. Nothing extra. If it's not here, it wasn't in today's class.


    1. Concept summary

    • 📎 CSS attaches three ways — inline (style=""), internal (<style> in head), external (<link rel="stylesheet">); external is the production default
    • ⚖️ Cascade & specificity scored — inline 1000 · ID 100 · class 10 · element 1; ties broken by source order
    • 🌳 Inheritance separated — typographic properties (color, font, line-height) inherit; layout properties (padding, margin, border, width) don't
    • 🧰 Selector toolbox — type · class · ID · descendant (space) · child (>) · adjacent sibling (+) · pseudo-class (:hover) · pseudo-element (::before)
    • 🎨 5 color notations — tomato · #ff6347 · rgb(255 99 71) · rgb(255 99 71 / 0.5) · hsl(9 100% 64%)
    • ✍️ Typography stack — font-family · font-size · font-weight · line-height (unitless) · letter-spacing · max-width: 60ch
    • 🎟️ :root tokens + var() — declare --name: value; once, consume var(--name) everywhere; theme by overriding on a wrapper class
    • 📦 Box model + border-box — content → padding → border → margin; * { box-sizing: border-box; } makes width math sane forever

    2. Quick syntax reference

    CSS attachment methods

    MethodExampleWhen to use
    Inline<h1 style="color: tomato">…</h1>Prototypes only — highest specificity, hardest to override
    Internal<style> h1 { color: tomato; } </style> in <head>Single-page experiments
    External<link rel="stylesheet" href="styles.css">Production default — cacheable, shared across pages

    Selector families

    SelectorExampleMatches
    Typeh1All <h1> elements
    Class.btnEvery element with class="btn" (reusable, weight 10)
    ID#navThe single element with id="nav" (one-of-a-kind, weight 100)
    Descendant.menu aAny <a> anywhere inside .menu
    Child.menu > a<a> that is a direct child of .menu
    Adjacent siblingh2 + pThe <p> that comes immediately after an <h2>
    Attributeinput[type="email"]Inputs whose type attribute equals "email"
    Pseudo-class (state):hover, :focus-visible, :nth-child(odd)Single colon — element state
    Pseudo-element (part)::before, ::after, ::placeholderDouble colon — needs content to render

    Specificity weights

    Selector kindWeight columnExampleScore
    Inline style=""1000<p style="color: blue">(1, 0, 0, 0)
    ID100#alert(0, 1, 0, 0)
    Class / pseudo-class / attribute10.warning, :hover, [type="email"](0, 0, 1, 0)
    Element / pseudo-element1p, ::before(0, 0, 0, 1)

    Read scorecards left-to-right as a 4-digit number — (0, 1, 0, 0) beats (0, 0, 9, 9) because the IDs column wins first. Ties go to source order.

    Color notations

    NotationExampleWhen to use
    NamedtomatoPrototypes only (~140 names)
    Hex#ff6347Design handoffs (Figma exports hex)
    rgb() (modern)rgb(255 99 71)Anywhere — no commas in modern syntax
    rgb() with alphargb(255 99 71 / 0.5)When you need transparency (alpha is 0–1, not 0–100)
    hsl()hsl(9 100% 64%)When you want to shift hue/lightness algorithmically

    Typography properties

    PropertyExampleEffect
    font-family'Inter', system-ui, sans-serifTypeface stack — fallback chain if first not loaded
    font-size18px / 1remrem = root font-size; em = element's own
    font-weight400 / 700100–900; pick 1–2 weights only when using web fonts
    line-height1.6Unitless — scales with font-size automatically
    letter-spacing0.01emTracks slightly opens/tightens
    max-width60chch = width of one 0; ~60ch is ideal reading measure

    Box model properties

    PropertyExampleBehavior
    width / heightwidth: 200px;Sizes the content layer by default (content-box)
    paddingpadding: 20px;Space inside the border, around the content
    borderborder: 5px solid steelblue;Line around the padding
    marginmargin: 16px;Space outside the border, separating siblings
    box-sizingbox-sizing: border-box;Make width include padding+border (sane math)
    border-radiusborder-radius: 12px; / 50%Round corners; 50% = circle
    box-shadowbox-shadow: 0 4px 12px rgb(0 0 0 / 0.08);<x-offset> <y-offset> <blur> <color>

    Custom properties (CSS variables)

    PatternExamplePurpose
    Declare on :root:root { --primary: #2563eb; }Global token, available everywhere
    Consume with var()background: var(--primary);Read the token's current value
    Theme override.theme-dark { --primary: #ff8c66; }Re-define inside a wrapper class — descendants pick the closer value
    Naming pattern--<category>-<role>-<modifier>e.g., --color-primary-hover, --font-size-lg

    3. Code snippets

    Slide 6 — The same <h1> styled three ways

    Inline beats internal beats external when specificity is otherwise tied.

    1<!DOCTYPE html>
    2<html lang="en">
    3 <head>
    4 <meta charset="UTF-8" />
    5 <title>Cascade Demo</title>
    6 <link rel="stylesheet" href="styles.css" />
    7 <style>
    8 h1 { color: rebeccapurple; }
    9 </style>
    10 </head>
    11 <body>
    12 <h1 style="color: tomato;">Hello CSS</h1>
    13 </body>
    14</html>
    1/* styles.css */
    2h1 { color: steelblue; }

    Renders: "Hello CSS" in tomato — inline wins. Open DevTools → Styles panel; the two losers have a strikethrough.


    Slide 7 — Anatomy of a CSS rule

    Selector · { block } · property: value; — three semicolons for three declarations.

    1h1 {
    2 color: tomato;
    3 font-size: 2rem;
    4 font-weight: 700;
    5}

    Renders: heading in tomato, 2× the body font-size, bold.


    Slide 15 — Element / class / ID selectors

    Bare name = element · . = class (reusable) · # = ID (one-of-a-kind).

    1<!DOCTYPE html>
    2<html lang="en">
    3 <head>
    4 <meta charset="UTF-8" />
    5 <title>Selectors Demo</title>
    6 <style>
    7 li { color: gray; }
    8 .featured { color: tomato; font-weight: 700; }
    9 #me { color: rebeccapurple; text-decoration: underline; }
    10 </style>
    11 </head>
    12 <body>
    13 <nav>
    14 <ul>
    15 <li>Home</li>
    16 <li class="featured">About</li>
    17 <li id="me" class="featured">Contact</li>
    18 </ul>
    19 </nav>
    20 </body>
    21</html>

    Renders: "Home" gray · "About" tomato + bold · "Contact" purple + underlined + bold (it gets .featured AND #me — non-conflicting properties merge).


    Slide 16 — Combinators — descendant, child, sibling

    Space = anywhere inside · > = direct child only · + = the very next sibling.

    1/* descendant — any <a> anywhere inside .menu */
    2.menu a { color: navy; }
    3
    4/* direct child only — <a> that is an immediate child of .menu */
    5.menu > a { background: gold; }
    6
    7/* adjacent sibling — the <p> that comes RIGHT AFTER an <h2> */
    8h2 + p { font-style: italic; }
    1<div class="menu">
    2 <a href="#">Direct child link</a>
    3 <ul>
    4 <li><a href="#">Nested link</a></li>
    5 </ul>
    6</div>
    7<h2>A heading</h2>
    8<p>This paragraph is italic — it follows an h2.</p>
    9<p>This one is not — it's the second paragraph.</p>

    Renders: "Direct child link" navy + gold background · "Nested link" navy only (descendant, not direct child) · first <p> italic; second <p> plain.


    Slide 17 — Pseudo-classes & pseudo-elements

    Single colon for state · double colon for parts · pseudo-elements need content.

    1/* state */
    2li { padding: 4px 8px; transition: background 0.15s; }
    3li:hover { background: lightyellow; cursor: pointer; }
    4li:focus { outline: 2px solid steelblue; }
    5
    6/* striping */
    7li:nth-child(odd) { background: #f7f7f7; }
    8
    9/* generated content — needs content: */
    10li.featured::before { content: "★ "; color: gold; }

    Renders: hover → light-yellow background · keyboard focus → blue outline · odd rows faint gray stripe · featured items show a gold star prefix.


    Slide 18 — Advanced selectors (paste demo)

    Production-grade patterns: attribute selectors, :focus-visible, :nth-of-type, :not(), ::placeholder.

    1/* attribute selectors */
    2input[type="email"] { border: 2px solid steelblue; }
    3input[type="email"]:invalid { border-color: tomato; }
    4a[target="_blank"]::after { content: " ↗"; color: #888; }
    5
    6/* focus-visible — keyboard focus only, not mouse */
    7button:focus-visible {
    8 outline: 3px solid #2563eb;
    9 outline-offset: 2px;
    10}
    11
    12/* nth-of-type — every 3rd <p>, ignoring sibling elements */
    13p:nth-of-type(3n) { color: tomato; }
    14
    15/* :not — invert a selector */
    16button:not(.primary) { opacity: 0.7; }
    17
    18/* placeholder text styling */
    19input::placeholder { color: #aaa; font-style: italic; }

    Renders: email input gets blue border (red when invalid) · external links get a ↗ arrow · keyboard tab to a button shows thick blue outline (mouse click does not) · every third <p> tomato · non-primary buttons dimmed.


    Slide 24 — Same color, five notations

    All five render the same orange — pick by use case.

    1<!DOCTYPE html>
    2<html lang="en">
    3 <head>
    4 <meta charset="UTF-8" />
    5 <title>Colors</title>
    6 <style>
    7 div { width: 200px; height: 60px; margin: 8px; color: white;
    8 display: inline-block; padding: 16px; font-family: sans-serif; }
    9 .c1 { background: tomato; }
    10 .c2 { background: #ff6347; }
    11 .c3 { background: rgb(255 99 71); }
    12 .c4 { background: rgb(255 99 71 / 0.5); color: black; }
    13 .c5 { background: hsl(9 100% 64%); }
    14 </style>
    15 </head>
    16 <body>
    17 <div class="c1">tomato</div>
    18 <div class="c2">#ff6347</div>
    19 <div class="c3">rgb()</div>
    20 <div class="c4">rgba 50%</div>
    21 <div class="c5">hsl()</div>
    22 </body>
    23</html>

    Renders: five tomato-colored boxes side by side · boxes 1, 2, 3, 5 identical opaque orange · box 4 half-transparent.


    Slide 25 — Typography stack

    Six properties layered onto one paragraph.

    1<!DOCTYPE html>
    2<html lang="en">
    3 <head>
    4 <meta charset="UTF-8" />
    5 <title>Typography</title>
    6 <style>
    7 p {
    8 font-family: Georgia, serif;
    9 font-size: 18px;
    10 font-weight: 400;
    11 line-height: 1.6;
    12 letter-spacing: 0.01em;
    13 text-align: left;
    14 max-width: 60ch;
    15 }
    16 </style>
    17 </head>
    18 <body>
    19 <p>The quick brown fox jumps over the lazy dog. This is a paragraph
    20 long enough to wrap onto multiple lines so you can see line-height
    21 and letter-spacing actually do something visible.</p>
    22 </body>
    23</html>

    Renders: a wide paragraph in Georgia serif at 18px, generous 1.6 line-height, slightly opened letter-spacing, capped at ~60 characters per line.


    Slide 28 — :root tokens + var() + theme override

    Same .btn class, two visual results — variables resolve to the nearest declaration.

    1<!DOCTYPE html>
    2<html lang="en">
    3 <head>
    4 <meta charset="UTF-8" />
    5 <title>Tokens</title>
    6 <style>
    7 :root {
    8 --primary: #ff6347;
    9 --bg: white;
    10 --text: #222;
    11 --radius: 8px;
    12 }
    13 body { background: var(--bg); color: var(--text); padding: 32px;
    14 font-family: system-ui, sans-serif; }
    15 .btn {
    16 background: var(--primary);
    17 color: white;
    18 padding: 12px 24px;
    19 border: none;
    20 border-radius: var(--radius);
    21 font-size: 16px;
    22 cursor: pointer;
    23 }
    24 .theme-dark {
    25 --primary: #ff8c66;
    26 --bg: #111;
    27 --text: #eee;
    28 }
    29 </style>
    30 </head>
    31 <body>
    32 <button class="btn">Light mode</button>
    33
    34 <div class="theme-dark" style="padding: 32px; margin-top: 16px;">
    35 <button class="btn">Dark mode (override)</button>
    36 </div>
    37 </body>
    38</html>

    Renders: top button on white bg with tomato fill · bottom button inside .theme-dark has salmon-pink fill on a dark background · same .btn class, different result.


    Slide 29 — Reusable starter token block

    Paste at the top of every new stylesheet. 8 colors · 4 font sizes · 3 radii · 2 fonts.

    1:root {
    2 /* color palette */
    3 --color-primary: #2563eb;
    4 --color-primary-hover: #1d4ed8;
    5 --color-text: #1f2937;
    6 --color-text-muted: #6b7280;
    7 --color-bg: #ffffff;
    8 --color-bg-alt: #f9fafb;
    9 --color-border: #e5e7eb;
    10 --color-danger: #dc2626;
    11
    12 /* typography scale */
    13 --font-size-sm: 0.875rem; /* 14px */
    14 --font-size-base: 1rem; /* 16px */
    15 --font-size-lg: 1.25rem; /* 20px */
    16 --font-size-xl: 1.875rem; /* 30px */
    17
    18 /* radii */
    19 --radius-sm: 4px;
    20 --radius-md: 8px;
    21 --radius-lg: 16px;
    22
    23 /* font stacks */
    24 --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
    25 --font-mono: 'JetBrains Mono', ui-monospace, Menlo, monospace;
    26}
    27
    28body {
    29 font-family: var(--font-sans);
    30 font-size: var(--font-size-base);
    31 color: var(--color-text);
    32 background: var(--color-bg);
    33 line-height: 1.5;
    34}

    Renders: body picks up Inter (with system fallback), 16px base, slate text on white. No visible change yet — further selectors reference the tokens.


    Slide 33 — The 200×200 card that's actually 250

    With default content-box, padding and border add outside the declared width.

    1<!DOCTYPE html>
    2<html lang="en">
    3 <head>
    4 <meta charset="UTF-8" />
    5 <title>Box Model</title>
    6 <style>
    7 .card {
    8 width: 200px;
    9 height: 200px;
    10 padding: 20px;
    11 border: 5px solid steelblue;
    12 margin: 16px;
    13 background: lightyellow;
    14 }
    15 </style>
    16 </head>
    17 <body>
    18 <div class="card">I declared 200×200. Measure me.</div>
    19 </body>
    20</html>

    Renders: pale-yellow card with blue border. DevTools box-model widget shows content 200×200, padding 20, border 5, margin 16. Outer edge: 200 + 20 + 20 + 5 + 5 = 250px.


    Slide 34 — box-sizing: border-box

    The single most important CSS line in the lecture — every stylesheet from now on starts with this.

    1* {
    2 box-sizing: border-box;
    3}

    Renders: re-measure the same card → outer edge is now 200px. Padding and border eat into the declared width. Margin still adds outside.


    Slide 35 — border-radius + box-shadow — instant polish

    Two properties · the cheapest "this looks designed" wins in CSS.

    1.card {
    2 width: 240px;
    3 height: 240px;
    4 padding: 24px;
    5 border: 1px solid #e5e7eb; /* lighter border */
    6 margin: 24px;
    7 background: white;
    8 border-radius: 12px;
    9 box-shadow: 0 4px 12px rgb(0 0 0 / 0.08);
    10}

    Renders: the heavy yellow square becomes a clean white card with rounded corners and a subtle drop shadow. border-radius: 50% would make it a circle (avatar trick).


    Slide 37 — 🧩 Mini challenge — reference solution

    Profile card from scratch using :root tokens, box-sizing: border-box, border-radius, and box-shadow.

    1<!DOCTYPE html>
    2<html lang="en">
    3 <head>
    4 <meta charset="UTF-8" />
    5 <title>Card Challenge</title>
    6 <style>
    7 :root {
    8 --primary: #2563eb;
    9 --text: #1f2937;
    10 --bg: #f9fafb;
    11 --radius: 12px;
    12 }
    13
    14 * { box-sizing: border-box; }
    15
    16 body {
    17 font-family: system-ui, sans-serif;
    18 background: var(--bg);
    19 padding: 32px;
    20 color: var(--text);
    21 }
    22
    23 .card {
    24 max-width: 280px;
    25 padding: 24px;
    26 border: 1px solid #e5e7eb;
    27 border-radius: var(--radius);
    28 box-shadow: 0 4px 12px rgb(0 0 0 / 0.08);
    29 background: white;
    30 text-align: center;
    31 }
    32
    33 .card img {
    34 width: 120px;
    35 height: 120px;
    36 border-radius: 50%;
    37 margin-bottom: 16px;
    38 }
    39
    40 .card p {
    41 font-size: 1.125rem;
    42 font-weight: 600;
    43 color: var(--primary);
    44 margin: 0;
    45 }
    46 </style>
    47 </head>
    48 <body>
    49 <div class="card">
    50 <img src="https://i.pravatar.cc/120" alt="Your avatar" />
    51 <p>Asad Iqbal</p>
    52 </div>
    53 </body>
    54</html>

    Renders: a clean white card on a soft gray background, with a circular avatar, the name in blue beneath it, rounded corners, and a subtle drop shadow.


    4. Common pitfalls

    • ❌ Treating CSS as "just decoration" → it's a rule-resolution engine; cascade + specificity + inheritance answer ~90% of "why doesn't my style work?"
    • ❌ Confusing inline style="" with internal <style> → inline is an attribute on one tag (specificity 1000); internal is a <head> block targeting many.
    • ❌ Reaching for !important to "fix" cascade conflicts → it becomes its own arms race; raise specificity with a class or restructure instead.
    • ❌ Assuming all CSS properties inherit → only typographic ones do (color, font, line-height); padding, margin, border, width do not.
    • ❌ Treating #nav and .nav as interchangeable → ID = weight 100, class = weight 10; use class for styling, reserve ID for fragment links.
    • ❌ Writing ::before without a content property → renders nothing; add content: ""; (even empty) so the pseudo-element appears.
    • ❌ Writing rgba(0,0,0,50) for "50% transparent" → invalid; alpha is a 0–1 fraction → use rgb(0 0 0 / 0.5) or rgba(0,0,0,0.5).
    • ❌ Expecting a width: 200px element to actually be 200px wide → with default content-box, padding+border add on top → set * { box-sizing: border-box; } at the top of every stylesheet.

    5. Glossary (for non-coders)

    TermPlain-English meaning
    CSSCascading Style Sheets — the language for telling the browser how HTML should look
    SelectorThe "address" part of a CSS rule — points at which HTML elements to style
    DeclarationA property: value; pair inside a rule (e.g. color: tomato;)
    RuleA selector plus its { ... } block of declarations
    CascadeThe browser's process for picking which rule wins when many target the same element
    SpecificityA score the cascade uses — higher score wins. Computed as (inline, IDs, classes, elements)
    InheritanceWhen a child element silently picks up its parent's value (only for typographic properties)
    Pseudo-classA selector for an element's state (:hover, :focus, :checked)
    Pseudo-elementA selector for a part of an element (::before, ::after, ::first-line) — needs content
    CombinatorA symbol that links selectors: descendant (space), child >, adjacent sibling +
    Box modelThe four concentric layers of every element: content → padding → border → margin
    box-sizingWhich layers count toward the declared width — content-box (default) vs border-box (sane)
    Custom Property (CSS Variable)A reusable value declared with --name: value; and consumed via var(--name)
    :rootThe topmost selector — equivalent to <html> but with one extra weight; where global tokens live
    HSLHue/Saturation/Lightness — a color notation that maps to human intuition
    Hex code#RRGGBB color notation — 0–255 in base-16 per channel
    rem / emFont-relative size units — rem = root's font-size; em = element's own
    Web fontA typeface loaded over the internet (Google Fonts, Adobe Fonts, self-hosted .woff2)
    Design tokenA named, centralized value (color, spacing, radius) referenced everywhere — single source of truth
    Specificity inflationWriting overly long selector chains "to be safe" — locks you in, slows the matcher
    Margin collapsingWhen two vertical margins meeting between siblings merge into the larger of the two

    6. Next steps

    Practice: assignment.md — build a styled card / landing page using :root tokens, box-sizing: border-box, the selector toolbox, and the box model. Core takes ~30–45 min, stretch another ~30–60. Submit before Lecture 5.

    Quiz: quiz.md — 10 questions. ~15 min, open-book. Due before Lecture 5.

    Next lecture: Lecture 5 — Layout Mastery: Flexbox & CSS Grid. With the box model fluent and selectors second-nature, you'll learn position, the Flexbox 1D model (navbars, card rows, sticky footers), the CSS Grid 2D model (full-page layouts), and the "1D → Flex, 2D → Grid" decision rule.

    Further reading (credible external resources)

    • MDN — Learn CSS: First Steps — beginner-friendly canonical walkthrough of how CSS attaches to HTML, rule structure, selectors, and the cascade.
    • MDN — Introduction to the CSS Box Model — definitive reference for margin/border/padding/content semantics and box-sizing behavior.
    • Josh W. Comeau — The Surprising Truth About Pixels and Accessibility — exceptional deep-dive on why rem-based sizing matters; primes the responsive lesson coming next module.
    • web.dev — Learn CSS — Google's structured CSS course covering selectors, cascade, box model, and color in modular lessons.

    Stuck? Post in the class forum or DM the TA.


    7. Interview prep — common questions

    Quick-reference questions commonly asked about Lecture 4 concepts. Try answering each one before reading the brief answer.

    • Walk me through the CSS cascade — when two rules target the same element with the same specificity, which one wins? Source order — the rule that appears later in the stylesheet (or in the later-loaded stylesheet) wins. Specificity is the first tie-breaker; source order is the second.

    • How is specificity calculated? Score #header .nav li a:hover vs nav ul li a.active. Score as (inline, IDs, classes, elements). #header .nav li a:hover = 1 ID + 1 class + 1 pseudo-class (counts as class) + 2 elements = (0, 1, 2, 2). nav ul li a.active = 1 class + 4 elements = (0, 0, 1, 4). The first wins on the IDs column.

    • Explain the box model. What does box-sizing: border-box change, and why is it the modern default? Every element is four concentric layers: content → padding → border → margin. By default (content-box), width sizes only the content, so padding and border add outside it (a width: 200px card with 20px padding + 5px border measures 250px). border-box makes width include padding and border, so 200px stays 200px — predictable math is why every modern reset starts with * { box-sizing: border-box; }.

    • What's the difference between a class selector and an ID selector — when would you reach for each in production code? Class (.btn) is reusable and weighs 10; ID (#nav) is one-of-a-kind per page and weighs 100. Use classes for all styling (reusable, easy to refactor); reserve IDs for fragment links (<a href="#section-2">) and <label for=""> associations.

    • Which CSS properties are inherited by default, and which are not? Give two examples of each. Typographic properties inherit: e.g., color and font-family (also line-height, visibility). Layout/box properties don't: e.g., padding and margin (also border, width). Rule of thumb: text-stuff inherits, box-stuff doesn't.

    • What's the practical difference between display: none, visibility: hidden, and opacity: 0? display: none removes the element from the layout entirely (no space reserved, not focusable). visibility: hidden hides the element but keeps its space in the layout. opacity: 0 makes it fully transparent but it still occupies space and remains interactive (clickable, focusable).

    • What are CSS Custom Properties and how do they differ from preprocessor variables (Sass $primary)? CSS custom properties (--primary: #2563eb; consumed via var(--primary)) are runtime values that cascade and inherit through the DOM, so a wrapper class can re-define them and descendants pick the closer value (the basis of theming). Sass $primary is compile-time — values are baked into the output CSS and can't be changed in the browser or scoped via the cascade.

    All Web Development Fundamentals-V2 Tutorials