Unit 3: Visual Polish, Theme & AnimationLesson #8 / 8

Lesson 8: CSS Custom Properties & Dark Mode Architecture

Er. Manoj Kumar — AuthorEr. Manoj KumarLast Updated: 26 Aug, 2026

1. CSS Custom Properties (Variables)

CSS variables allow you to store design tokens (colors, font sizes, spacing) centrally in the :root pseudo-class and update entire themes with a single declaration.

  • Declaration: --primary: #04AA6D; inside :root.
  • Usage: color: var(--primary);.
  • Dark Mode Toggling: Override variables under [data-theme="dark"] or @media (prefers-color-scheme: dark).
  • HTML5 Web Code (index.html)
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>CSS Variables & Theme Switcher</title>
    <style>
    :root {
    --bg-color: #17181c;
    --card-bg: #1f2029;
    --text-color: #ffffff;
    --accent-color: #04AA6D;
    --border-color: #333333;
    }
    body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: Arial, sans-serif;
    padding: 30px;
    }
    .theme-card {
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    padding: 24px;
    border-radius: 12px;
    max-width: 400px;
    margin: 0 auto;
    text-align: center;
    }
    .accent-btn {
    background-color: var(--accent-color);
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    font-weight: bold;
    cursor: pointer;
    margin-top: 12px;
    }
    </style>
    </head>
    <body>
    <div class="theme-card">
    <h2>Tokenized Design System</h2>
    <p>Using CSS variables enables instant dark/light theme switching.</p>
    <button class="accent-btn">Dynamic Button</button>
    </div>
    </body>
    </html>

    Interactive Knowledge Check

    Test your understanding of Lesson #8 concepts

    How do you access a CSS custom property --primary-color in a declaration?