DEV Community

Adebola
Adebola

Posted on

3 1

The easiest way to achieve dark mode in tailwindcss

Here's a quickest way I've found to achieve dark and light mode theming in tailwindcss.

In tailwind.config.js

In this file, add the following screens to extend. i.e

module.exports = {
  future: {
    removeDeprecatedGapUtilities: true,
  },
  purge: ['./components/**/*.{js,ts,jsx,tsx}', './pages/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {
      colors: {},
      screens: {
        'light': { raw: '(prefers-color-scheme: light)' },
        'dark': { raw: '(prefers-color-scheme: dark)' }
      }
    },
  },
  variants: {
    backgroundColor: ['responsive', 'hover', 'focus', 'checked'],
  },
  plugins: [],
}

Enter fullscreen mode Exit fullscreen mode

Now, we can do something like dark:bg-red-700.

To test, open your Google dev tools console, click the 3 dots icon, click more tools then select rendering.

Under rendering, scroll down to Emulate CSS media feature prefers-color-scheme. In the dropdown, select prefers-color-scheme: dark

You should see our dark:bg-red-700 now take effect.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more