Dark mode with TailwindCSS in under 7 minutes

  Рет қаралды 30,951

Built With Code

Built With Code

Күн бұрын

In this video, we are going to walkthrough how to add dark mode support using TailwindCSS.
💾 Starter code: github.com/Built-With-Code/ta...
⏭️ Watch This Next: • Custom themes with Tai...
Have a question about anything in this video? Drop it in the comments!
⏰ Timecodes ⏰
0:00 - Intro
0:12 - Starter code
0:27 - Method #1: System preference
2:11 - Method #2: Custom class name
5:48 - Simple animation for mode switching
6:38 - Outro
-------------------------------
Welcome to Built With Code, a channel focused on intermediate/advanced frontend web development topics. Some of my favorite tools are Next.js, TailwindCSS, and Framer Motion. If you find value from the videos, a like and/or subscribe is much appreciated!

Пікірлер: 84
@hawkdawg443
@hawkdawg443 6 ай бұрын
Thanks man, that transition part is a cherry on top
@ethan.creates
@ethan.creates 9 ай бұрын
Dope video man! Loved the transition tip and everything else was very clear!
@dr.insane9929
@dr.insane9929 2 ай бұрын
Thanks brotha, The transition was a lifesaver.
@fulviavaz
@fulviavaz 4 ай бұрын
Amazing! Your solution is much simpler that I was trying! Thanks!
@chadvise3308
@chadvise3308 Күн бұрын
This was so helpful thanks for the content ❤
@the.mackuha
@the.mackuha 7 ай бұрын
Straight to the point! Thanks man🔥
@builtwithcode
@builtwithcode 6 ай бұрын
Thanks for watching, glad it was helpful!
@vladanisov1689
@vladanisov1689 6 ай бұрын
This was incredibly helpful, thank you my man!
@builtwithcode
@builtwithcode 6 ай бұрын
Glad to hear it!
@felipepadovani6263
@felipepadovani6263 5 ай бұрын
Excellent dude !! helped me a lot
@DEBUGENTITY
@DEBUGENTITY 6 ай бұрын
Loved this video.. Thanks for making this🎉
@masoodacheampong5385
@masoodacheampong5385 2 ай бұрын
Thank you very much, at the end, I knew you would use useState to handle the transition, so I did mine just like that.
@francesco_gramazio
@francesco_gramazio 2 ай бұрын
freaking awesome, thanks mate !
@musicworld1717
@musicworld1717 5 ай бұрын
it helped me immensly..thanks bro
@angeline9552
@angeline9552 4 ай бұрын
This is so helpful, thank u :)
@sybrespect3161
@sybrespect3161 5 ай бұрын
you just got a new subscriber 👍🏾
@akshaykumar-xu2be
@akshaykumar-xu2be 5 ай бұрын
Thanks bro. 💯Worth watching . just SUBSCRIBED
@builtwithcode
@builtwithcode 5 ай бұрын
Glad it was helpful!
@Yesterday_i_ate_rat
@Yesterday_i_ate_rat 5 ай бұрын
Top notch content❤
@abishekbaiju1705
@abishekbaiju1705 Ай бұрын
Excellent tutorial. thanks a lot
@builtwithcode
@builtwithcode 26 күн бұрын
Glad it was helpful!
@nisargjaiswal4264
@nisargjaiswal4264 9 ай бұрын
Suggestion for video: Keep mobile users in mind, so zoom in the code for better readability. Nice video, keep it up buddy ✌️
@builtwithcode
@builtwithcode 8 ай бұрын
Thanks for the callout, will try to zoom in more in the future.
@davonnev696
@davonnev696 6 ай бұрын
Definitely doing this .,.!!
@killiankavanagh3854
@killiankavanagh3854 5 ай бұрын
Thanks so much man
@adityarajawat3185
@adityarajawat3185 8 ай бұрын
Nice explanation
@builtwithcode
@builtwithcode 8 ай бұрын
Thanks!
@nested9301
@nested9301 Ай бұрын
oh tailwind is awesome !!
@tutopoints1920
@tutopoints1920 2 ай бұрын
Nice One
@AnkitKumar-dy5um
@AnkitKumar-dy5um 3 ай бұрын
That's super easy :)
@abhishekchaudhary4058
@abhishekchaudhary4058 3 ай бұрын
Thanks
@it_is_random
@it_is_random 18 күн бұрын
thx
@ahrenwagner2993
@ahrenwagner2993 27 күн бұрын
Great video :) Is there a way to do this without needing to prefix dark in your markup?
@builtwithcode
@builtwithcode 26 күн бұрын
The prefix is how tailwind recognizes which styles to apply in light vs dark mode. You could leverage a "non-Tailwind" approach using CSS variables and the prefers-color-scheme CSS selector. At that point I just prefer the "dark" prefix :)
@AhmedMohamed-hz9bs
@AhmedMohamed-hz9bs 3 ай бұрын
❤❤❤❤
@chan4est
@chan4est 2 ай бұрын
Super helpful! I'm curious how you handle both use cases at once. Basically using the user's preferred theme during first site use but once the user chooses their preferred theme, it overrides their OS's preference upon every revisit. Also how do you handle images that need to be swapped out in dark mode?
@builtwithcode
@builtwithcode 2 ай бұрын
You'll want to store the preferred theme in localStorage. If you see that the storage is empty, then default to the OS preference. For images, I imagine the best way is to have the theme stored in some sort of global state (e.g., with React Context), then dynamically set the img src based on which theme is active.
@alexdin1565
@alexdin1565 22 күн бұрын
thanks bro please can you make your video about "Custom themes with TailwindCSS in under 9 minutes" with this ? i mean user can switch thems and change from dark and light in same time
@nonefvnfvnjnjnjevjenjvonej3384
@nonefvnfvnjnjnjevjenjvonej3384 Ай бұрын
so how does tailwind get the dark mode actually. does it set it to dark on the html element?
@MyGeorge1964
@MyGeorge1964 9 ай бұрын
Is there is way to abstract all of the dark mode css to the tailwind config and maybe the stylesheet so that you only need to specify one variable in the html or jsx that will change depending on whether it's dark or light?
@builtwithcode
@builtwithcode 8 ай бұрын
Yes you can definitely do this. One way is to use reusable components so you add the light and dark mode classes once in the component definition and have multiple instances. The other is in the stylesheet you can use the @apply declaration and directly use the appropriate tailwind classes in CSS.
@pastiille7467
@pastiille7467 4 ай бұрын
merci
@lolhp._.
@lolhp._. Ай бұрын
anyone know how to do the same thing but for the html tag?
@tasfin660
@tasfin660 2 ай бұрын
for some reason after writing * { @apply transition-colors duration-200; } this, all my other transition duration not working
@anonymoussloth6687
@anonymoussloth6687 9 ай бұрын
Could u do a video with dark mode using css variables - which could be extended to multiple themes
@builtwithcode
@builtwithcode 8 ай бұрын
Thanks for the suggestion, I'll consider it for a future video.
@Kyruu28
@Kyruu28 2 ай бұрын
So instead of using text, I chose to use icons for the toggle instead. I wonder how to add transition for the toggle icon so it can fade or spin etc. ? I've tried added transition class to the icon element but it doesn't work...
@builtwithcode
@builtwithcode 2 ай бұрын
Framer Motion would help here to animate when one icon unmounts and another comes into view
@cabaucom376
@cabaucom376 6 ай бұрын
What if you wanted a dropdown menu for “system”, “dark”, “light”? I would like system to be the default but if someone specifies then update accordingly.
@builtwithcode
@builtwithcode 6 ай бұрын
The system value is stored in the `prefers-color-scheme` CSS property. In TS/JS you can check if it's dark using `window.matchMedia('(prefers-color-scheme: dark)').matches`
@daniali4619
@daniali4619 6 ай бұрын
Can its change multiple pages mood, i means if we set toggle btn in nav bar then how to change multiple pages dark mood i think then its not work, I'm right or not ?
@builtwithcode
@builtwithcode 6 ай бұрын
For using across an entire website, you'll want to setup a React Context that stores the theme value, and then wrap the entire app in that context's provider. You then connect the toggle button to update the value in the context, which will propagate to other pages.
@rayudubharani
@rayudubharani 2 ай бұрын
sir what about system default
@ahmedahmedx9600
@ahmedahmedx9600 3 ай бұрын
What if i want also the darkmode got saved in browser when toggling the button so if its still darkmode even when page refreshed
@builtwithcode
@builtwithcode 3 ай бұрын
You can store the active theme in local storage and read it on page load
@nested9301
@nested9301 Ай бұрын
cookies + server side rendering
@userre85
@userre85 3 ай бұрын
Which VS code theme are you using?
@builtwithcode
@builtwithcode 3 ай бұрын
"Dark Modern" that comes with vscode
@angeline9552
@angeline9552 4 ай бұрын
how would you implement a site-wide light/dark mode toggle?
@builtwithcode
@builtwithcode 4 ай бұрын
Store the state in the main App component, then apply the "dark" class in the or at the top App level.
@martinfriday5481
@martinfriday5481 2 ай бұрын
How??? I created a button component that I want to reuse across the site, how do I make it toggle the whole thing
@maeel
@maeel 5 ай бұрын
can i have a list of every vs code extension do you have for react?
@builtwithcode
@builtwithcode 5 ай бұрын
The main ones I use in my workflow: - ES7+ React/Redux/React-Native snippets - Tailwind CSS IntelliSense - Prettier - Code formatter - Error Lens
@maeel
@maeel 5 ай бұрын
thanks a lot @@builtwithcode
@qotibamhamed6467
@qotibamhamed6467 5 ай бұрын
I'm experiencing an issue where, when I switch to light mode in my system, dark mode functions perfectly. However, when I enable dark mode in my system, the toggle between dark mode and light mode doesn't work. Can someone please assist me with this problem? (I replicated your steps exactly for importing dark mode in Tailwind.)
@builtwithcode
@builtwithcode 5 ай бұрын
Hmm that's odd. Did you update the Tailwind config file to use "class" for dark mode functionality?
@martinfriday5481
@martinfriday5481 2 ай бұрын
I created a button component and applied this dark and light mode to it, but when I call the component in another component and write "dark" styles, it doesn't work, is there any way around this??
@builtwithcode
@builtwithcode 2 ай бұрын
Did you setup dark mode in the tailwind config?
@martinfriday5481
@martinfriday5481 2 ай бұрын
@@builtwithcode yea, I did everything there, but the dark mode only applied to that button component and when I exported it to a page, it didn't affect the rest of the page
@builtwithcode
@builtwithcode 2 ай бұрын
Are you applying the “dark” class to the root body or html tag? Tailwind should then enable dark mode for all the children
@martinfriday5481
@martinfriday5481 2 ай бұрын
@@builtwithcode I should add the dark class to the HTML?
@martinfriday5481
@martinfriday5481 2 ай бұрын
@@builtwithcode Then how will my button component toggle the dark mode on and off on button click, that's the issue
@DrJake-dl7rg
@DrJake-dl7rg 4 ай бұрын
what if we want to refresh the page?
@builtwithcode
@builtwithcode 4 ай бұрын
You would need to persist the state in storage, e.g., using LocalStorage.
@DrJake-dl7rg
@DrJake-dl7rg 4 ай бұрын
@@builtwithcode yeah thanks for replying! I'm having a rough time connecting the react state and Local Storage properly and couldn't find a good tutorial about that, yours was good tho.
@rishabhagrawal8938
@rishabhagrawal8938 6 ай бұрын
How to add one condition inside an another condition like inside dark want to add another condition . Please help it is urgent or please share Where I can talk to yo
@builtwithcode
@builtwithcode 6 ай бұрын
What type of other condition? If it's something stored in a component variable, then in the styling class names you can use a ternary operator with the variable and use the "dark" prefixing to limit the styling to dark mode.
@deylerdeyler6401
@deylerdeyler6401 5 ай бұрын
does this work on next 13?
@builtwithcode
@builtwithcode 5 ай бұрын
Yes this should work on Next 13
@arupde6320
@arupde6320 8 ай бұрын
be regular
@haha-eg8fj
@haha-eg8fj 5 ай бұрын
In my case I don’t need to give my document a dark class name and it just works after I confige tailwind.config.ts. I don’t know why.
@SrEdgar
@SrEdgar 27 күн бұрын
how?
10 Tailwind Classes I Wish I Knew Earlier
13:31
Web Dev Simplified
Рет қаралды 173 М.
UI Libraries Are Dying, Here's Why
13:28
Theo - t3․gg
Рет қаралды 289 М.
Идеально повторил? Хотите вторую часть?
00:13
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 5 МЛН
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 36 МЛН
Can A Seed Grow In Your Nose? 🤔
00:33
Zack D. Films
Рет қаралды 24 МЛН
Adding a Dark Mode to Vue in 5 Minutes
4:25
LearnVue
Рет қаралды 26 М.
8 TailwindCSS Classes I Wish I Found Earlier
4:48
Tom Is Loading
Рет қаралды 72 М.
Create Stunning Landing Page with Next js and Framer Motion
23:27
Use these instead of vh
6:06
Kevin Powell
Рет қаралды 494 М.
Animated gradient text with TailwindCSS
7:44
Built With Code
Рет қаралды 5 М.
React Dark Mode Toggle/Theme - Complete Guide
12:29
Code Complete
Рет қаралды 23 М.
This UI component library is mind-blowing
8:23
Beyond Fireship
Рет қаралды 638 М.
So, you want to build apps & websites?
9:34
Sajid
Рет қаралды 36 М.
Building Light/Dark Mode in React with Tailwind CSS
13:07
Code Radiance
Рет қаралды 1,2 М.
How to Vim in 2023: Tips and Tricks
15:53
ThePrimeagen
Рет қаралды 406 М.
Nokia 3310 top
0:20
YT 𝒯𝒾𝓂𝓉𝒾𝓀
Рет қаралды 3,5 МЛН
Vision Pro наконец-то доработали! Но не Apple!
0:40
ÉЖИ АКСЁНОВ
Рет қаралды 88 М.