How to Add Dark Mode in ReactJS using Tailwind CSS ?
Last Updated :
23 May, 2024
Tailwind CSS is a CSS framework that helps in building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Tailwind CSS creates small utilities with a defined set of options enabling easy integration of existing classes directly into the HTML code.
Prerequisites:
Approach:
To Add Dark Mode in ReactJS using Tailwind CSS we will be using tailwind classes in the application. Tailwind provides a 'dark' variant that helps in styling our website differently when dark mode is enabled. It adds the functionality to switch between light and dark modes.
Creating React Application
Step 1: Create a React application using the following command:
npm create-react-app appname
Step 2: After creating your project folder i.e. folder name, move to it using the following command:
cd foldername
Step 3: After creating the React.js application, install the Tailwind CSS using the following command.
npm i -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Step 4: Configure template paths and add a class in a dark mode in tailwind.config.js file using the following command:
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
darkMode: "class",
}
Step 5: Install a Sun/Moon Icon animation module for transition with React.
npm i react-toggle-dark-mode
Project Structure
Project Structure will look like the following.

The Updated list of dependencies after installing required modules
{
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"react-toggle-dark-mode": "^1.1.1",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"autoprefixer": "^10.4.16",
"postcss": "^8.4.31",
"tailwindcss": "^3.3.5"
}
}
Example: This example demonstrate the dark mode which is enabled when the button is clicked. This data is stored in the localStorage to store the user preference of web app theme.
JavaScript
// Filename - App.js
import React from "react";
import Switcher from "./Components/Switcher";
function App() {
return (
<div>
<div style={{ textAlign: "center" }}>
<h1 className="text-green text-3xl font-bold">
GeeksforGeeks
</h1>
<h3 className="text-black dark:text-white text-2xl">
Adding Dark Mode in ReactJS using
Tailwind CSS
</h3>
</div>
<center>
<Switcher />
<div
className="w-56 overflow-hidden bg-white
rounded-lg border border-gray-200
shadow-md dark:bg-gray-800 dark:border-gray-700"
>
<img
className="rounded-t-lg"
src=
"https://media.geeksforgeeks.org/wp-content/uploads/20220221132017/download.png"
alt="gfg"
/>
<div className="p-5">
<a href="##">
<h5
className="mb-2 text-2xl
font-bold tracking-tight
text-gray-900 dark:text-white"
>
GeeksforGeeks
</h5>
</a>
<p
className="mb-3 font-normal text-gray-700
dark:text-gray-400"
>
Best coding website for
developers in the world.
</p>
</div>
</div>
</center>
</div>
);
}
export default App;
JavaScript
// Filename - Components/Switcher.js
import { useState } from "react";
import { DarkModeSwitch } from "react-toggle-dark-mode";
import useDarkSide from "../hooks/useDarkSide";
export default function Switcher() {
const [colorTheme, setTheme] = useDarkSide();
const [darkSide, setDarkSide] = useState(
colorTheme === "light" ? true : false
);
const toggleDarkMode = (checked) => {
setTheme(colorTheme);
setDarkSide(checked);
};
return (
<>
<DarkModeSwitch
style={{ marginBottom: "2rem" }}
checked={darkSide}
onChange={toggleDarkMode}
size={30}
/>
</>
);
}
JavaScript
// Filename - hooks/useDarkSide.js
import { useState, useEffect } from "react";
export default function useDarkSide() {
const [theme, setTheme] = useState(localStorage.theme);
const colorTheme = theme === "dark" ? "light" : "dark";
localStorage.setItem("theme", theme);
useEffect(() => {
const root = window.document.documentElement;
root.classList.remove(colorTheme);
root.classList.add(theme);
if (localStorage.theme == "dark")
localStorage.removeItem("theme");
else localStorage.setItem("theme", theme);
}, [theme, colorTheme]);
return [colorTheme, setTheme];
}
Step to run the application: Use this command in the terminal inside the project directory.
npm start
Output: This output will be visible on the http://localhost:3000/ on the browser window.
Similar Reads
How to add scale animation on hover using Tailwind CSS in React ?
In this article, we'll see how to add scale animation on hover using tailwind CSS in a ReactJS app. The hover effect appears when a user positions the cursor over an element. In tailwind CSS, the scale utility class helps in getting the zoom effect over an element. PrerequisitesReact JSTailwind CSSA
3 min read
How to style a href links in React using Tailwind CSS ?
In this article, we will learn how we can style a href link in Reactjs. Since we use Tailwind CSS, Tailwind CSS describes itself as the first applicable CSS framework. Rather than focusing on the performance of a style object, Tailwind focuses on how it should be displayed. This makes it easier for
2 min read
How to Enable Dark Mode in Tailwind CSS ?
Tailwind CSS offers built-in features for enabling dark mode, allowing developers to create a dark-themed version of their web pages. By configuring the dark mode setting in the Tailwind configuration file and using the dark class, specific elements can seamlessly switch to dark-themed styles.There
3 min read
How to use Ant Design with Tailwind CSS in a React Project ?
The integration of Ant Design and Tailwind CSS in a React project presents challenges due to their differing styling conventions. Ant Design offers a feature-rich component library with its own class names, while Tailwind CSS provides a utility-first framework for custom designs. Combining both libr
2 min read
How To Show Background Image In Tailwind CSS Using React Dynamic Url?
Tailwind CSS is a utility-first framework that uses preset classes to greatly speed up styling. However, utility classes are insufficient when handling dynamic content such as background graphics. We'll demonstrate how to use Tailwind CSS for layout and styling in a React component while managing dy
3 min read
Create Modal Dialogs UI using React and Tailwind CSS
Modal dialogs are an essential part of modern web applications. They offer a user-friendly way to present information or collect input without navigating away from the current page. Modals typically appear as overlays which help focus the user's attention on specific tasks like forms or alerts and c
6 min read
How to Change Image on Hover using Tailwind CSS?
One common effect is changing an image when the user hovers over it. We use Tailwind CSS, a utility-first CSS framework, to accomplish this without any additional JavaScript logic for the hover effect. By utilizing Tailwind's built-in classes we can create smooth transitions between two images where
2 min read
How to create Dark Mode in ReactJS using Material UI ?
Over the past few years, dark mode has gained widespread popularity as a feature in various applications and websites. It provides a visually pleasing and more comfortable viewing experience, particularly in low-light environments. If you're using ReactJS and Material UI, incorporating dark mode fun
3 min read
How to Create Tailwind CSS Profile Card in React?
TailwindCSS can be integrated with React to create highly customizable and responsive profile cards by using utility-first classes for styling. This approach allows you to design visually appealing cards with gradients, rounded corners, and interactive animations, all while ensuring seamless respons
3 min read
How to apply Dynamic Styles using Tailwind CSS?
Tailwind CSS helps you style your website easily by using simple classes. Sometimes, you might need to change these styles based on different conditions, like when a button is clicked. Below are the approaches to apply dynamic styles using Tailwind CSS:Table of ContentUsing Inline StylesUsing CSS Cu
3 min read