Courses/HTML & CSS/Cascade, Layers & Specificity

    CSS Cascade, Layers & Specificity Deep Dive

    Lesson 24 โ€ข Advanced Track

    What You'll Learn

    How the browser resolves conflicting CSS rules
    Specificity scoring: IDs, classes, elements
    CSS Cascade Layers with @layer for style organization
    Inheritance: which properties pass to children
    The !important escape hatch and why to avoid it
    Practical strategies for managing large stylesheets

    ๐Ÿ’ก Think of It Like This

    The cascade is like a courtroom. Multiple "witnesses" (style rules) testify about what an element should look like. The judge (browser) decides based on a strict hierarchy: importance โ†’ layer โ†’ specificity โ†’ source order. The most authoritative witness wins.

    PriorityFactorExample
    1 (highest)!importantcolor: red !important
    2Inline stylesstyle="color: red"
    3Cascade layer order@layer base, components
    4Specificity#id > .class > element
    5 (lowest)Source orderLast rule wins (if same specificity)

    Specificity Scoring

    Every CSS selector has a specificity score. Higher scores win when rules conflict.

    Specificity Calculator

    See which selector wins when rules conflict

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>CSS Specificity</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
    
        h1 { color: #3b82f6; margin-bottom: 20px; }
    
        .demo-box {
          background: #1e293b;
          padding: 20px;
          border-radius: 8px;
          margin: 15px 0;
        }
    
        /* Specificity battles! */
    
        /* (0, 0, 1) โ€” element select
    ...

    CSS Cascade Layers (@layer)

    @layer lets you organize your CSS into priority levels. Later layers override earlier ones, regardless of specificity within the layer.

    CSS @layer in Action

    Organize styles with cascade layers

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>CSS @layer</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
    
        h1 { color: #3b82f6; margin-bottom: 20px; }
    
        /* Define layer order โ€” last layer has highest priority */
        @layer reset, base, components, utilities;
    
        /* Reset layer (lowest priority) */
        @layer reset {
          * { margin: 0
    ...

    Inheritance

    Some CSS properties are inherited by child elements, others are not. Understanding this saves you from writing redundant CSS.

    CSS Inheritance

    See which properties inherit and which don't

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>CSS Inheritance</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
    
        h1 { color: #3b82f6; margin-bottom: 20px; }
    
        .parent {
          color: #22c55e;
          font-size: 18px;
          font-family: Georgia, serif;
          border: 3px solid #f97316;
          padding: 20px;
          background: #1e293b;
          bor
    ...

    โš ๏ธ Common Mistakes

    • Over-using !important โ€” It breaks the natural cascade and makes debugging nightmares. Only use it for utility classes in a layer-based architecture.
    • ID selectors for styling โ€” IDs have very high specificity (1,0,0). Prefer classes (0,1,0) which are easier to override.
    • Not understanding source order โ€” When two selectors have equal specificity, the last one in the stylesheet wins.
    • Confusing @layer order with specificity โ€” Layer order takes priority over specificity. A low-specificity rule in a later layer beats a high-specificity rule in an earlier layer.

    ๐ŸŽ‰ Lesson Complete

    You now understand exactly how the browser resolves CSS conflicts:

    • โœ… Cascade order: !important โ†’ inline โ†’ layers โ†’ specificity โ†’ source order
    • โœ… Specificity: IDs (1,0,0) > classes (0,1,0) > elements (0,0,1)
    • โœ… @layer organises styles with explicit priority
    • โœ… Text properties inherit; box properties don't

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service