,

Getting started with Dark Mode in Next JS 13 App Directory using TailwindCSS: A beginner’s guide

Posted by






Dark Mode in Next JS 13 App Directory with TailwindCSS (for beginners)

Dark Mode in Next JS 13 App Directory with TailwindCSS (for beginners)

Dark mode has become a popular feature in modern web applications. In this article, we will explore how to implement dark mode in a Next JS 13 app directory using TailwindCSS. This tutorial is aimed at beginners who are looking to enhance the user experience of their web applications by adding a dark mode feature.

Getting Started

First, make sure you have Next JS 13 and TailwindCSS installed in your project. If not, you can install them using the following commands:


npx create-next-app@latest my-app
cd my-app
npm install tailwindcss

Once you have Next JS 13 and TailwindCSS installed, you can start implementing the dark mode feature in your app.

Implementing Dark Mode

The first step is to create a dark mode utility class in your TailwindCSS configuration file. Open the tailwind.config.js file and add the following code:


module.exports = {
theme: {
extend: {
colors: {
dark: {
100: '#1a202c',
200: '#2d3748',
300: '#4a5568',
400: '#718096',
500: '#a0aec0',
600: '#cbd5e0',
700: '#e2e8f0',
800: '#f7fafc',
900: '#f7fafc',
},
},
},
},
}

This will create a set of dark mode colors that you can use in your application. Next, you need to create a toggle button to switch between light mode and dark mode. You can do this by adding the following HTML and CSS to your project:



This will create a toggle button with a switch that can be used to change the color scheme of your app. Finally, you need to write some JavaScript to add functionality to the toggle button. You can do this by adding the following code to the script section of your HTML file:



Conclusion

In this tutorial, we learned how to implement dark mode in a Next JS 13 app directory using TailwindCSS. By following these steps, you can enhance the user experience of your web application and provide a more immersive and comfortable experience for your users. Remember, dark mode isn’t just a trend – it’s an important accessibility feature that can benefit a wide range of users. So go ahead and give it a try in your own projects!


0 0 votes
Article Rating
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Wade Thomas
11 months ago

Thanks for this video I really appreciate it. This is the only one I found that implemented light and dark mode with the new app structure.

Shohanur Shohan
11 months ago

video is blurry even at 1080p

Guillermo Carmona
11 months ago

Very useful!

Resul Abdulla
11 months ago

Bro cookie or local saved ??? 🤔🤔

Adem Meral
11 months ago

it is not suitable for SEO

Mohammad Tamaddon
11 months ago

Thanks for the clear and great content 💖

Nabi Abdelkader
11 months ago

I want to change the value of the variables, but it works for me (global.css)

:root {

–background: #eee;

–foreground: #1f1b1b;

}

@media (prefers-color-scheme: dark) {

:root {

–background: #911818;

–foreground: #989815;

}

}

/*

# Doesn't work either

[data-theme='dark'] {

:root {

–background: #911818;

–foreground: #989815;

}

}

*/

body {

color: var(–foreground);

background-color: var(–background);

}

Pawel Gajewski
11 months ago

Hi that's a great tutorial, can you tell me why when dark mode is selected and I navigate to another page it reverts to light mode and then back to dark again? Is this how this is supposed to work?

Piotr Rodzen
11 months ago

Thanks it was helpful t

Artemii
11 months ago

InvalidCharacterError: Failed to execute 'add' on 'DOMTokenList': The token provided ('[object Object]') contains HTML space characters, which are not valid in tokens.

Claud Watari
11 months ago

Thanks for this 💻

جعفر محمد
11 months ago

Jamie B
11 months ago

Very good content! Subscribing!
It would be great if you did not thank those who leave a nice comment and instead answered the questions of those who need help.

Jared Brown
11 months ago

Thank you for this!!

6099x
11 months ago

Hey thank you Tuomo 👍

Zainab Salah
11 months ago

this is amazing tysm

Kacper Kowalski
11 months ago

My issue has been resolved by you :), nice video

Amancharla Manas
11 months ago

Thanks a lot brother !!

Frederick Bruce
11 months ago

Great video! Thank you!

Gilbert Delaloye
11 months ago

Hey man – thank you so much. I almost never comment or like videos but you helped me so so much – I just had to☺ Do you have a patreon? 🙂