-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
variable colors with <alpha-value> not working with theme function on plugins #9143
Comments
I hate to be the one doing this but is there any updates on this @thecrypticace? |
No worries! Sorry for the late reply. We have been thinking this through some but other things took priority for the v3.2 release. The reason it doesn't work for plugins like this is two fold:
For example, this works today: https://play.tailwindcss.com/1a6LIjNR2S?file=config — Except for the I think that in an ideal world each plugin would take in the opacity and modify the color itself (or we provide a function to do this for you — not sure though). And we could remove this special case for the
|
This comment was marked as spam.
This comment was marked as spam.
In the Library that I've created which is RippleUI, I encountered with this same issue, I created a function to apply the opacity and then when some color is called, the function will trigger and if the user specifies a opacity will go there Definition of variables in this example The function when a user call some color |
hey 👋 I was trying to add a I'm using this configuration as explained by you above, but can't seem to get it working like in your example: plugin(({ matchUtilities, theme }) => {
matchUtilities(
{
foo: (value) => ({ backgroundColor: value }),
},
{
type: ['color'],
values: theme('colors')
}
)
}) The generated CSS looks like this: background-color: ({ opacityValue =1 })=>oldValue.replace("<alpha-value>", opacityValue) !important; I tried to rename the property from I'm using Tailwind CSS v3.2.6. Can you point me in the right direction? |
It's not working on the class either: We can't apply opacity when using edit: workaround is to apply alpha with hex color: |
@karimhossenbux The reason you can't apply an opacity to Until very very recently there has been no way in browsers to augment any arbitrary color and give it an alpha channel. The only two ways to do this are:
And, unfortunately — at least as of right now — none of those are 100% implemented across all browsers — though I think things have gotten close-ish in just the past 3ish months. Can I Use unfortunately doesn't have up-to-date data at the moment. |
Same issue here. Extending the
|
Just wanted to comment also experiencing the same problem as re2005 extending the theme alpha-value is not being parsed properly. As the docs suggest it should work in the CSS variables section |
This works for me. It was covered in the opacity section of this video https://www.youtube.com/watch?v=MAtaT8BZEAo It seems like the docs don't mention this anywhere? |
Hey @austinm911 thanks for the example, the problem is that we are trying to set |
After little more investigation was very clear how to make it work. First you need to declare your color variable using On your css config:
On tailwind config
Usage:
|
Something that might be valuable. I’ve found that |
Same issues, extend color cannot use opacity. ex bg-primary/20 or bg-primary bg-opacity-20. its not work |
interestingly, see https://play.tailwindcss.com/fX0CEoXvGu addUtilities({
'.test': {
color: theme('colors.primary.DEFAULT'),
background: 'theme("colors.primary.DEFAULT")',
'@apply border-primary': '',
},
}) will generate: .test {
color: rgb(var(--primary) / <alpha-value>);
background: rgb(var(--primary) / 1);
--tw-border-opacity: 1;
border-color: rgb(var(--primary) / var(--tw-border-opacity));
} looks like only |
@stefanprobst Wow, |
The docs for HSL are incorrect
should be
|
@Pagebakers that is incorrect. The example is using the correct "modern" CSS syntax. See example here: https://play.tailwindcss.com/rFbKXTknA2 |
I've stumbled on the same problem in my current project, I've defined some theme colors: And when I reference these colors to attach them to a css variable like so: It get's outputted like so in the css: Very frustrating because my current project depends on those css variables, otherwise i'm stuck for the foreseeable future. It would be nice if I could just target the actual value of the color, so that it just outputs |
this issue has been open for more than half and a year now. when will this issue be fixed? |
This a working version of your example |
So using |
I ran into this issue today and this is fixed it for me. Thanks so much @stefanprobst ! |
Anyone was able to create a workaround when dealing with hex vars? |
You can use hex or any color format you want if you use
You can make it much less wordy with a simple function like: const colorMixAlphaValueWithCustomProperty = customPropertyName => `color-mix(
in srgb,
var(${customPropertyName}),
transparent calc(100% - 100% * <alpha-value>)
)`
/*
colors: {
primary: colorMixAlphaValueWithCustomProperty('--primary-color-in-hex')
secondary: colorMixAlphaValueWithCustomProperty('--secondary-color-in-hex')
}
*/ A sample play: https://play.tailwindcss.com/PLN66o7PtF?file=config |
@crswll thank you very much, but I just realized it was even simpler and decided to open up a PR for a doc example: // tailwind.config.js
module.exports = {
theme: {
colors: {
// For --color-primary: #ff73b3
primary: 'rgb(from var(--color-primary) r g b / <alpha-value>)',
}
}
} |
No problem. The browser support for Firefox is not there for that, though. |
do you have a link to share? I was looking for it right now |
@thecrypticace @crswll In fact, this project of mine successfully uses this approach. |
Hey folks! Looking at this issue today which has been open for quite a while and thought I would share what's possible today with Tailwind CSS v3.x and also what we're hoping to support in the upcoming v4.0 release. Reading through this issue it appears there are two different things being discussed: 1) how to add color opacity modifier support when authoring plugins, and 2) how to define colors using CSS variables while still being able to use the color opacity modifier. I will address these separately below: Writing pluginsThe original reason this issue was open was because @gsmeira was trying to write a Tailwind plugin that could have a color opacity modifier. As @thecrypticace mentioned (#9143 (comment)), this is not possible using the let plugin = require('tailwindcss/plugin')
plugin(function ({ matchUtilities }) {
matchUtilities(
{
foo: (value) => ({ color: value }),
},
{
type: ['color'],
values: {
red: '#ff0000',
green: '#00ff00',
blue: '#0000ff',
},
}
)
}) The only limitation with this approach is that you cannot create a root-level utility that supports the color opacity modifier — something like Using CSS variablesIn addition to writing plugins, it appears that a bunch of people in this thread are also struggling with how to use the color opacity modifier when they are defining their colors using CSS variables. Something like this: // tailwind.config.js
export default {
theme: {
extend: {
colors: {
primary: 'var(--my-primary-color)'
}
},
},
plugins: [],
} While this works as a simple color utility (eg. Our documentation does explain how to do this (see here), however this assumes that you're using rgb colors. If you're using hex, oklch, or other color formats you need to get a little more creative with how you implement this. Let's say you have the following CSS variables defined in your project: html {
--my-hex-color: #ff0000;
--my-oklch-color: oklch(63.24% 0.286 29);
--my-named-color: red;
} Here is how you can use the // tailwind.config.js
export default {
theme: {
extend: {
colors: {
hex: 'color-mix(in srgb, var(--my-hex-color) calc(<alpha-value> * 100%), transparent)',
oklch: 'color-mix(in srgb, var(--my-oklch-color) calc(<alpha-value> * 100%), transparent)',
named: 'color-mix(in srgb, var(--my-named-color) calc(<alpha-value> * 100%), transparent)',
}
},
},
plugins: [],
} With that all in place you can now use Here is a Tailwind Play demoing this: https://play.tailwindcss.com/LJMZg2LLta Really hope that helps! Going to close this issue for now as there are workarounds available in the current version of Tailwind, and it's something we plan to change in v4.0 🤙 |
Hi, just moved across to tailwind/daisyui and think I have the same issue. I need to support older browsers/devices (chrome 106/Android 6). Is there a fix for this? |
@rob-baker what specifically isn't working in your project? Can you share a minimum reproduction? |
Thanks for the quick response. I may of raised this in the wrong place!
Background colours are not working on older devices, I defined a theme in tailwind.config.js:
I don't think the older browsers support "oklch" colors. Static Site: |
It works well for me, with re-using tailwind classes in my custom ones: /** @type {import('tailwindcss').Config} */
// Support custom colors like `bg-surface/50`
// https://github.com/tailwindlabs/tailwindcss/issues/9143#issuecomment-1946755547
function toRgba(variableName) {
return ({ opacityValue }) => {
return `color-mix(in srgb, var(${variableName}) calc(${opacityValue} * 100%), transparent)`
}
}
module.exports = {
content: [
'./src/pages/**/*.{js,ts,jsx,tsx}',
'./src/components/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
colors: {
surface: toRgba('--color-surface'),
'primary-on-surface': toRgba('--color-primary-on-surface'),
'primary-on-bg': toRgba('--color-primary-on-bg'),
'warning-on-surface': toRgba('--color-warning-on-surface'),
'warning-on-bg': toRgba('--color-warning-on-bg'),
},
},
},
plugins: [],
} css file: /* Themes */
:root {
--color-surface: #fff;
--color-primary-on-surface: theme('colors.gray.500');
--color-primary-on-bg: theme('colors.gray.950');
--color-warning-on-surface: theme('colors.orange.500');
--color-warning-on-bg: theme('colors.orange.600');
}
.theme-dark {
--color-surface: #2b202c;
--color-primary-on-surface: theme('colors.gray.50');
--color-primary-on-bg: theme('colors.gray.50');
--color-warning-on-surface: theme('colors.orange.400');
--color-warning-on-bg: theme('colors.orange.500');
} |
@rob-baker FWIW: And relative usage data from caniuse |
It works for me only if I use the legacy format (rgba) as prescribed in official docs. // Using legacy rgba
|
For me, when I use a modern
|
worked |
I'm trying to create a plugin for Tailwind 3.1.8, but when I use variables on colors with the new syntax -
rgb(var(--primary) / <alpha-value>)
- the color stop working.I created an example on https://play.tailwindcss.com/2apCJBfHdC
Removing the
/ <alpha-value>
from the color value make things work again.I'm doing something wrong?
The text was updated successfully, but these errors were encountered: