Dark & Light Theme
Easily set up dark and light mode in your TailwindCSS project with a ready-to-use theme config template and a toggle function. Just copy, paste, and customize to define your color scheme, and switch themes effortlessly with the provided function—no extra setup needed!
Install dependencies
1npm i tailwindcss
Dual Theme TailwindCSS Config Template
You can use this Tailwind CSS dual-theme template for your project, customize it with your own color schemes, and even create utility classes that adapt to both light and dark modes.
1/** @type {import('tailwindcss').Config} */2export default {3darkMode: ['class'],4content: [5'./pages/**/*.{js,ts,jsx,tsx,mdx}',6'./components/**/*.{js,ts,jsx,tsx,mdx}',7'./app/**/*.{js,ts,jsx,tsx,mdx}'8],9theme: {10extend: {11colors: {12primary: {13light: '#60d666', // ? Green for Light Mode14dark: '#66BB6A' // ? Lighter Green for Dark Mode15},16secondary: {17light: '#448AFF', // ? Bright Blue for Light Mode18dark: '#2979FF' // ? Deep Blue for Dark Mode19},20background: {21light: '#f2f2f2', // ? White for Light Mode22dark: '#121212' // ? Dark Gray for Dark Mode23},24text: {25light: '#000000', // ? Dark Gray for Light Mode26dark: '#fdfdfd' // ? Light Gray for Dark Mode27},28header: {29light: '#e8e8e8', // ? Light Gray for Header30dark: '#1F1F1F' // ? Deep Gray for Header31},32},33}34},35class: 'dark',36plugins: [37function ({ addComponents }) {38addComponents({39// ? You can define more classes here that would work for both light and dark modes40'.bg-primary': {41'@apply bg-primary-light text-text-light transition-all': {},42'.dark &': {43'@apply bg-primary-dark text-text-dark transition-all': {}44}45},46'.bg-secondary': {47'@apply bg-secondary-light text-white transition-all': {},48'.dark &': {49'@apply bg-secondary-dark transition-all': {}50}51},52'.text-primary': {53'@apply text-primary-dark transition-all': {},54'.dark &': {55'@apply text-primary-light transition-all': {}56}57},58});59}60]61};
How to toggle between light and dark mode?
You can use the following code to toggle between light and dark mode.
1const toggleDarkMode = () => {2document.documentElement.classList.toggle('dark');3}
💡 Pro tip: Store the value of current theme in localStorage for better user experience.
⭐️ Got a question or feedback?
Feel free to reach out!