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!
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.
video is blurry even at 1080p
Very useful!
Bro cookie or local saved ??? 🤔🤔
it is not suitable for SEO
Thanks for the clear and great content 💖
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);
}
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?
Thanks it was helpful t
InvalidCharacterError: Failed to execute 'add' on 'DOMTokenList': The token provided ('[object Object]') contains HTML space characters, which are not valid in tokens.
Thanks for this 💻
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.
Thank you for this!!
Hey thank you Tuomo 👍
this is amazing tysm
My issue has been resolved by you :), nice video
Thanks a lot brother !!
Great video! Thank you!
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? 🙂