Batman55's avatar
Level 32

Force tailwind to display light mode components

My Firefox browser is set to "Automatic". If I set it to light or dark, tailwind components will respect those choices. But on "Automatic", it usually defaults to dark mode components, not always. I have been unsuccessful trying to get tailwind to force light mode in these situations.

The worst offenders are WireUI components.

0 likes
5 replies
LaryAI's avatar
Level 58

To force Tailwind CSS to display light mode components regardless of the user's system preference, you can configure your Tailwind CSS setup to default to light mode. This involves setting the darkMode option in your tailwind.config.js file to 'class' and then ensuring that your HTML or JavaScript sets the appropriate class on the html or body element.

Here's how you can do it:

  1. Configure Tailwind CSS:

    In your tailwind.config.js file, set the darkMode option to 'class'. This tells Tailwind to use a class-based approach for dark mode instead of relying on the user's system preference.

    // tailwind.config.js
    module.exports = {
      darkMode: 'class', // or 'media' if you want to use media queries
      // other configurations...
    }
    
  2. Set the Light Mode Class:

    Ensure that your application defaults to light mode by setting the appropriate class on the html or body element. You can do this using JavaScript:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Force Light Mode</title>
      <link href="/path/to/tailwind.css" rel="stylesheet">
    </head>
    <body>
      <script>
        // Remove 'dark' class to ensure light mode
        document.documentElement.classList.remove('dark');
      </script>
      <!-- Your content here -->
    </body>
    </html>
    
  3. Ensure Consistency Across Components:

    If you're using components like WireUI, make sure they are not adding the dark class themselves. You might need to check their documentation or source code to ensure they respect the class-based dark mode setting.

By following these steps, you should be able to force Tailwind to display light mode components consistently, regardless of the user's system settings.

Sinnbeck's avatar

Try changing it in your os. What os are you running?

1 like
Batman55's avatar
Level 32

@Sinnbeck I am running Mac, but I want it os independent. The whole site is to be consistently light themed, not something dark here or there based on the os preferences.

Please or to participate in this conversation.