How to define custom color names with alpha/opacity support using default colors & having the same name in dark & light mode #13483
-
I'd like to use something like The color can be different in light and dark mode, e.g. resolve to Red 300 in light mode and to Red 800 in dark mode. While I know of the possibility to define color variants as shown below and use them as theme: {
extend: {
colors: {
primary: {
light: colors.red["300"],
dark: colors.red["800"],
},
},
}, With CSS variables, I can define different values and refer to them in colors: {
primary: "var(--color-primary)",
} To support alpha/opacity modifiers like colors: {
primary: "rgb(var(--color-focus-contrast) / <alpha-value>)",
} To refer to tailwindcss colors, I'd use something like this: @layer base {
:root {
--color-primary: theme(colors.red.300);
}
// alternatively, use some document-level CSS class / selectors etc.
@media (prefers-color-scheme: dark) {
:root {
--color-primary: theme(colors.red.800);
}
}
} However, So, what's the suggested approach here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hey @kpvhn! To achieve your goal, modify the tailwind.config color specification to utilize relative color syntax. Here's the updated code snippet:
Check out full example: https://play.tailwindcss.com/juMPtEAft4?file=config Relative syntax is not universally supported in all modern browsers, but it is available in |
Beta Was this translation helpful? Give feedback.
Hey @kpvhn!
To achieve your goal, modify the tailwind.config color specification to utilize relative color syntax. Here's the updated code snippet:
Check out full example: https://play.tailwindcss.com/juMPtEAft4?file=config
Relative syntax is not universally supported in all modern browsers, but it is available in
postcss-preset-env
. This allows PostCSS to convert all colors to the compatible variant during the build process.