How to Access Theme in Tailwind CSS v4? #16436
Replies: 2 comments 9 replies
-
Hi @mirhamasala I'm kind of stuck on this as well during my migration to v4. In the docs, they state that they hope we can just access the values via
and then reference like in your theme like this
I feel like this is fine for most CSS situations. However for breakpoints, this generates an invalid css like this
Apparently you can't reference css variables in The quick fix for this is to just define the CSS variable Because then you can extract the value using Anyone have any further ideas? |
Beta Was this translation helpful? Give feedback.
-
It would be amazing if Tailwind generated a Then colors, spacing, etc. could be properly type-checked when you're referencing them in TypeScript. In our project with Tailwind v3, we do a lot of this: // We use some nifty TypeScript crawling to get every defined color in our config. Basically dot-notation for `color.shade`:
type TwColor = ...;
const getTwColor = (path: TwColor) => {
// logic to get theme color based on input
};
// Usage
const color = getTwColor("red.500");
const variableColor = getTwColor(myConfig.color); This lets us pass colors around freely with full type-safety. With Tailwind v4's CSS theme setup, there's no way to loop over the available colors or theme values unless we define them manually, which defeats the purpose of parsing the theme. Now we would have to manage a separate "theme" away from the real theme, and keep them in sync manually, which is a nightmare. |
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.
-
Hey Everyone, ☀️
We used to be able to use:
import theme from 'tailwindcss/defaultTheme';
We primarily used it for TypeScript type definitions, like:
We also used theme.screens programmatically, like this:
After upgrading to Tailwind CSS v4, this no longer seems possible.
Is there an official or recommended way to access the theme (types) in v4?
Appreciate any guidance—thanks! 🙏🏼
(We already read about Theme values in JavaScript.)
Beta Was this translation helpful? Give feedback.
All reactions