How To Implement Dark/Light Themes in Next.js (React js) with Tailwind CSS (2025)

How To Implement Dark/Light Themes in Next.js (React js) with Tailwind CSS (1)

How To Implement Dark/Light Themes in Next.js (React js) with Tailwind CSS (2)

Published in

Towards Dev

·

5 min read

·

Jun 29, 2024

--

Having an application or a website that enables users to choose themes is really nice and awesome in this modern day web. These days, people spend most of their time online and they are really hurting their eyes, providing such feature will improve the user experience.

In this article, we are going to discus how to build a Next js application that will enable users to choose between light and dark themes using Tailwind CSS and make it to be persistent.

Prerequisites you need to understand this article

  1. Basic knowledge of React or Next js.
  2. Basic understanding of javascript.
  3. You must have Node js installed on your computer (for your React or Next js application to run)
  4. And finally, a web browser like Google Chrome.

Let`s install Next js

We have to install the latest version of Next js by running this command in our terminal.

npx create-next-app@latest

You will see some questions, asking you to choose what you would like to install along side with Next js. Make sure that you install Tailwind CSS through this way or you can also go to the official website of Tailwind CSS and read the documentation to install it manually. You can use your arrow keys to choose what you want to install and then click enter to select it.

How To Implement Dark/Light Themes in Next.js (React js) with Tailwind CSS (4)

Let`s start coding

We need to modify the config file of our Tailwind CSS with a special property value so that we can easily change theme of our application. We have to add this darkMode: “class” in the config file.

import type { Config } from "tailwindcss";

const config: Config = {
content: [
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
"./app/**/*.{js,ts,jsx,tsx,mdx}",
],
darkMode: "class",
theme: {
extend: {
backgroundImage: {
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
"gradient-conic":
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
},
},
},
plugins: [],
};
export default config;

Here, we are checking if the app is done loading, then we will get the theme that the user might have selected previously from the local storage. If the user haven`t selected any theme previously, then we will set the theme to light.

const [theme, setTheme] = useState<any>(
typeof window !== "undefined" ? localStorage.getItem("theme") : "light"
);

Here we are using useffect to select the theme automatically once the page is done loading. We are checking the value in the useState if it is dark theme, once it`s dark theme, we add the dark class to our application which will give it a dark theme effect through the help of Tailwind CSS. We also store the latest value or theme to the local storage and also trigger the function whenever a user updates the theme.

