Dark Mode — Complete Guide
Dark Mode — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of CSS Tutorial on Toolliyo Academy.
On this page
CSS Tutorial · Lesson 79 of 100
Dark Mode
Foundations & Layout ✓ → Responsive & Motion ✓ → Architecture & Perf → Projects
Architecture & Perf · 3 — Scale · ~15 min read · CSS — Framework Integration
1. Introduction
Level up: Dark Mode. Think architecture, performance, and maintainable design systems.
Dark mode uses darker surfaces and lighter text via prefers-color-scheme media query or manual class toggle with inverted semantic color tokens.
2. Real-world story
User enables dark mode in iOS settings — web app matches automatically.
Outcome: Token swap in one media block themes entire dashboard.
3. Why it matters
StyleVerse banking users enable dark mode at night; traders prefer dark charts — CSS must not blind with white flash.
4. Visual understanding
Read this diagram — the mental model for Dark Mode.
Component ├── structure (HTML/JSX) └── styles (CSS Modules / Tailwind / tokens) Theme: light/dark via data-theme or class on <html>
5. Key concepts (easy words)
| Idea | Meaning |
|---|---|
| What | Dark mode uses darker surfaces and lighter text via prefers-color-scheme media query or manual class toggle with inverted semantic color tokens. |
| Remember | prefers-color-scheme or class toggle. Invert semantic surface and text tokens. color-scheme property helps native UI. |
| Practice tip | Change one property, refresh, watch DevTools Computed. |
6. How it works
- OS dark preference automatically swaps token values. body consumes variables so all components inherit dark surfaces without duplicate rules.
- prefers-color-scheme or class toggle.
- Invert semantic surface and text tokens.
- Check the result in DevTools → Computed and the box model overlay.
7. Choose wisely
| Option | Notes |
|---|---|
| Do | Practice Dark Mode on a small StyleVerse card |
| Avoid | Copying huge frameworks before learning core CSS |
8. Try this example
Paste into a <style> block (or use Run Example). Refresh to see StyleVerse UI changes.
@media (prefers-color-scheme: dark) {
:root {
--sv-bg-page: #0f172a;
--sv-bg-surface: #1e293b;
--sv-text-primary: #f8fafc;
--sv-border: #334155;
}
}
body {
background: var(--sv-bg-page);
color: var(--sv-text-primary);
}
Line walkthrough
| Code | What it means |
|---|---|
@media (prefers-color-scheme: dark) { | Selector — picks which elements get these declarations. |
:root { | Selector — picks which elements get these declarations. |
--sv-bg-page: #0f172a; | Declaration — property and value that change appearance. |
--sv-bg-surface: #1e293b; | Declaration — property and value that change appearance. |
--sv-text-primary: #f8fafc; | Declaration — property and value that change appearance. |
--sv-border: #334155; | Declaration — property and value that change appearance. |
} | Ends the rule block. |
} | Ends the rule block. |
body { | Selector — picks which elements get these declarations. |
background: var(--sv-bg-page); | Declaration — property and value that change appearance. |
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
9. Another real-world angle
10. Best practices checklist
- Prefer classes over ids for reusable StyleVerse components.
- Use design tokens (CSS variables) for color and spacing.
- Mobile-first media queries; test keyboard focus.
- Keep specificity low — avoid !important except rare overrides.
- Animate transform/opacity when you can; respect prefers-reduced-motion.
11. Common mistakes
- Pure #000 background — too harsh; use dark slate tokens.
- Forgetting border colors — cards invisible on dark bg.
12. Practice in the browser
- Define paired light/dark token sets.
- Support manual override with .dark class on html.
- Set color-scheme: dark for scrollbars and inputs.
- Test images and charts for dark background contrast.
Experiments
- Add transition on background-color for smooth theme toggle.
- Dim images slightly in dark mode with opacity filter.
13. FAQ
Where should I put CSS for Dark Mode?
Start in a <style> block or styles.css linked from HTML. Later use CSS Modules, Tailwind, or tokens in a design system.
Why is my rule not applying?
Check selector match, typos, specificity, and whether a more specific rule or inline style wins. DevTools Computed tells the truth.
Flexbox or Grid for this?
Flex for one direction (toolbars, card rows). Grid for two-dimensional page frames. Many UIs use both.
14. Interview questions
Explain Dark Mode simply.
Dark mode uses darker surfaces and lighter text via prefers-color-scheme media query or manual class toggle with inverted semantic color tokens.
How do you debug CSS?
Inspect element, toggle declarations, read Computed styles, outline the box model, and reduce specificity conflicts.
How does this show up in production UI?
Token swap in one media block themes entire dashboard.
15. Remember
- prefers-color-scheme or class toggle.
- Invert semantic surface and text tokens.
- color-scheme property helps native UI.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!