I’ve been searching all day to find a way to implement different color themes. Most solutions implement this on a small scale (change the body background and text for instance). But what I’ve a large project that has different types of cards, buttons, forms, etc. Something like this won’t be enough of course:
:root {
--body-text-clr: #232323;
--body-bg-clr: #fff;
}
:root[data-theme="dark"] {
--body-text-clr: #fff;
--body-bg-clr: #232323;
}
Usually, I have a lot of colors like this:
:root {
// THOSE USUALLY DON'T CHANGE
--clr-primary-400: somevalue;
--clr-primary-500: somevalue;
--clr-primary-600: somevalue;
// THOSE CHANGE COMPLETELY
--clr-neutral-100: somevalue;
--clr-neutral-200: somevalue;
--clr-neutral-300: somevalue;
--clr-neutral-400: somevalue;
--clr-neutral-500: somevalue;
--clr-neutral-600: somevalue;
}
and every color is used multiple times in different places. I tried to solve this in different ways but none worked. Here’s some of the methods I tried to implement:
Method 1: Leave it as it is
I thought the design was made in a way that every color in the light mode corresponds to another color in the dark mode. But it didn’t take me long until I realized this is not the case. Some elements have a shared background in a particular theme but they don’t share the same background in another theme.
Method 2: Tokens
I tried to do something like this:
// I DO THIS FOR EVERY COMPONENT I HAVE
:root {
--card-text-clr: somevalue;
--card-bg-clr: somevalue;
}
:root[data-theme="dark"] {
--card-text-clr: somevalue;
--card-bg-clr: somevalue;
}
I don’t have a problem that it’s a lot of work to set that up. But my problem was that I couldn’t use any utility classes to set my colors anymore (I won’t generate a utility class for a single use like this .card-bg
or .card-text-clr
). If I want to set the background of my card, I’d have to do it in my CSS file.
I’m looking forwards to see how you approach this problem in your projects.
I don’t have a problem if your solution includes using SASS since I use it already.
2
Answers
1st option: assign the relevant theme class or attr to the body. and write CSS for each component according to the relevant theme class.
2nd option: create the color palette using variables and change the variables. color palette and variable should be designed or planned first on the design side / figma.
for example: https://github.com/royalfig/dark-mode-demo/blob/master/main.scss
You can use whichever is simpler.
Looks like your using
tailwindcss
so you should look at daisyui themes.Alternatively, This is what i do which is influenced by materializecss.
And then for particular widgets such as buttons.