Lovable ui

Dark Mode Not Toggling or Persisting

Dark mode toggle doesn't work or resets on page reload. Classes like 'dark:bg-gray-900' don't apply when dark mode is enabled. Theme preference not saved.

Dark mode requires class management on root element and localStorage persistence. Tailwind dark mode has multiple strategy options.

Common Causes

  1. tailwind.config.js darkMode not set to 'class' or 'media'
  2. Dark class not added to html element
  3. Dark mode preference not saved to localStorage
  4. Page doesn't check saved preference on load
  5. CSS specificity issue with dark mode selectors

How to Fix It

Set darkMode strategy in tailwind.config.js:

export default {
  darkMode: 'class'
}

Toggle and persist:

function toggleDarkMode() {
  document.documentElement.classList.toggle('dark');
  localStorage.setItem('theme', 
    document.documentElement.classList.contains('dark') ? 'dark' : 'light'
  );
}

// On load
if (localStorage.getItem('theme') === 'dark') {
  document.documentElement.classList.add('dark');
}

Real developers can help you.

Jen Jacobsen Jen Jacobsen I’m a Full-Stack Developer with over 10 years of experience building modern web and mobile applications. I enjoy working across the full product lifecycle — turning ideas into real, well-built products that are intuitive for users and scalable for businesses. I particularly enjoy building mobile apps, modern web platforms, and solving complex technical problems in a way that keeps systems clean, reliable, and easy to maintain. Matthew Butler Matthew Butler Systems Development Engineer @ Amazon Web Services PawelPloszaj PawelPloszaj I'm fronted developer with 10+ years of experience with big projects. I have small backend background too Bastien Labelle Bastien Labelle Full stack dev w/ 20+ years of experience Victor Denisov Victor Denisov Developer Caio Rodrigues Caio Rodrigues I'm a full-stack developer focused on building practical and scalable web applications. My main experience is with **React, TypeScript, and modern frontend architectures**, where I prioritize clean code, component reusability, and maintainable project structures. I have strong experience working with **dynamic forms, state management (Redux / React Hook Form), and complex data-driven interfaces**. I enjoy solving real-world problems by turning ideas into reliable software that companies can actually use in their daily operations. Beyond coding, I care about **software quality and architecture**, following best practices for componentization, code organization, and performance optimization. I'm also comfortable working across the stack when needed, integrating APIs, handling business logic, and helping transform prototypes into production-ready systems. My goal is always to deliver solutions that are **simple, efficient, and genuinely useful for the people using them.** Anthony Akpan Anthony Akpan Developer with 8 years of experience building softwares fro startups Jaime Orts-Caroff Jaime Orts-Caroff I'm a Senior Android developer, open to work in various fields Antriksh Narang Antriksh Narang 5 years+ Experienced Dev (Specially in Web Development), can help in python, javascript, react, next.js and full stack web dev technologies. Richard McSorley Richard McSorley Full-Stack Software Engineer with 8+ years building high-performance applications for enterprise clients. Shipped production systems at Walmart (4,000+ stores), Cigna (20M+ users), and Arkansas Blue Cross. 5 patents in retail/supply chain tech. Currently focused on AI integrations, automation tools, and TypeScript-first architectures.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

What are darkMode strategies?

'class' uses dark class on html element, 'media' uses prefers-color-scheme media query. 'class' allows manual toggle.

How do I prevent theme flash?

Run theme check script before HTML renders. Put script in head or use preload to load preference immediately.

Related Lovable Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help