CHAPTER 79
CSS Dark Mode
Learn how CSS can create dark themes and respond to the user's preferred color scheme.
01. What is CSS Dark Mode?
Dark mode uses darker backgrounds and lighter foreground content.
CSS variables make theme changes easier.
prefers-color-scheme can detect the user's operating system preference.
02. Syntax
@media (prefers-color-scheme: dark) {
body {
background: #020617;
color: white;
}
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
:root {
--bg: white;
--text: #111827;
}
body {
background: var(--bg);
color: var(--text);
}
@media(prefers-color-scheme: dark) {
:root {
--bg: #020617;
--text: #f8fafc;
}
}
</style>
</head>
<body>
<h1>AIFA Coding Lab</h1>
<p>
Theme follows system preference.
</p>
</body>
</html>
04. Important Points
✓
Use CSS variables for theme colors.
✓
prefers-color-scheme detects user preference.
✓
Text contrast must remain strong in dark mode.
✓
Images and borders may also need theme adjustments.
05. Common Mistakes
⚠
Using pure black everywhere without visual hierarchy.
⚠
Poor contrast between text and background.
⚠
Forgetting form controls and borders.
06. Real World Use
Dark mode is common in coding platforms, dashboards, mobile apps, developer tools and modern websites.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Create a Dark Theme
Create a page that automatically changes between light and dark mode using prefers-color-scheme.