,

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
7 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
7 months ago

video is blurry even at 1080p

Guillermo Carmona
7 months ago

Very useful!

Resul Abdulla
7 months ago

Bro cookie or local saved ??? 🤔🤔

Adem Meral
7 months ago

it is not suitable for SEO

Mohammad Tamaddon
7 months ago

Thanks for the clear and great content 💖

Nabi Abdelkader
7 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
7 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
7 months ago

Thanks it was helpful t

Artemii
7 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
7 months ago

Thanks for this 💻

جعفر محمد
7 months ago

Jamie B
7 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
7 months ago

Thank you for this!!

6099x
7 months ago

Hey thank you Tuomo 👍

Zainab Salah
7 months ago

this is amazing tysm

Kacper Kowalski
7 months ago

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

Amancharla Manas
7 months ago

Thanks a lot brother !!

Frederick Bruce
7 months ago

Great video! Thank you!

Gilbert Delaloye
7 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? 🙂