Design

Dark Mode Best Practices

How to implement dark mode that users will love, with accessibility in mind.

Your Name 1 min read

Dark mode has become a must-have feature for modern websites. Here’s how to implement it properly.

Why Dark Mode?

Users love dark mode for several reasons:

  • Reduced eye strain in low-light conditions
  • Battery savings on OLED screens
  • Personal preference - some just prefer it
  • Accessibility for light-sensitive users

Implementation Strategies

1. CSS Custom Properties

Use CSS variables for easy theme switching:

:root {
  --bg-color: #ffffff;
  --text-color: #1a1a1a;
}

.dark {
  --bg-color: #0f172a;
  --text-color: #f1f5f9;
}

body {
  background: var(--bg-color);
  color: var(--text-color);
}

2. Tailwind Dark Mode

Tailwind makes it easy with the dark: prefix:

<div class="bg-white dark:bg-slate-900">
  <p class="text-gray-900 dark:text-gray-100">
    Content
  </p>
</div>

Respecting User Preferences

Always check the system preference first:

if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
  document.documentElement.classList.add('dark');
}

Persistence

Save the user’s choice:

// Save preference
localStorage.setItem('theme', 'dark');

// Load preference
const theme = localStorage.getItem('theme');

Accessibility Considerations

Important: Ensure sufficient contrast in both modes. WCAG recommends a minimum contrast ratio of 4.5:1 for normal text.

Contrast Ratios

Element Light Mode Dark Mode
Body text 12.6:1 13.1:1
Links 8.2:1 7.9:1
Muted text 4.6:1 4.8:1

Common Mistakes

Don’t Just Invert Colors

Simply inverting colors creates poor results:

  • Images look wrong
  • Brand colors get distorted
  • Contrast may become too harsh

Do Create a Proper Palette

Design intentional dark mode colors:

/* Light mode */
--primary: #4f46e5;

/* Dark mode - slightly brighter for visibility */
--primary: #818cf8;

Smooth Transitions

Add transitions for a polished feel:

body {
  transition: background-color 0.3s ease, color 0.3s ease;
}
Pro tip: Disable transitions on page load to prevent a flash of animation.

Testing

Test your dark mode thoroughly:

  1. Check all pages in both modes
  2. Verify image visibility
  3. Test form elements
  4. Validate contrast ratios
  5. Test the toggle mechanism

Conclusion

Dark mode is more than an aesthetic choice—it’s about user comfort and accessibility. Take the time to implement it properly, and your users will thank you.

#dark-mode #accessibility #ux