A JavaScript function/module that toggles dark mode using Tailwind CSS classes.
This repository contains a simple JavaScript function/module that allows you to toggle dark mode on your web pages using Tailwind CSS classes. Dark mode is a popular feature that enhances user experience, especially in low-light environments.
-
Include
dark
class in your Tailwind CSS config (tailwind.config.js
) to style dark mode elements. -
Add the following HTML snippet to your webpage where you want the dark mode toggle button:
<button onclick="toggleDarkMode()" class="bg-gray-800 text-white px-4 py-2 rounded-md">Toggle Dark Mode</button>
- Include the JavaScript code provided in your project to enable dark mode toggling.
- The
toggleDarkMode()
function toggles thedark
class on thebody
element, allowing you to style dark mode elements in your CSS. - User preference for dark mode is saved in
localStorage
to persist the dark mode state across page reloads.
darkModeToggle.js
: Contains the JavaScript code for the dark mode toggle functionality.tailwind.config.js
: Includes the Tailwind CSS configuration file with the dark mode class added.
Contributions are welcome! If you have any improvements or bug fixes, feel free to open a pull request.