Create a Dark Mode Toggle in Vue.js (With Tailwind CSS)

Dark mode has become a standard feature in modern web applications. It improves readability, reduces eye strain, and gives your UI a polished, contemporary feel. With Vue.js and Tailwind CSS, implementing a dark mode toggle is both simple and elegant. Letโ€™s walk through how to set it up.

๐—ช๐—ต๐˜† ๐—จ๐˜€๐—ฒ ๐—ง๐—ฎ๐—ถ๐—น๐˜„๐—ถ๐—ป๐—ฑ ๐—–๐—ฆ๐—ฆ ๐—ณ๐—ผ๐—ฟ ๐——๐—ฎ๐—ฟ๐—ธ ๐— ๐—ผ๐—ฑ๐—ฒ?

Tailwind CSS includes built-in dark mode utilities that let you style components effortlessly. By default, Tailwind uses the dark class strategyโ€”meaning when the dark class is added to the html or body tag, all dark-mode-ready styles instantly activate.

๐—ฆ๐—ฒ๐˜๐˜๐—ถ๐—ป๐—ด ๐—จ๐—ฝ ๐˜๐—ต๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜

First, create a Vue.js project:

npm create vue@latest

Install Tailwind CSS:

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Enable dark mode by adding this to tailwind.config.js:

darkMode: \'class\',

Now Tailwind is ready to respond to dark mode toggles.

๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ง๐—ผ๐—ด๐—ด๐—น๐—ฒ ๐—Ÿ๐—ผ๐—ด๐—ถ๐—ฐ

In your main App component or Navbar component, add a reactive variable such as isDark. When toggled, you can add or remove the dark class from the html element:

const isDark = ref(false);

watch(isDark, (value) => {
  document.documentElement.classList.toggle(\'dark\', value);
});

This instantly switches all styles across your app.

๐——๐—ฒ๐˜€๐—ถ๐—ด๐—ป๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ง๐—ผ๐—ด๐—ด๐—น๐—ฒ ๐—•๐˜‚๐˜๐˜๐—ผ๐—ป

Use Tailwind classes to style your toggle:

<button @click=\"isDark = !isDark\" class=\"p-2 rounded-lg bg-gray-200 dark:bg-gray-700\">
  {{ isDark ? \'Light Mode\' : \'Dark Mode\' }}
</button>

Youโ€™ll notice how Tailwindโ€™s dark: variants automatically apply the appropriate styling.

๐—˜๐—ป๐—ต๐—ฎ๐—ป๐—ฐ๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ

For better UX:

  • Save user preference in localStorage
  • Add transition utilities like transition-colors
  • Use icons (sun/moon) for visual clarity

With just a few lines of code, you now have a sleek, responsive dark mode toggle powered by Vue.js and Tailwind CSSโ€”giving your app a modern, user-friendly touch.

If you are looking for any services related to Website Development, App Development, Digital Marketing and SEO, just email us at nchouksey@manifestinfotech.com

#Vuejs #Vue3 #TailwindCSS #DarkMode #DarkModeToggle #FrontendDevelopment #WebDevelopment #JavaScript #VueTips #UIUXDesign #ModernFrontend #VueDevelopers #CodingProjects #VueTutorial #TailwindUI #JSDeveloper #WebAppDesign #ResponsiveDesign #DeveloperTips #CleanUI