Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
When studying for school board examinations in your bedroom:
+-------------------------------------------------------------------------+ | HOW MODERN THEME SWITCHING WORKS | | | | Light Mode (:root): | | --bg-page: #f8fafc (Clean Soft White) | | --surface-card: #ffffff (Pure White) | | --text-primary: #0f172a (Deep Midnight Slate) | | | | Dark Mode ([data-theme="dark"]): | | --bg-page: #0f172a (Deep Midnight Slate) | | --surface-card: #1e293b (Rich Dark Navy) | | --text-primary: #f8fafc (Crisp Clean White) | | | | Notice: Your components (.card, .btn, h1, p) DO NOT CHANGE! | | They just consume the variables. When the variables flip, | | THE ENTIRE WEBSITE FLIPS IN 1 MILLISECOND! | +-------------------------------------------------------------------------+
Years ago, creating a dark mode required maintaining two separate stylesheets (light.css and dark.css), doubling your maintenance burden. Today, with CSS Custom Properties, you can build a complete, accessible, and persistent dark mode in under 50 lines of code!
The secret to effortless dark mode is semantic color abstraction. Never use variables named --white or --black. Instead, define variables by their function:
prefers-color-schemeBefore the user even clicks a toggle button, your website should automatically respect their device's operating system setting (Windows Dark Mode, iOS Dark Mode, Android Night Mode):
The :not([data-theme="light"]) guard ensures that if the student explicitly clicks "Light Mode" on their school portal, their manual choice overrides their operating system!
Your UI elements simply consume the variables. They have no idea whether it is day or night:
To allow users to switch themes and remember their preference across page refreshes, use this simple 10-line script:
| Bad Practice (Don't) | Good Practice (Do) | Why? |
|---|---|---|
Using pure black #000000 for dark mode surfaces | Use rich dark slates (#0f172a or #121212) | Pure black creates extreme visual contrast that causes eye strain and optical vibration. |
Forgetting to transition background-color and color | Add transition: background-color 0.25s, color 0.25s; to body | Gives the theme swap a gentle, polished cross-fade rather than a blinding strobe flash. |
Inverting images or avatar photos with filter: invert(1) | Never invert photos or human faces! | Inverting turns student and faculty photos into terrifying photographic negatives. |
Hardcoding #ffffff anywhere in component stylesheets | Always reference var(--surface-card) or var(--bg-page) | A single hardcoded #fff creates an awkward white box in the middle of dark mode. |
--bg-page, --text-primary, --surface-card) rather than literal colors.[data-theme="dark"]: The industry standard attribute selector placed on <html> to redefine custom properties.prefers-color-scheme: dark: Media query that automatically matches the user's OS operating system appearance.#0f172a) instead of pitch black (#000000) for premium dark mode aesthetics.0.25s transition to the background and text colors on the root document for seamless switching.#000000) backgrounds for dark mode?data-theme="dark" attribute in modern web applications?.card component when its parent document switches data-theme if the card uses var(--surface-card)?filter: invert(1) on parent containers to achieve dark mode?Build a complete School Student Portal with a live, functional Light/Dark Theme Switcher:
:root (Light) and [data-theme="dark"] (Dark).data-theme between light and dark in real-time with smooth CSS cross-fading!Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.