Setting Up Light and Dark Mode Switcher in Nuxt.js with Tailwind CSS

In this guide, we'll walk through setting up a light and dark mode switcher in a Nuxt.js project using Tailwind CSS. This feature enhances user experience by allowing them to choose between light and dark modes according to their preferences. We'll integrate this functionality seamlessly into our Nuxt.js application with Tailwind CSS.

Key Takeaways:

  • Integrating light and dark mode switcher enhances user experience.
  • Nuxt.js provides a convenient framework for building Vue.js applications.
  • Tailwind CSS offers utility-first CSS framework for designing UI components.

Steps to Set Up Light and Dark Mode Switcher:

  1. Install Nuxt.js:
    npx create-nuxt-app my-app
    cd my-app
    
  2. Install Tailwind CSS:
    npm install tailwindcss
    
  3. Configure Tailwind CSS:
    • Create a tailwind.config.js file in the root of your project:
      npx tailwindcss init
      
  4. Set Up Light and Dark Mode CSS:
    • Add light and dark mode CSS in your tailwind.config.js:
      module.exports = {
        darkMode: 'class',
        theme: {
          extend: {},
        },
        variants: {
          extend: {},
        },
        plugins: [],
      }
      
  5. Implement Light and Dark Mode Switcher Component:
    • Create a Vue component for the switcher:
      <template>
        <button @click="toggleMode">Toggle Mode</button>
      </template>
      
      <script>
      export default {
        methods: {
          toggleMode() {
            document.documentElement.classList.toggle('dark')
          }
        }
      }
      </script>
      
  6. Integrate Switcher Component in Layout:
    • Include the switcher component in your layout or any desired component:
      <template>
        <div>
          <ModeSwitcher />
          <!-- Your other content goes here -->
        </div>
      </template>
      
      <script>
      import ModeSwitcher from '@/components/ModeSwitcher.vue'
      
      export default {
        components: {
          ModeSwitcher
        }
      }
      </script>
      
  7. Toggle Mode in Nuxt.js Layout:
    • Update your Nuxt.js layout to toggle dark mode class based on user preference:
      <template>
        <div :class="{ 'dark': darkMode }">
          <ModeSwitcher />
          <Nuxt />
        </div>
      </template>
      
      <script>
      export default {
        data() {
          return {
            darkMode: false
          }
        },
        created() {
          this.darkMode = localStorage.getItem('darkMode') === 'true'
        },
        watch: {
          darkMode(newVal) {
            localStorage.setItem('darkMode', newVal)
          }
        }
      }
      </script>
      
  8. Customize Styles for Dark Mode:
    • Adjust your CSS styles in tailwind.config.js to accommodate dark mode:
      module.exports = {
        darkMode: 'class',
        theme: {
          extend: {
            colors: {
              'dark-bg': '#121212',
              'dark-text': '#ffffff',
            },
          },
        },
        variants: {
          extend: {},
        },
        plugins: [],
      }
      
  9. Test Your Application:
    • Run your Nuxt.js application and test the light and dark mode switcher functionality.

By following these steps, you can easily integrate a light and dark mode switcher into your Nuxt.js application using Tailwind CSS, providing users with a more personalized browsing experience.

Let’s work together.

If you’re looking for someone to build or improve a website, feel free to reach out. I’m currently open to freelance projects and remote roles.

© 2026 carlopaa.me. All rights reserved.