tailwindcss: Background Opacity does not work when color relies on css variable
What version of Tailwind CSS are you using? For example: v3.0.18
What build tool (or framework if it abstracts the build tool) are you using? Tailwind play, Nx
What browser are you using? Chrome, Firefox
What operating system are you using? Linux
Reproduce
When you add your own color key in the colors object and make the color a css variable the opacity modifier i.e. /50 is not working anymore. If you don’t use a css variable it is working.
Create the html and try to use the opacity modifier at a custom color
<div style="--custom-color: #123" class="h-52 w-52 bg-custom-color/50"></div>
module.exports = {
theme: {
extend: {
colors:{
// Reference a css variable, otherwise it will work
"custom-color": "var(--custom-color)"
}
},
},
plugins: [],
}
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 15 (1 by maintainers)
Ok, also make sure you don’t use comma
This will work
This will not work, i wasted about an hour not understanding why its not working before i saw i was using comma
Hey! See the docs to learn how to make this work: https://tailwindcss.com/docs/customizing-colors#using-css-variables
Agree with @enix79. This is working as expected when you use an hex value directly in the theme, but if you use that same value through a css variable it won’t. So it looks like a bug.
✅ this works:
❌ but this don’t
in both cases, this works:
For anyone stumbling across this in the future… Just a small clarification which was messing me around for a while:
As per the docs: (https://tailwindcss.com/docs/customizing-colors#using-css-variables) you need to define the variables correctly (ie without color space) to make the opacity modifiers work.
HOWEVER, the only way to modify opacity on a color defined in a CSS variable is to use the slash notation, eg:
If you try to use the opacity classes, they will not work:
Hint: Use a nicely crafted regex to update your codebase for this change! 😃
@sfilzek, same for me here at the end of 2024…I would rather classify this as bug?
@jonshipman I’ve already tried all of those alternatives and they do not work for me. After a lot of researching I found that using the color mix function with hex values is what works. https://github.com/tailwindlabs/tailwindcss/issues/9143#issuecomment-1946755547
But this solution causes IDEs like vscode to not display the color, this is so annoying for me.