Performance: Media-based dark mode strategy
This affects initial page load speed and rendering by conditionally applying styles based on user system preferences.
@media (prefers-color-scheme: dark) { .dark\:bg-gray-900 { background-color: #111; } .dark\:text-gray-200 { color: #eee; } } /* Tailwind applies these automatically */@media (prefers-color-scheme: dark) { body { background-color: #111; color: #eee; } } /* plus JavaScript toggling classes on load */| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| JavaScript toggling dark mode after load | Multiple class changes | Multiple reflows triggered | Multiple paints causing CLS | [X] Bad |
| CSS media query for dark mode | No DOM changes | Single reflow during initial layout | Single paint with no CLS | [OK] Good |
darkMode: 'media' in Tailwind CSS configuration do?darkMode: 'media'.darkMode: 'media'dark:, placed before the class.dark:bg-* overrides in dark mode.dark:bg-gray-800 bg-gray-200 -> Option Cdark: prefix before class [OK]dark: prefix before classes for dark mode [OK]{ darkMode: 'media' } and this HTML: <div class="bg-white dark:bg-black text-black dark:text-white">Hello</div>What background and text colors will appear if the user's system prefers dark mode?
darkMode: 'media', dark styles apply if system prefers dark mode.dark:bg-black sets background black, dark:text-white sets text white in dark mode.{ darkMode: 'media' } and your HTML: <div class="bg-white darkmode:bg-black">Test</div>Why does the dark mode background not apply when system prefers dark mode?
dark:, not darkmode:.darkmode:bg-black is invalid and ignored by Tailwind.dark:, not darkmode: -> Option Adark: prefix for dark mode [OK]dark: prefix, no variations [OK]md:dark: means dark mode styles apply only on medium screens and up.bg-white md:dark:bg-gray-800 uses md:dark:bg-gray-800 which applies dark background only on screens ≥768px and in dark mode.bg-white md:dark:bg-gray-800 -> Option Dmd:dark: to combine media query and dark mode [OK]Pass the test and this pattern moves unknown