Custom themes/variants based on prefix (similar to "dark") #12421
Replies: 6 comments 2 replies
-
This config should work for you: /** @type {import('tailwindcss').Config} */
const plugin = require("tailwindcss/plugin");
module.exports = {
plugins: [
plugin(function ({ addVariant, e }) {
addVariant("onyx", ".onyx &");
})
]
}; With .onyx .onyx\:border-green-500 { --tw-border-opacity: 1; border-color: rgb(34 197 94 / var(--tw-border-opacity)); } The difference between with If you want to have it with addVariant("onyx", `:is(.onyx &)`) Result: :is(.onyx .onyx\:border-green-500) {
--tw-border-opacity: 1;
border-color: rgb(34 197 94 / var(--tw-border-opacity));
} |
Beta Was this translation helpful? Give feedback.
-
That looks awesome! 🎊 I will definitely try this out on my projects. Ideally though, Tailwind would provide this out of the box, allowing anyone to just start describing their theme in their HTML, without any declarations of definitions. Tailwind would then detect the use of this "variant" syntax (i.e. by cross referencing it with pre-existing variants) and then allow this custom variant (or "theme") to be used throughout the rest of the code. Again, this is just me trying to imagine a perfect world. 🌈 I realize there might be constraints, but it would make things feel way more natural. Another way could be to define these variants in the Tailwind config through one What do you think? Am I hallucinating or being spoiled, or both? ;-) I would very much appreciate @adamwathan's opinion on this. |
Beta Was this translation helpful? Give feedback.
-
This is exactly what I needed. Thank guys |
Beta Was this translation helpful? Give feedback.
-
Thank you, you really saved my day |
Beta Was this translation helpful? Give feedback.
-
This should be on the docs, crazy stuff, thank you! |
Beta Was this translation helpful? Give feedback.
-
how to do it on tailwindcss4? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Perhaps this is an idea so simple that it has already been considered inside and out, but perhaps it's so simple it hasn't. ;-)
Anyway, I was thinking on ways to create a few themes and couldn't quite figure out how to do so. I did find ways like adjusting some config rules, setting CSS variables, etc.
My idea would suggest using the same method as
dark
, implying this is essentially a theme. Similar to how we handledark
styling, we could style an element using a custom keyword, e.g. "onyx" in case we're designing a theme called "Onyx".Styling would then look like this:
In conclusion, this idea would allow users to create their own custom theme keyword, of which
dark
is a default example, and would match any other elements using that same theme name (in our caseonyx
).Let me know what you all think! ;-) Including @adamwathan, of course.
Beta Was this translation helpful? Give feedback.
All reactions