-
Hi, How can I use CSS variables defined in a data-* selector in the CSS: @import "tailwindcss";
[data-theme="dark"] {
--theme-color-background: red;
--theme-color-foreground: blue;
}
[data-theme="light"] {
--theme-color-background: blue;
--theme-color-foreground: red;
}
@theme {
--color-background: var(--theme-color-background);
--color-foreground: var(--theme-color-foreground);
} HTML: <div>
<div data-theme="dark">
<p class="bg-background p-4 text-foreground">Text</p>
</div>
<div data-theme="light">
<p class="bg-background p-4 text-foreground">Text</p>
</div>
</div> I want to be able to change the theme based on the data attribute on every element on the html. With tailwind v3 everything works fine by using css variables in the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
You'd want to use the This changes the CSS rules from: .bg-background {
background-color: var(--color-background);
} To: .bg-background {
background-color: var(--theme-color-background);
} Allowing your CSS variables to be resolved correctly. |
Beta Was this translation helpful? Give feedback.
-
@wongjn What does the inline keyword do, and why does it solve the problem above? |
Beta Was this translation helpful? Give feedback.
You'd want to use the
inline
tag with@theme
: https://play.tailwindcss.com/nSlymBjv3E?file=cssThis changes the CSS rules from:
To:
Allowing your CSS variables to be resolved correctly.