The button color changes to a darker blue in dark mode for better contrast.
Tailwind
<button class="bg-blue-500 dark:bg-blue-700 text-white px-4 py-2 rounded">
Click me
</button>
Sample Program
This page uses Tailwind CSS to switch between light and dark modes when you click the button. The background and text colors change for better comfort.
Tailwind
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Dark Mode Example</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
// Enable dark mode class strategy
tailwind.config = {
darkMode: 'class'
}
// Toggle dark mode on button click
function toggleDarkMode() {
document.documentElement.classList.toggle('dark')
}
</script>
</head>
<body class="bg-white dark:bg-gray-900 text-black dark:text-white min-h-screen flex flex-col items-center justify-center">
<h1 class="text-3xl font-bold mb-4">Dark Mode Improves User Experience</h1>
<p class="mb-6 max-w-md text-center">
Dark mode reduces eye strain in low light and saves battery on some devices.
</p>
<button onclick="toggleDarkMode()" class="bg-blue-600 dark:bg-blue-400 text-white dark:text-black px-6 py-2 rounded focus:outline-none focus:ring-4 focus:ring-blue-300">
Toggle Dark Mode
</button>
</body>
</html>
OutputSuccess
Important Notes
Dark mode helps users focus by reducing bright white light.
Always provide a way for users to switch modes easily.
Test your colors for good contrast in both light and dark modes.
Summary
Dark mode makes screens easier on the eyes in dark environments.
It can save battery on devices with OLED or AMOLED screens.
Tailwind CSS makes it easy to add dark mode styles with the dark: prefix.
Practice
(1/5)
1. Why does dark mode improve user experience in low-light environments?
easy
A. It makes the screen brighter to help users see better.
B. It reduces eye strain by using darker colors that are easier to look at.
C. It disables all colors except black and white.
D. It increases the font size automatically.
Solution
Step 1: Understand eye strain in low light
Bright screens in dark rooms cause discomfort and strain on eyes.
Step 2: How dark mode helps
Dark mode uses darker backgrounds and lighter text, reducing brightness and easing eye strain.
Final Answer:
It reduces eye strain by using darker colors that are easier to look at. -> Option B
Quick Check:
Dark mode reduces eye strain = A [OK]
Hint: Dark mode = less bright = easier on eyes [OK]
Common Mistakes:
Thinking dark mode makes screen brighter
Believing dark mode changes font size
Assuming dark mode removes all colors
2. Which Tailwind CSS class prefix is used to apply styles only in dark mode?
easy
A. light:
B. mode-dark:
C. dark:
D. night:
Solution
Step 1: Recall Tailwind dark mode syntax
Tailwind uses the prefix dark: to apply styles when dark mode is active.
Step 2: Check other options
Other prefixes like light:, mode-dark:, or night: are not valid Tailwind prefixes.
Final Answer:
dark: -> Option C
Quick Check:
Tailwind dark mode prefix = dark: [OK]
Hint: Remember Tailwind uses dark: prefix for dark mode styles [OK]
Common Mistakes:
Using light: instead of dark:
Adding extra words like mode- or night-
Confusing with other CSS frameworks
3. What will be the background color of this div in dark mode?