3

I'd like to use custom themes in Tailwind config to set primary/secondary colors for light and dark mode. The Tailwind docs only go over using classes in an html/jsx element like so:

<div class="bg-white dark:bg-slate-900...

Instead of declaring this on every element in my app, I'd like to do the following:

<div class="bg-primary text-secondary" />

and then in config, define something like:

colors: {
  light: {
    primary: "white",
    secondary: "black",
  }
  dark: {
    primary: "black",
    secondary: "white",
  }
}

Does anyone know of a way to do this? I am using Tailwind with React.

user12457151
  • 853
  • 2
  • 12
  • 25

3 Answers3

4

You can use @apply:

.bg-primary {
    @apply bg-white dark:bg-slate-900
}

/* ... */

You can even go so far as to write a script to generate this CSS.

https://tailwindcss.com/docs/functions-and-directives

kelsny
  • 23,009
  • 3
  • 19
  • 48
0

To anyone looking for answers in the future:

I was also looking for the same thing, and the best I think we can do at this point is:

  • Define colors in your tailwind config use css vars
  • in root/body have 2 sets of css vars. One default, and other for dark class

More information about setting tailwind config using css vars:

https://tailwindcss.com/docs/customizing-colors#using-css-variables

-3

I actually found two solutions for this:

  1. Daisy UI has a pretty good solution for this built in. However if you don't want to get stuck with using the entire component library, you can do what daisy is doing under the hood with:

  2. Tailwind theme switcher

user12457151
  • 853
  • 2
  • 12
  • 25