Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tailwind css dark mode does not enable

I am trying to enable dark mode based on the system default using tailwind.

To accomplish this I am using the plugin: Tailwind dark mode.

My config fail for tailwind is as follows:

defaultTheme = require('tailwindcss/defaultTheme');
module.exports = {
    experimental: {
        darkModeVariant: true
    },
    purge: [],
  theme: {
      extend: {
          fontFamily: {
              sans: ['Nunito', ...defaultTheme.fontFamily.sans],
          },
          screens: {
              'dark': {'raw': '(prefers-color-scheme: dark)'},
              // => @media (prefers-color-scheme: dark) { ... }
          },
      },
  },
  variants: {
      backgroundColor: ['dark', 'dark-hover', 'dark-group-hover', 'dark-even', 'dark-odd'],
      borderColor: ['dark', 'dark-disabled', 'dark-focus', 'dark-focus-within'],
      textColor: ['dark', 'dark-hover', 'dark-active', 'dark-placeholder'],
      opacity: ['responsive', 'hover', 'focus', 'disabled']
  },
  plugins: [require('tailwindcss-dark-mode')()],
}


defaultTheme = require('tailwindcss/defaultTheme');

And in my html file I am adding the following:

<span class="dark:text-yellow-400">
    1
</span>

The plugin checks for my dark mode but the text wont turn yellow it stays black.

Does anyone know why it wont work?

like image 611
Robin Avatar asked Sep 25 '20 11:09

Robin


People also ask

How to enable Dark mode in tailwind theme?

For the sake of simplicity, we will just modify the tailwind configuration to allow for basic dark mode. First, we need to extend the theme to define light and dark modes. This uses the css property prefers-color-scheme which can have light or dark values.

How do I Turn on dark mode manually using CSS?

By default this uses the prefers-color-scheme CSS media feature, but you can also build sites that support toggling dark mode manually using the ‘class’ strategy. If you want to support toggling dark mode manually instead of relying on the operating system preference, use the class strategy instead of the media strategy:

Is tailwind's dark variant working with angular 11?

I am using Tailwind in an Angular 11 application and when using @apply dark:bg-gray-700 inside the component's css file, the dark variant is not applied whereas if I set dark:bg-gray-700 directly in the component's html file, it's working. Note that this seems to only apply to the dark variant. Other variants are working fine.

How do I enable Dark mode in a wrapper element?

How you do that is up to you. prefers-dark.js is provided as an option, which is a simple script that enables dark mode based on the user's system theme. Add that extra mode-dark class to the wrapper element ( body in this case). To change the theme based on user preferences through the plugin:


2 Answers

First things first, now TailWIndCSS supports dark-mode out-of-the-box by adding the dark: prefix before any class after it is enabled. Not sure if it is related to the question, but thought you need to know.

The plugin you are using states the following use for enabling dark mode:

< tailwind.config.js >

...
plugins: [
  require('tailwindcss-dark-mode')()
]
// To enable dark mode for all classes:
variants: ['dark', 'dark-hover', 'dark-group-hover', 'dark-even', 'dark-odd', ...]

// To enable dark mode for only single utility class:
variants: {
  backgroundColor: ['dark', 'dark-hover', 'dark-group-hover', 'dark-even', 'dark-odd']
}
...

It also states that

Styles generated by this plugin are only used if the selector is applied to the <html> element. How you do that is up to you. prefers-dark.js is provided as an option, which is a simple script that enables dark mode based on the user's system theme.

So, to enable dark mode through the plugin, use:

< mypage.html >

...
<body class="mode-dark">
  <div class="bg-white dark:bg-black">
    <p class="text-black dark:text-white">
      My eyes are grateful.

      <a class="text-blue-300 hover:text-blue-400">
        Learn more
      </a>
    </p>
  </div>
...
</body>

Add that extra mode-dark class to the wrapper element (body in this case).

To change the theme based on user preferences through the plugin:

< mypage.html >

<head>
  <script src="to/prefers-dark.js"></script>
</head>

...
<body class="mode-dark">
  <div class="bg-white dark:bg-black">
    <p class="text-black dark:text-white">
      My eyes are grateful.

      <a class="text-blue-300 hover:text-blue-400">
        Learn more
      </a>
    </p>
  </div>
...
</body>

With the above, the theme will change as the user changes his/her preferences in the system settings.

P.S. If you wanna use dark mode, use the one in-built in TailWindCSS v2. Enable it like this:

< tailwind.config.js >

module.exports = {
  darkMode: 'media',
  ...
}

media can be changed to class.

Media: Now whenever dark mode is enabled on the user's operating system, dark:{class} classes will take precedence over unprefixed classes. The media strategy uses the prefers-color-scheme media feature under the hood.

Class: If you want to support toggling dark mode manually instead of relying on the operating system preference, use the class strategy instead of the media strategy.

Hope this helped you :)

like image 147
RealZombs Avatar answered Oct 22 '22 19:10

RealZombs


I had this problem due to forgetting to update tailwind.config.js:

I changed:

darkMode: false,

to

darkMode: 'class',

I have a simple watcher in Vue that toggles it via:

document.querySelector('html').classList.add('dark')

document.querySelector('html').classList.remove('dark')

You can read more here: https://tailwindcss.com/docs/dark-mode

like image 23
agm1984 Avatar answered Oct 22 '22 19:10

agm1984