Dark mode with TailwindCSS in under 7 minutes

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024

Комментарии • 95

  • @hawkdawg443
    @hawkdawg443 8 месяцев назад +13

    Thanks man, that transition part is a cherry on top

  • @nisargjaiswal4264
    @nisargjaiswal4264 11 месяцев назад +5

    Suggestion for video: Keep mobile users in mind, so zoom in the code for better readability.
    Nice video, keep it up buddy ✌️

    • @builtwithcode
      @builtwithcode  10 месяцев назад +2

      Thanks for the callout, will try to zoom in more in the future.

  • @ethan.creates
    @ethan.creates 11 месяцев назад +4

    Dope video man! Loved the transition tip and everything else was very clear!

  • @TOONSSTATION
    @TOONSSTATION Месяц назад

    Nice

  • @tasfin660
    @tasfin660 4 месяца назад

    for some reason after writing
    * {
    @apply transition-colors duration-200;
    } this, all my other transition duration not working

  • @alexdin1565
    @alexdin1565 2 месяца назад

    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

  • @K4NEKIIIIa
    @K4NEKIIIIa 14 дней назад

    how we can make the navbar dark mode on all the pages??

  • @chan4est
    @chan4est 4 месяца назад

    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  4 месяца назад

      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.

  • @lolhp._.
    @lolhp._. 3 месяца назад

    anyone know how to do the same thing but for the html tag?

  • @nonefvnfvnjnjnjevjenjvonej3384
    @nonefvnfvnjnjnjevjenjvonej3384 3 месяца назад

    so how does tailwind get the dark mode actually. does it set it to dark on the html element?

  • @fulviavaz
    @fulviavaz 6 месяцев назад +1

    Amazing! Your solution is much simpler that I was trying! Thanks!

  • @DEBUGENTITY
    @DEBUGENTITY 8 месяцев назад +1

    Loved this video.. Thanks for making this🎉

  • @Kyruu28
    @Kyruu28 4 месяца назад

    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  4 месяца назад

      Framer Motion would help here to animate when one icon unmounts and another comes into view

  • @davonne007
    @davonne007 8 месяцев назад +1

    Definitely doing this .,.!!

  • @rayudubharani
    @rayudubharani 4 месяца назад

    sir what about system default

  • @the.mackuha
    @the.mackuha 8 месяцев назад +3

    Straight to the point! Thanks man🔥

    • @builtwithcode
      @builtwithcode  8 месяцев назад

      Thanks for watching, glad it was helpful!

  • @01dom
    @01dom Месяц назад

    Not good to be honest, would be better to define colors and change them with css so you wouldn't have to write dark everywhere

    • @danishnazirpeer3422
      @danishnazirpeer3422 Месяц назад

      That's what i was thinking when i clicked the video. Instead we got the most basic approach.

  • @ahrenwagner2993
    @ahrenwagner2993 2 месяца назад

    Great video :)
    Is there a way to do this without needing to prefix dark in your markup?

    • @builtwithcode
      @builtwithcode  2 месяца назад

      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 :)

  • @dr.insane9929
    @dr.insane9929 4 месяца назад +1

    Thanks brotha, The transition was a lifesaver.

  • @sybrespect3161
    @sybrespect3161 7 месяцев назад +1

    you just got a new subscriber 👍🏾

  • @AnkitKumar-dy5um
    @AnkitKumar-dy5um 4 месяца назад

    That's super easy :)

  • @killiankavanagh3854
    @killiankavanagh3854 7 месяцев назад +1

    Thanks so much man

  • @bryan_11016
    @bryan_11016 23 дня назад

    Thanks a lot mate. I was struggling with Dark mode in Tailwind and you made it look simple. Appreciate it👍❤

  • @sai-code
    @sai-code 19 дней назад

    thanks

  • @deelakaransilu814
    @deelakaransilu814 28 дней назад

    thank u ❤

  • @tutopoints1920
    @tutopoints1920 3 месяца назад

    Nice One

  • @sad_man_no_talent
    @sad_man_no_talent 2 месяца назад

    thx

  • @AhmedMohamed-hz9bs
    @AhmedMohamed-hz9bs 5 месяцев назад

    ❤❤❤❤

  • @felipepadovani6263
    @felipepadovani6263 7 месяцев назад +1

    Excellent dude !! helped me a lot

  • @userre85
    @userre85 5 месяцев назад

    Which VS code theme are you using?

    • @builtwithcode
      @builtwithcode  5 месяцев назад

      "Dark Modern" that comes with vscode

  • @ahmedahmedx9600
    @ahmedahmedx9600 5 месяцев назад

    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  5 месяцев назад

      You can store the active theme in local storage and read it on page load

    • @nested9301
      @nested9301 3 месяца назад

      cookies + server side rendering

  • @masoodacheampong5385
    @masoodacheampong5385 4 месяца назад

    Thank you very much, at the end, I knew you would use useState to handle the transition, so I did mine just like that.

  • @chadvise3308
    @chadvise3308 2 месяца назад

    This was so helpful thanks for the content ❤

  • @deylerdeyler6401
    @deylerdeyler6401 7 месяцев назад

    does this work on next 13?

    • @builtwithcode
      @builtwithcode  7 месяцев назад

      Yes this should work on Next 13

  • @haha-eg8fj
    @haha-eg8fj 7 месяцев назад +1

    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 2 месяца назад +1

      how?

  • @qotibamhamed6467
    @qotibamhamed6467 7 месяцев назад

    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  7 месяцев назад

      Hmm that's odd. Did you update the Tailwind config file to use "class" for dark mode functionality?

  • @mhalwasoffice6132
    @mhalwasoffice6132 Месяц назад

    how can i share this useState betwen the componants

    • @builtwithcode
      @builtwithcode  Месяц назад

      If you want to share across multiple components, consider using state management such as React Context and storing the theme information there

    • @mhalwasoffice6132
      @mhalwasoffice6132 Месяц назад

      @@builtwithcode ty

  • @cabaucom376
    @cabaucom376 8 месяцев назад

    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  8 месяцев назад +2

      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`

  • @nested9301
    @nested9301 3 месяца назад

    oh tailwind is awesome !!

  • @francesco_gramazio
    @francesco_gramazio 4 месяца назад

    freaking awesome, thanks mate !

  • @daniali4619
    @daniali4619 8 месяцев назад

    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  8 месяцев назад +1

      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.

  • @akshaykumar-xu2be
    @akshaykumar-xu2be 7 месяцев назад

    Thanks bro. 💯Worth watching . just SUBSCRIBED

  • @musicworld1717
    @musicworld1717 7 месяцев назад

    it helped me immensly..thanks bro

  • @martinfriday5481
    @martinfriday5481 4 месяца назад

    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  4 месяца назад

      Did you setup dark mode in the tailwind config?

    • @martinfriday5481
      @martinfriday5481 4 месяца назад

      @@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  4 месяца назад

      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 4 месяца назад

      @@builtwithcode I should add the dark class to the HTML?

    • @martinfriday5481
      @martinfriday5481 4 месяца назад

      @@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 6 месяцев назад

    what if we want to refresh the page?

    • @builtwithcode
      @builtwithcode  6 месяцев назад +1

      You would need to persist the state in storage, e.g., using LocalStorage.

    • @DrJake-dl7rg
      @DrJake-dl7rg 6 месяцев назад

      @@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 8 месяцев назад

    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  8 месяцев назад

      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.

  • @angeline9552
    @angeline9552 6 месяцев назад

    This is so helpful, thank u :)

  • @abhishekchaudhary4058
    @abhishekchaudhary4058 5 месяцев назад

    Thanks

  • @MyGeorge1964
    @MyGeorge1964 11 месяцев назад

    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  10 месяцев назад

      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.

  • @maeel
    @maeel 7 месяцев назад

    can i have a list of every vs code extension do you have for react?

    • @builtwithcode
      @builtwithcode  7 месяцев назад

      The main ones I use in my workflow:
      - ES7+ React/Redux/React-Native snippets
      - Tailwind CSS IntelliSense
      - Prettier - Code formatter
      - Error Lens

    • @maeel
      @maeel 7 месяцев назад

      thanks a lot @@builtwithcode

  • @angeline9552
    @angeline9552 6 месяцев назад

    how would you implement a site-wide light/dark mode toggle?

    • @builtwithcode
      @builtwithcode  6 месяцев назад

      Store the state in the main App component, then apply the "dark" class in the or at the top App level.

    • @martinfriday5481
      @martinfriday5481 4 месяца назад

      How??? I created a button component that I want to reuse across the site, how do I make it toggle the whole thing

  • @anonymoussloth6687
    @anonymoussloth6687 11 месяцев назад

    Could u do a video with dark mode using css variables - which could be extended to multiple themes

    • @builtwithcode
      @builtwithcode  10 месяцев назад

      Thanks for the suggestion, I'll consider it for a future video.

  • @Yesterday_i_ate_rat
    @Yesterday_i_ate_rat 7 месяцев назад

    Top notch content❤

  • @abishekbaiju1705
    @abishekbaiju1705 3 месяца назад

    Excellent tutorial. thanks a lot

  • @vladanisov1689
    @vladanisov1689 8 месяцев назад

    This was incredibly helpful, thank you my man!

  • @pastiille7467
    @pastiille7467 6 месяцев назад

    merci

  • @adityarajawat3185
    @adityarajawat3185 10 месяцев назад

    Nice explanation

  • @arupde6320
    @arupde6320 10 месяцев назад

    be regular

  • @kruthiks5400
    @kruthiks5400 9 дней назад

    Thank you man