
Hi, welcome here In this tutorial, we will Next Js Tailwind CSS Toggle app using Next js and TailwindCss. This is the complete tutorial on how to develop a toggle app in Next Js. The prerequisites for this project are as follows:
- Node.js should be installed in your system.
- Create a brand new ReactJs project. Just copy the code below:
code to create Nextjs project:
npx create-next-app my-app
after it run the below command:
cd my-app
after it install the required package by entering the below command:
npm i next-themes
after its installation run the below commands to install Tailwind css into your project:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
The brand-new Next Js project should look like this:

Here is your index.js file, copy the code below and paste it into index.js.
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import ThemeToggler from './ThemeToggler'
export default function Home() {
return (
<>
Laptops that you can't afford even in 2022.
Lorem ipsum dolor sit amet. Cum laudantium laborum ut saepe
facilis aut rerum corporis qui debitis voluptas. Rerum dolores aut
voluptas galisum aut iure repellendus.
Read more
>
)
}
Here is the code for _app.js so copy the code below and paste it into _app.js.
import '../styles/globals.css';
import { ThemeProvider } from 'next-themes';
function MyApp({ Component, pageProps }) {
return (
);
}
export default MyApp;
Make a new component and name it ‘ThemeToggler.js’ and then Copy the code below and paste it into ThemeToggler.js.
import { useTheme } from 'next-themes';
import { useState, useEffect } from 'react';
const ThemeToggler = () => {
const { theme, setTheme } = useTheme();
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
if (!mounted) return null;
return (
);
};
export default ThemeToggler;
Copy the code below and paste it into tailwind.config.js.
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
content: [
"./app/**/*.{js,ts,jsx,tsx}",
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
// Or if using `src` directory:
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Here is your code for the globals.css file:
@tailwind base;
@tailwind components;
@tailwind utilities;
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
body {
color: white;
background: black;
}
}
After writing the code into the files (don’t forget to save it ,) you can run the command below:
npm run dev