Best way creating CSS variables from theme values (CSS-first approach)? #18805
Replies: 3 comments 1 reply
-
There isn't really any analagous in CSS configuration as far as I am aware. You'd be stuck with copy-pasting as you are now. |
Beta Was this translation helpful? Give feedback.
-
Related: Specifically for this example, however, you can generate a utility that allows any integer. @utility animate-opacity-* {
opacity: calc(--value(integer, [integer]) * 1%);
opacity: --value([percentage]);
} |
Beta Was this translation helpful? Give feedback.
-
I’ve run into a similar situation before, and what worked well for me was leaning on @layer base for smaller cases and a simple Tailwind plugin when I needed scalability. With @layer base, it feels lightweight and direct since you can just expose a handful of values as CSS variables.
But once I needed to expose a larger set of theme values, writing them manually quickly became unmanageable. That’s where a tiny plugin in tailwind.config.js felt much cleaner. It loops through the theme values and generates the CSS variables under :root automatically:
This way, I don’t have to worry about maintaining two sources of truth. The Tailwind theme stays the single reference point, and all variables are always in sync. Personally, I’ve found this approach to be the most sustainable. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I'm refactoring a Tailwind CSS project and looking for the modern, recommended way to handle a pattern I was previously using in my tailwind.config.js.
My old approach was to extend the theme with new namespaces, pulling values from other parts of my theme. This created new sets of utility classes. For example:
tailwind.config.js (Old approach)
This worked, but my goal now is to move away from creating more utility classes and instead generate global CSS custom properties (like --animation-delay-100, --animation-opacity-50, etc.) that I can use in my own custom CSS.
I'd like to do this directly in my main CSS file if possible, rather than using a JavaScript plugin. Here is my proposed CSS-based solution:
In my main CSS file
This seems to work, but it feels very manual.
Is there a more dynamic way to do this within CSS without having to list every single value? What is the current best practice for this?
I'm trying to find the scalable way to make my theme values available as standard CSS variables.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions