Home >Web Front-end >JS Tutorial >How to Implement Dark Mode in Tailwind CSS in One Command
Step 1: Run the Setup Command
Open your terminal and run the following command to create a complete Tailwind project with dark mode functionality:
mkdir dark-mode-tailwind && cd dark-mode-tailwind && npm init -y && npm install -D tailwindcss postcss autoprefixer && npx tailwindcss init && echo '@tailwind base;\n@tailwind components;\n@tailwind utilities;' > styles.css && echo '<!DOCTYPE html>\n<html lang="en"> <p>Step 2: Open the Project<br> After the command completes, open the folder in your code editor. Your files are ready, and you can test the project in any live server, such as:<br> </p> <pre class="brush:php;toolbar:false">npx live-server
What the Command Does
Creates a project folder (dark-mode-tailwind) and navigates into it.
Initializes the project with npm and installs Tailwind CSS dependencies.
Configures Tailwind CSS and creates the required styles.css file.
Adds a basic index.html with a dark mode toggle button.
Writes a script.js file to handle dark mode logic using localStorage.
Builds the Tailwind CSS file for immediate use.
Test the Dark Mode
Open index.html in a browser.
Click the "Toggle Dark Mode" button to switch themes.
Your preference will persist even after refreshing, thanks to localStorage!
Enjoy coding! ?
The above is the detailed content of How to Implement Dark Mode in Tailwind CSS in One Command. For more information, please follow other related articles on the PHP Chinese website!