useEffect(() => {
if (theme === "dark") {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
localStorage.setItem("theme", theme);
}, [theme]);

This function helps users to switch themes. Here we are using ternary operator to check the value of the theme to update it accordingly. If the value of the theme is dark, we will then update the value of the theme to light, vice versa.

const toggleTheme = () => {
setTheme(theme === "dark" ? "light" : "dark");
};

Here we are using the dark class in Tailwind CSS to give each element a different kind of style if the application is in dark theme, you can clearly see that you can hide some elements if you don`t want them to be visible in the dark theme.

import { useState, useEffect } from "react";
import DarkModeIcon from "@mui/icons-material/DarkMode";
import WbSunnyIcon from "@mui/icons-material/WbSunny";

export default function Home() {
const [theme, setTheme] = useState<any>(
typeof window !== "undefined" ? localStorage.getItem("theme") : "light"
);

useEffect(() => {
if (theme === "dark") {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
localStorage.setItem("theme", theme);
}, [theme]);

const toggleTheme = () => {
setTheme(theme === "dark" ? "light" : "dark");
};
return (
<main className="">
<nav className="flex items-center justify-between bg-red-600 h-[60px] dark:bg-black p-4 dark:text-white">
{/* logo */}

<svg
id="logo-88"
width="40"
height="41"
viewBox="0 0 40 41"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
className="ccustom"
fill-rule="evenodd"
clip-rule="evenodd"
d="M13.7146 0.516113C11.4582 0.516113 9.2943 1.41245 7.69881 3.00794L0 10.7067V14.2307C0 16.7204 1.06944 18.9603 2.77401 20.5161C1.06944 22.0719 0 24.3118 0 26.8015V30.3255L7.69881 38.0243C9.2943 39.6198 11.4582 40.5161 13.7146 40.5161C16.2043 40.5161 18.4442 39.4467 20 37.7421C21.5558 39.4467 23.7957 40.5161 26.2854 40.5161C28.5418 40.5161 30.7057 39.6198 32.3012 38.0243L40 30.3255V26.8015C40 24.3118 38.9306 22.0719 37.226 20.5161C38.9306 18.9603 40 16.7204 40 14.2307V10.7067L32.3012 3.00794C30.7057 1.41245 28.5418 0.516113 26.2854 0.516113C23.7957 0.516113 21.5558 1.58555 20 3.29012C18.4442 1.58555 16.2043 0.516113 13.7146 0.516113ZM25.7588 20.5161C25.6629 20.4286 25.5688 20.3387 25.4766 20.2465L20 14.7699L14.5234 20.2465C14.4312 20.3387 14.3371 20.4286 14.2412 20.5161C14.3371 20.6036 14.4312 20.6935 14.5234 20.7857L20 26.2623L25.4766 20.7857C25.5688 20.6935 25.6629 20.6036 25.7588 20.5161ZM22.2222 30.3255L22.2222 32.0085C22.2222 34.2525 24.0414 36.0717 26.2854 36.0717C27.363 36.0717 28.3965 35.6436 29.1585 34.8816L35.5556 28.4845V26.8015C35.5556 24.5575 33.7364 22.7383 31.4924 22.7383C30.4148 22.7383 29.3813 23.1664 28.6193 23.9284L22.2222 30.3255ZM17.7778 30.3255L11.3807 23.9284C10.6187 23.1664 9.58524 22.7383 8.50762 22.7383C6.26359 22.7383 4.44444 24.5575 4.44444 26.8015V28.4845L10.8415 34.8816C11.6035 35.6436 12.637 36.0717 13.7146 36.0717C15.9586 36.0717 17.7778 34.2525 17.7778 32.0085V30.3255ZM17.7778 9.02373V10.7067L11.3807 17.1038C10.6187 17.8658 9.58524 18.2939 8.50762 18.2939C6.26359 18.2939 4.44444 16.4747 4.44444 14.2307V12.5477L10.8415 6.15063C11.6035 5.38864 12.637 4.96056 13.7146 4.96056C15.9586 4.96056 17.7778 6.7797 17.7778 9.02373ZM28.6193 17.1038L22.2222 10.7067L22.2222 9.02373C22.2222 6.7797 24.0414 4.96056 26.2854 4.96056C27.363 4.96056 28.3965 5.38864 29.1585 6.15063L35.5556 12.5477V14.2307C35.5556 16.4747 33.7364 18.2939 31.4924 18.2939C30.4148 18.2939 29.3813 17.8658 28.6193 17.1038Z"
fill="#FF630B"
></path>
</svg>

{/* links */}
<ul className="flex items-center justify-between gap-x-3">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
{/* toggle */}
<div className="cursor-pointer" onClick={toggleTheme}>
<DarkModeIcon className=" dark:hidden" />
{/* sun */}
<WbSunnyIcon className="hidden dark:block" />
</div>
</nav>

{/* theme con */}
<div className="flex items-center justify-center w-[500px] mt-[20px] h-[500px] bg-red-600 dark:bg-black">
{/* text con */}
{/* <div> */}
<h6 className=" text-center dark:hidden">Light theme</h6>
<h6 className=" hidden text-center dark:block dark:text-white">
Dark theme
</h6>
{/* </div> */}
</div>
</main>
);
}

You can watch the demo of the application here too.

https://screenrec.com/share/o6vG1BcIiY

Conclusion

Applications with dark and light themes makes it easier for users to have good user experience, giving them the flexibility to choose any theme of their choice which will make them to stay longer in your application leading to increase in sales.

We can achieve this easily with Nextjs, Tailwind CSS and the browser`s local storage to make it persistent.

Now with your newly acquired knowledge, go and make the web a better place.

Happy coding 😊!

How To Implement Dark/Light Themes in Next.js (React js) with Tailwind CSS (2025)

FAQs

How To Implement Dark/Light Themes in Next.js (React js) with Tailwind CSS? ›

First we add the dark class to the Html element. This enables the dark mode for the entire application. Then we add dark:bg-gray-800 to the body element to provide a dark background for the Next'js application when it is in dark mode. yarn dev will run the application, and you should see a dark background.

How to implement dark mode in next js tailwind? ›

Implementing dark mode in Next. js is effortlessly achieved through the dark variant in Tailwind CSS. Simply prefix properties with dark: to designate them for dark mode. For instance, utilizing dark:text-red-700 adjusts the text color to red-700 when the theme is switched to dark.

How do I enable dark mode in Tailwind CSS react? ›

Step 1: Open Your Tailwind. config. js
  1. true Dark mode is enabled globally for the entire application.
  2. false Dark mode is disabled.
  3. media Dark mode is activated based on the prefers-color-scheme media query. ...
  4. class Dark mode is controlled by adding a class to the HTML element when dark mode is active.
Jan 2, 2024

How to implement dark and light theme in React? ›

Adding a switch can be done by importing the Switch Component from Material UI and then defining it in the App component. Once you have added the Switch button, let's define the toggle states so that the switch can toggle the dark or light theme. Below is how we can define the states using the useState() hook in React.

How to setup nextjs with Tailwind CSS? ›

Setting up Tailwind CSS in a Next.js project.
  1. Create your project. Start by creating a new Next.js project if you don't have one set up already. ...
  2. Install Tailwind CSS. ...
  3. Configure your template paths. ...
  4. Add the Tailwind directives to your CSS. ...
  5. Start your build process. ...
  6. Start using Tailwind in your project.

Does tailwind UI support dark mode? ›

It's important to note that because of file size considerations, the dark mode variant is not enabled in Tailwind by default. To enable it, set the darkMode option in your tailwind. config.

How to implement dark mode using CSS? ›

Steps to create Dark-Mode websites using HTML, CSS, and JavaScript:
  1. Create an HTML document.
  2. Add CSS properties to the body and add dark-mode class properties in CSS.
  3. Add buttons to switch between Dark and Light Mode.
  4. Add functionality to buttons to switch between dark-mode and light-mode using JavaScript.
May 10, 2024

How to add shadow using Tailwind CSS? ›

Adding an outer shadow

Use the shadow-sm , shadow , shadow-md , shadow-lg , shadow-xl , or shadow-2xl utilities to apply different sized outer box shadows to an element.

How do I turn off dark mode in Tailwind? ›

Toggling dark mode manually

/** @type {import('tailwindcss').Config} */ module.exports = { darkMode: 'selector', // ... } Now instead of dark:{class} classes being applied based on prefers-color-scheme , they will be applied whenever the dark class is present earlier in the HTML tree.

How do I customize Tailwind CSS in react? ›

Follow these steps to integrate Tailwind CSS into your React project:
  1. Install Tailwind CSS. ...
  2. Configure PostCSS. ...
  3. Create CSS File. ...
  4. Import CSS in Your React Application. ...
  5. Start Using Tailwind CSS Classes. ...
  6. Create a Navbar Component. ...
  7. Import the Navbar Component.
Oct 17, 2023

How do I change my theme from light to dark? ›

On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

How do you implement a dark theme in an app? ›

Apps must opt in to Force Dark by setting android:forceDarkAllowed="true" in the activity's theme. This attribute is set on all of the system- and AndroidX-provided light themes, such as Theme. Material.

What is dark theme vs light theme users? ›

The Verdict. The choice between dark mode and light mode is largely subjective and depends on your specific requirements, preferences, and ambient light conditions. The dark mode is ideal for reducing eye strain in low-light conditions and conserving battery power.

Is Tailwind better than bootstrap? ›

Tailwind gives you more freedom to design things your way, while Bootstrap has lots of ready-to-use parts that make building websites quicker. If you really care about making your site look just right, Tailwind might be better. But if you need to get a website up fast, Bootstrap could save you a lot of time.

Can you use Tailwind and CSS at the same time? ›

A guide to using Tailwind with common CSS preprocessors like Sass, Less, and Stylus. Since Tailwind is a PostCSS plugin, there's nothing stopping you from using it with Sass, Less, Stylus, or other preprocessors, just like you can with other PostCSS plugins like Autoprefixer.

Can I use Tailwind with JavaScript? ›

Plugins let you register new styles for Tailwind to inject into the user's stylesheet using JavaScript instead of CSS. To get started with your first plugin, import Tailwind's plugin function from tailwindcss/plugin .

How do you make a dark mode switcher with tailwind? ›

Enabling Dark Mode in Tailwind CSS

/** @type {import('tailwindcss'). Config} */ module. exports = { darkMode: 'class', // ... } Once enabled, you can use the dark class on the html tag (or any other element) to apply the dark mode styles to all children.

How do I change my wing IDE to dark mode? ›

Editor Color Configuration

This can be changed with the User Interface > Light Editor and User Interface > Dark Editor preferences, by selecting Use Selected and choosing one of the available light or dark themes.

How to toggle dark mode JavaScript? ›

Steps to Create Dark/Light Mode
  1. Create an HTML Document.
  2. Create CSS for the document file as well as for dark mode.
  3. Add a switch/toggler to toggle between light and dark modes.
  4. Add functionality to the switch/toggler to toggle between light and dark mode using JavaScript or jQuery code.
May 14, 2024

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6327

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.